Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timeout attribute ignored

Tags:

c#

.net

xunit.net

I use console runner (xUnit 2.4.1) to execute set of tests. Some tests should fail in case they need too long to finish, but it seems like Timeout attribute is ignored.

This is my simple test method that should fail:

[Fact(Timeout = 1000)]
public void ShouldTimeout()
{
    Thread.Sleep(15000);
}

I execute runner with the option -parallel none in order to avoid undefined behaviour:

xunit.console.exe tests.dll -parallel none -method "Tests.TessClass.ShouldTimeout"

and I also added [assembly: CollectionBehavior(DisableTestParallelization = true)] into my assembly configuration.

The test, however, still finishes successfully with the following output.

xUnit.net Console Runner v2.4.1 (64-bit Desktop .NET 4.5.2, runtime: 4.0.30319.42000)
  Discovering: tests
  Discovered:  tests
  Starting:    tests
  Finished:    tests
=== TEST EXECUTION SUMMARY ===
   tests  Total: 1, Errors: 0, Failed: 0, Skipped: 0, Time: 15,151s

Did I miss something? Is the parallelization still not disabled and therefore Timeout is ignored?

like image 721
Marek Klein Avatar asked Sep 02 '25 02:09

Marek Klein


1 Answers

Looks like xUnit code has some internal SynchronizationContext usage, which results in all threads blocked when synchronous wait is executed inside a test - i.e. Thread.Sleep(). See source code

But, Timeout works just fine if your test code is fully async:

[Fact(Timeout = 1000)]
public async void ShouldTimeout()
{
    await Task.Delay(3000);
}

So I see 2 options here for you: either create a bug for xUnit regarding synchronous code blocking breaking Timeout behavior or rewrite your code to be async if your code base allows it.

like image 195
Yura Avatar answered Sep 04 '25 16:09

Yura