Skip to content

Commit 6d6062f

Browse files
linkdotnetegil
authored andcommitted
refactor: Use WaitAsync logic for >net6.0
1 parent 50853c1 commit 6d6062f

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

tests/bunit.testassets/AssertExtensions/TaskAssertionExtensions.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ public static class TaskAssertionExtensions
44
{
55
public static async Task ShouldCompleteWithin(this Task task, TimeSpan timeout)
66
{
7-
if (task != await Task.WhenAny(task, Task.Delay(timeout)))
8-
throw new TimeoutException();
7+
#if NET6_0_OR_GREATER
8+
await task.WaitAsync(timeout);
9+
#else
10+
var cts = new CancellationTokenSource();
11+
var delayTask = Task.Delay(timeout, cts.Token);
12+
if (task != await Task.WhenAny(task, delayTask))
13+
throw new TimeoutException();
14+
15+
cts.Cancel();
16+
#endif
917
}
1018
}

0 commit comments

Comments
 (0)