The class TaskCompletionSource<T> is quite useful in unit tests on asynchronous calls. The following assertions helps to check that the result is available within specific time.
| C# |
Copy Code |
|---|---|
var tcs = new TaskCompletionSource<bool>(); await tcs.Should().CompleteWithinAsync(1.Seconds()); | |
The assertion returns the result for subsequent value assertions.
| C# |
Copy Code |
|---|---|
var tcs = new TaskCompletionSource<bool>(); await tcs.Should().CompleteWithinAsync(1.Seconds()).WithResult(true); | |
Additionally it is possible to assert that the task will not complete within specific time.
| C# |
Copy Code |
|---|---|
var tcs = new TaskCompletionSource<bool>(); await tcs.Should().NotCompleteWithinAsync(1.Seconds()); | |