Skip to content

Commit 3d42279

Browse files
authored
fix unit test issue on macos (#29)
1 parent b094e42 commit 3d42279

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

tests/AsyncNavigation.Tests/AsyncJobProcessorTests.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -233,28 +233,29 @@ public async Task RunJobAsync_QueueStrategy_Should_Handle_10000_ConcurrentJobs()
233233
public async Task RunJobAsync_CancelCurrentStrategy_Should_Handle_RapidSuccessiveJobs()
234234
{
235235
var processor = _serviceProvider.GetRequiredService<IAsyncJobProcessor>();
236-
var iterations = 100;
237-
var cancellationDelayMs = 50;
238-
var jobDurationMs = 200;
236+
const int iterations = 100;
237+
const int cancellationDelayMs = 20;
238+
const int jobDurationMs = 500;
239239
var stopwatch = Stopwatch.StartNew();
240240
List<TestJobContext> jobs = [];
241241

242242
_testOutputHelper.WriteLine($"Starting {iterations} rapid jobs with CancelCurrent strategy...");
243243

244-
for (int i = 0; i < iterations; i++)
244+
for (var i = 0; i < iterations; i++)
245245
{
246246
var context = new TestJobContext();
247247
jobs.Add(context);
248-
var task = processor.RunJobAsync(context, async ctx =>
248+
var index = i;
249+
_ = processor.RunJobAsync(context, async ctx =>
249250
{
250251
try
251252
{
252253
await Task.Delay(jobDurationMs, ctx.CancellationToken);
253-
_testOutputHelper.WriteLine($"The job '{i}' was completed!");
254+
_testOutputHelper.WriteLine($"The job '{index}' was completed!");
254255
}
255256
catch (OperationCanceledException)
256257
{
257-
_testOutputHelper.WriteLine($"The job '{i}' was canceled!");
258+
_testOutputHelper.WriteLine($"The job '{index}' was canceled!");
258259
}
259260
}, NavigationJobStrategy.CancelCurrent);
260261

@@ -266,19 +267,17 @@ public async Task RunJobAsync_CancelCurrentStrategy_Should_Handle_RapidSuccessiv
266267

267268
_testOutputHelper.WriteLine($"Rapid cancellation test completed in {stopwatch.ElapsedMilliseconds} ms");
268269
_testOutputHelper.WriteLine($"Jobs in queue: {processor.JobsCount}");
269-
_testOutputHelper.WriteLine($"Jobs cancelled: {jobs.Where(j => j.CancellationToken.IsCancellationRequested).Count()}");
270-
_testOutputHelper.WriteLine($"Jobs completed: {jobs.Where(j => !j.CancellationToken.IsCancellationRequested).Count()}");
270+
_testOutputHelper.WriteLine($"Jobs cancelled: {jobs.Count(j => j.CancellationToken.IsCancellationRequested)}");
271+
_testOutputHelper.WriteLine($"Jobs completed: {jobs.Count(j => !j.CancellationToken.IsCancellationRequested)}");
271272

272-
if (OperatingSystem.IsWindows())
273-
Assert.InRange(jobs.Where(j => !j.CancellationToken.IsCancellationRequested).Count(), 1, 1);
274-
else if (OperatingSystem.IsLinux())
275-
Assert.InRange(jobs.Where(j => !j.CancellationToken.IsCancellationRequested).Count(), 1, 1);
273+
if (OperatingSystem.IsWindows() || OperatingSystem.IsLinux())
274+
Assert.InRange(jobs.Count(j => !j.CancellationToken.IsCancellationRequested), 1, 1);
276275
else if (OperatingSystem.IsMacOS())
277-
Assert.InRange(jobs.Where(j => !j.CancellationToken.IsCancellationRequested).Count(), 1, 2);
276+
Assert.InRange(jobs.Count(j => !j.CancellationToken.IsCancellationRequested), 1, 2);
278277
else
279278
{
280279
_testOutputHelper.WriteLine($"Unknown OS - {Environment.OSVersion.Platform}");
281-
Assert.InRange(jobs.Where(j => !j.CancellationToken.IsCancellationRequested).Count(), 1, 2);
280+
Assert.InRange(jobs.Count(j => !j.CancellationToken.IsCancellationRequested), 1, 2);
282281
}
283282

284283
}

0 commit comments

Comments
 (0)