Repository navigation
Done
Description
Bug report checklist
- I've searched existing issues to ensure this isn't a duplicate
- I've read the documentation and this isn't expected behavior
- I'm using the latest version of TUnit
Description
Four problems with CompletesWithin:
- A canceled task or action is reported as too slow. In
CompletesWithinActionAssertionandCompletesWithinAsyncAssertion, thecatch (OperationCanceledException)turns the task's own cancellation into "it took too long to complete", even when it finished at once. A faulted task is reported with its exception. - A passing
CompletesWithinkeeps its timer running until the timeout. TheCancellationTokenSourceis disposed but never canceled, so the timer ofTask.Delay(timeout, token)stays alive. Cancel Token when CompletesWithin(TimeSpan) passes #1822 canceled it; that got lost in the rework in Simplify Assertion API #3324. - The failure message ends with an empty
atline: the assertion gets a newStringBuilderinstead of the source's expression. CompletesWithinon aTask<T>or aFunc<Task<T>>is gone. Up to 0.67.19,var result = await Assert.That(task).CompletesWithin(...)returned the task's result (CompletesWithin<TActual>onIValueDelegateSource<TActual>). Since 0.70.0 it is CS1929, and the docs cast the task toFunc<Task>and await it a second time for the result.
Expected Behavior
but threw System.Threading.Tasks.TaskCanceledException: A task was canceled.- No timer left running once the assertion has passed.
at Assert.That(() => canceled).CompletesWithin(TimeSpan.FromSeconds(30))var result = await Assert.That(task).CompletesWithin(timeout);compiles and gives the task's result, as in 0.67.19.
Actual Behavior
but it took too long to complete, after 29 ms with a 30 s limit.Timer.ActiveCountgrows by one per passing call (about 100 after 100 calls with a one-hour limit), and those timers stay until the hour is over.atwith nothing after it.error CS1929: 'TaskAssertion<int>' does not contain a definition for 'CompletesWithin'
Steps to Reproduce
[Test]
public async Task CanceledTaskIsReportedAsTooSlow()
{
var canceled = Task.FromCanceled(new CancellationToken(canceled: true));
await Assert.That(() => canceled).CompletesWithin(TimeSpan.FromSeconds(30));
}
[Test]
public async Task PassingCallsLeaveTheirTimers()
{
var before = Timer.ActiveCount;
for (var i = 0; i < 100; i++)
await Assert.That(() => Task.CompletedTask).CompletesWithin(TimeSpan.FromHours(1));
Console.WriteLine(Timer.ActiveCount - before); // about 100
}And, in a separate file, since it does not compile:
var result = await Assert.That(Task.FromResult(42)).CompletesWithin(TimeSpan.FromSeconds(5)); // CS1929TUnit Version
1.87.12
.NET Version
net10.0
Operating System
Windows
IDE / Test Runner
dotnet CLI (dotnet test / dotnet run)
Error Output / Stack Trace
Expected action to complete within 30000 milliseconds
but it took too long to complete
atAdditional Context
I'll open a PR that fixes all four, with tests.
IDE-Specific Issue?
- I've confirmed this issue occurs when running via
dotnet testordotnet run, not just in my IDE
Activity
fix(assertions): CompletesWithin reports cancellation, stops its time…