Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Microsoft.ComponentDetection.Contracts;
using Microsoft.ComponentDetection.Contracts.BcdeModels;
using Microsoft.ComponentDetection.Orchestrator.ArgumentSets;
using Microsoft.ComponentDetection.Orchestrator.Experiments;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using static System.Environment;
Expand All @@ -23,12 +24,15 @@ public class DetectorProcessingService : IDetectorProcessingService
{
private readonly IObservableDirectoryWalkerFactory scanner;
private readonly ILogger<DetectorProcessingService> logger;
private readonly IExperimentService experimentService;

public DetectorProcessingService(
IObservableDirectoryWalkerFactory scanner,
IExperimentService experimentService,
ILogger<DetectorProcessingService> logger)
{
this.scanner = scanner;
this.experimentService = experimentService;
this.logger = logger;
}

Expand Down Expand Up @@ -105,6 +109,8 @@ public async Task<DetectorProcessingResult> ProcessDetectorsAsync(IDetectionArgu
exitCode = resultCode;
}

this.experimentService.RecordDetectorRun(detector, detectedComponents);

if (isExperimentalDetector)
{
return (new IndividualDetectorScanResult(), new ComponentRecorder(), detector);
Expand All @@ -116,6 +122,7 @@ public async Task<DetectorProcessingResult> ProcessDetectorsAsync(IDetectionArgu
}).ToList();

var results = await Task.WhenAll(scanTasks);
await this.experimentService.FinishAsync();

var detectorProcessingResult = this.ConvertDetectorResultsIntoResult(results, exitCode);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public DetectorProcessingServiceTests()
this.loggerMock = new Mock<ILogger<DetectorProcessingService>>();
this.directoryWalkerFactory = new Mock<IObservableDirectoryWalkerFactory>();
this.serviceUnderTest =
new DetectorProcessingService(this.directoryWalkerFactory.Object, this.loggerMock.Object);
new DetectorProcessingService(this.directoryWalkerFactory.Object, this.experimentServiceMock.Object, this.loggerMock.Object);

this.firstFileComponentDetectorMock = this.SetupFileDetectorMock("firstFileDetectorId");
this.secondFileComponentDetectorMock = this.SetupFileDetectorMock("secondFileDetectorId");
Expand Down Expand Up @@ -509,6 +509,47 @@ public async Task ProcessDetectorsAsync_HandlesDetectorArgsAsync()
.And.Contain("arg3", "val3");
}

[TestMethod]
public async Task ProcessDetectorsAsync_FinishesExperimentsAsync()
{
this.detectorsToUse = new[]
{
this.firstFileComponentDetectorMock.Object, this.secondFileComponentDetectorMock.Object,
};

await this.serviceUnderTest.ProcessDetectorsAsync(DefaultArgs, this.detectorsToUse, new DetectorRestrictions());

this.experimentServiceMock.Verify(x => x.FinishAsync(), Times.Once());
}

[TestMethod]
public async Task ProcessDetectorsAsync_RecordsDetectorRunsAsync()
{
this.detectorsToUse = new[]
{
this.firstFileComponentDetectorMock.Object, this.secondFileComponentDetectorMock.Object,
};

var firstComponents = new[] { this.componentDictionary[this.firstFileComponentDetectorMock.Object.Id] };
var secondComponents = new[] { this.componentDictionary[this.secondFileComponentDetectorMock.Object.Id] };

await this.serviceUnderTest.ProcessDetectorsAsync(DefaultArgs, this.detectorsToUse, new DetectorRestrictions());

this.experimentServiceMock.Verify(
x =>
x.RecordDetectorRun(
It.Is<IComponentDetector>(detector => detector == this.firstFileComponentDetectorMock.Object),
It.Is<IEnumerable<DetectedComponent>>(components => components.SequenceEqual(firstComponents))),
Times.Once());

this.experimentServiceMock.Verify(
x =>
x.RecordDetectorRun(
It.Is<IComponentDetector>(detector => detector == this.secondFileComponentDetectorMock.Object),
It.Is<IEnumerable<DetectedComponent>>(components => components.SequenceEqual(secondComponents))),
Times.Once());
}

private Mock<FileComponentDetector> SetupFileDetectorMock(string id)
{
var mockFileDetector = new Mock<FileComponentDetector>();
Expand Down