From 23f5e7798c543b67cc6e0e79a0bd8f5520ab0650 Mon Sep 17 00:00:00 2001 From: Fotis Gimian Date: Wed, 15 Jan 2025 10:06:51 +1100 Subject: [PATCH] Correct various xUnit warnings relating to task cancellation in tests. --- src/TotalMixVC.Tests/TaskExtensionsTests.cs | 32 +++++++++++++-------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/TotalMixVC.Tests/TaskExtensionsTests.cs b/src/TotalMixVC.Tests/TaskExtensionsTests.cs index 0e0767d..e85f8c1 100644 --- a/src/TotalMixVC.Tests/TaskExtensionsTests.cs +++ b/src/TotalMixVC.Tests/TaskExtensionsTests.cs @@ -14,7 +14,7 @@ public async Task TimeoutAfter_Completes_DoesNotThrowException_Async() completed = true; }; - await Task.Run(task).TimeoutAfter(1000); + await Task.Run(task, TestContext.Current.CancellationToken).TimeoutAfter(1000); Assert.True(completed); } @@ -47,14 +47,19 @@ public async Task TimeoutAfter_Cancellation_ThrowsException_Async() completed = true; }; - var cancelTask = Task.Run(async () => - { - await Task.Delay(100); - await cancellationTokenSource.CancelAsync(); - }); + var cancelTask = Task.Run( + async () => + { + await Task.Delay(100); + await cancellationTokenSource.CancelAsync(); + }, + TestContext.Current.CancellationToken + ); await Assert.ThrowsAsync( - async () => await Task.Run(task).TimeoutAfter(1000, cancellationTokenSource) + async () => + await Task.Run(task, TestContext.Current.CancellationToken) + .TimeoutAfter(1000, cancellationTokenSource) ); Assert.False(completed); await cancelTask; @@ -107,11 +112,14 @@ public async Task TimeoutAfter_CancellationWithReturn_ThrowsException_Async() return "Hello"; }; - var cancelTask = Task.Run(async () => - { - await Task.Delay(100); - await cancellationTokenSource.CancelAsync(); - }); + var cancelTask = Task.Run( + async () => + { + await Task.Delay(100); + await cancellationTokenSource.CancelAsync(); + }, + TestContext.Current.CancellationToken + ); await Assert.ThrowsAsync( async () => await Task.Run(task).TimeoutAfter(1000, cancellationTokenSource)