Skip to content

Commit

Permalink
Correct various xUnit warnings relating to task cancellation in tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
fgimian committed Jan 14, 2025
1 parent 7a4e271 commit 23f5e77
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/TotalMixVC.Tests/TaskExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<OperationCanceledException>(
async () => await Task.Run(task).TimeoutAfter(1000, cancellationTokenSource)
async () =>
await Task.Run(task, TestContext.Current.CancellationToken)
.TimeoutAfter(1000, cancellationTokenSource)
);
Assert.False(completed);
await cancelTask;
Expand Down Expand Up @@ -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<OperationCanceledException>(
async () => await Task.Run(task).TimeoutAfter(1000, cancellationTokenSource)
Expand Down

0 comments on commit 23f5e77

Please sign in to comment.