Skip to content

[Bug]: CompletesWithin misreports a canceled task, keeps its timer running, and lost its Task<T> result #7852

Done

Description

@JohnVerheij

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:

  1. A canceled task or action is reported as too slow. In CompletesWithinActionAssertion and CompletesWithinAsyncAssertion, the catch (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.
  2. A passing CompletesWithin keeps its timer running until the timeout. The CancellationTokenSource is disposed but never canceled, so the timer of Task.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.
  3. The failure message ends with an empty at line: the assertion gets a new StringBuilder instead of the source's expression.
  4. CompletesWithin on a Task<T> or a Func<Task<T>> is gone. Up to 0.67.19, var result = await Assert.That(task).CompletesWithin(...) returned the task's result (CompletesWithin<TActual> on IValueDelegateSource<TActual>). Since 0.70.0 it is CS1929, and the docs cast the task to Func<Task> and await it a second time for the result.

Expected Behavior

  1. but threw System.Threading.Tasks.TaskCanceledException: A task was canceled.
  2. No timer left running once the assertion has passed.
  3. at Assert.That(() => canceled).CompletesWithin(TimeSpan.FromSeconds(30))
  4. var result = await Assert.That(task).CompletesWithin(timeout); compiles and gives the task's result, as in 0.67.19.

Actual Behavior

  1. but it took too long to complete, after 29 ms with a 30 s limit.
  2. Timer.ActiveCount grows by one per passing call (about 100 after 100 calls with a one-hour limit), and those timers stay until the hour is over.
  3. at with nothing after it.
  4. 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)); // CS1929

TUnit 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

at

Additional 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 test or dotnet run, not just in my IDE

Activity

  1. added
    bug
    on Nov 25, 2026
  2. added
    P2
    on Nov 25, 2026
  3. added a commit that references this issue on Nov 29, 2026
    9cda217
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions