Skip to content

Commit

Permalink
Refactor scanning service to replace event based scanning (#41)
Browse files Browse the repository at this point in the history
* Refactor scanning service to replace event based scanning
  • Loading branch information
Erik-White authored Sep 4, 2022
1 parent a2a778f commit 489516d
Show file tree
Hide file tree
Showing 13 changed files with 247 additions and 194 deletions.
83 changes: 54 additions & 29 deletions Mosey.Application.Tests/IntervalScanningServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using AutoFixture.NUnit3;
using FluentAssertions;
using Mosey.Application.Tests.AutoData;
using Mosey.Core;
using Mosey.Core.Imaging;
using Mosey.Tests.AutoData;
using Mosey.Tests.Extensions;
Expand Down Expand Up @@ -49,19 +50,6 @@ public async Task RepeatRefreshDevices([Frozen] IImagingHost scanningHost, Inter
.Should().BeInRange(5, 15);
}

[Theory, AutoNSubstituteData]
public async Task CancelTask([Frozen] IImagingHost scanningHost, IntervalScanningService sut)
{
using var cts = new CancellationTokenSource();
cts.Cancel();

await sut.BeginRefreshDevices(TimeSpan.FromSeconds(1), cts.Token);

await scanningHost
.DidNotReceiveWithAnyArgs()
.RefreshDevicesAsync(default, default);
}

[Theory, AutoNSubstituteData]
public async Task Raise_DevicesRefreshedEvent(IntervalScanningService sut)
{
Expand Down Expand Up @@ -94,38 +82,75 @@ public class StartScanningShould
[Theory, IntervalScanningServiceAutoData]
public void SetScanningProperties(IntervalScanningService sut)
{
sut.StartScanning(TimeSpan.FromDays(1), TimeSpan.Zero, 1);
_ = sut.StartScanning(new IntervalTimerConfig(TimeSpan.FromDays(1), TimeSpan.Zero, 1));

sut.IsScanRunning.Should().BeTrue();
sut.ScanRepetitionsCount.Should().Be(0);
sut.StartTime.Should().BeBefore(sut.FinishTime);
sut.FinishTime.Should().BeAfter(sut.StartTime);
}

[Theory, IntervalScanningServiceAutoData]
public async Task Raise_ScanRepetitionCompletedEvent([Greedy] IntervalScanningService sut)
public async Task Cancel_BeforeScanning([Greedy] IntervalScanningService sut)
{
using (var monitoredSubject = sut.Monitor())
ScanningProgress? finalProgress = null;
var progress = new Progress<ScanningProgress>((report) =>
{
sut.StartScanning(TimeSpan.Zero, TimeSpan.Zero, 1);
finalProgress = report;
});

await Task.Delay(1000);
var scanningTask = sut.StartScanning(new IntervalTimerConfig(TimeSpan.FromDays(1), TimeSpan.Zero, 100), progress);
await Task.WhenAny(scanningTask, Task.Delay(100));
sut.StopScanning(waitForCompletion: false);

monitoredSubject.Should().Raise(nameof(IntervalScanningService.ScanRepetitionCompleted));
}
finalProgress.Should().BeNull();
}
}

public class StopScanningShould
{
[Theory, IntervalScanningServiceAutoData]
public void Raise_ScanningCompletedEvent(IntervalScanningService sut)
public void Cancel_DuringScanning([Greedy] IntervalScanningService sut)
{
ScanningProgress? finalProgress = null;
var progress = new Progress<ScanningProgress>((report) =>
{
finalProgress = report;
});

using (var monitoredSubject = sut.Monitor())
_ = sut.StartScanning(new IntervalTimerConfig(TimeSpan.Zero, TimeSpan.FromSeconds(10), 100), progress);
sut.StopScanning();

finalProgress?.Should().NotBeNull();
finalProgress?.RepetitionCount.Should().Be(1);
finalProgress?.Stage.Should().Be(ScanningProgress.ScanningStage.Finish);
}

[Theory, IntervalScanningServiceAutoData]
public async Task Report_IterationCount(int iterations, [Greedy] IntervalScanningService sut)
{
var iterationCount = 0;
var progress = new Progress<ScanningProgress>((report) =>
{
sut.StopScanning();
iterationCount = report.RepetitionCount;
});

monitoredSubject.Should().Raise(nameof(IntervalScanningService.ScanningCompleted));
}
await sut.StartScanning(new IntervalTimerConfig(TimeSpan.Zero, TimeSpan.Zero, iterations), progress);

iterationCount.Should().Be(iterations);
}

[Theory, IntervalScanningServiceAutoData, Repeat(5)]
public async Task Report_RepetitionsInAscendingOrder(int iterations, [Greedy] IntervalScanningService sut)
{
var repetitions = new List<int>();
var progress = new Progress<ScanningProgress>((report) =>
{
if (report.Stage == ScanningProgress.ScanningStage.Finish)
{
repetitions.Add(report.RepetitionCount);
}
});

await sut.StartScanning(new IntervalTimerConfig(TimeSpan.Zero, TimeSpan.Zero, iterations), progress);

repetitions.Should().BeInAscendingOrder();
}
}
}
Expand Down
12 changes: 0 additions & 12 deletions Mosey.Application/EventArgs/ExceptionEventArgs.cs

This file was deleted.

12 changes: 0 additions & 12 deletions Mosey.Application/EventArgs/StringCollectionEventArgs.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Mosey.Application/Imaging/DNTScannerDevices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private static T WIARetry<T>(Func<T> method, int connectRetries, TimeSpan retryD
result = method.Invoke();
break;
}
catch (Exception ex) when (ex is COMException || ex is NullReferenceException)
catch (Exception ex) when (ex is COMException || ex is AccessViolationException || ex is NullReferenceException)
{
if (--connectRetries > 0)
{
Expand Down
2 changes: 1 addition & 1 deletion Mosey.Application/Imaging/DntScanningHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task<IEnumerable<CapturedImage>> PerformImagingAsync(bool useHighes
using (var staQueue = new StaTaskScheduler(concurrencyLevel: 1, disableComObjectEagerCleanup: true))
{
results = await Task.Factory.StartNew(
() => PerformImaging(useHighestResolution, cancellationToken),
() => PerformImaging(useHighestResolution, cancellationToken).ToList(),
cancellationToken,
TaskCreationOptions.LongRunning,
staQueue)
Expand Down
Loading

0 comments on commit 489516d

Please sign in to comment.