Skip to content

Commit

Permalink
Throw when user tries to run a non-async test with a timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Feb 2, 2024
1 parent 9ebc10c commit 2413c57
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
Expand Up @@ -116,6 +116,9 @@ protected override Task<decimal> InvokeTestMethodAsync(object testClassInstance)

async Task<decimal> InvokeTimeoutTestMethodAsync(object testClassInstance)
{
if (!AsyncUtility.IsAsync(TestMethod))
throw new TestTimeoutException();

var baseTask = base.InvokeTestMethodAsync(testClassInstance);
var resultTask = await Task.WhenAny(baseTask, Task.Delay(TestCase.Timeout));

Expand Down
11 changes: 10 additions & 1 deletion src/xunit.execution/Sdk/Frameworks/TestTimeoutException.cs
Expand Up @@ -9,7 +9,16 @@ namespace Xunit.Sdk
public class TestTimeoutException : Exception
{
/// <summary>
/// Initializes a new instance of <see cref="TestTimeoutException"/>.
/// Initializes a new instance of the <see cref="TestTimeoutException"/> class, returning a
/// message indicating that the test method isn't compatible with timeout functionality.
/// </summary>
public TestTimeoutException()
: base("Tests marked with Timeout are only supported for async tests")
{ }

/// <summary>
/// Initializes a new instance of the <see cref="TestTimeoutException"/> class, returning a
/// message indicating that the test method timed out.
/// </summary>
/// <param name="timeout">The timeout that was exceeded, in milliseconds</param>
public TestTimeoutException(int timeout)
Expand Down
Expand Up @@ -192,7 +192,7 @@ class ClassUnderTest
public Task LongRunningTest() => Task.Delay(10000);

[Fact(Timeout = 10000)]
public void ShortRunningTest() => Task.Delay(10);
public Task ShortRunningTest() => Task.Delay(10);
}
}

Expand Down

0 comments on commit 2413c57

Please sign in to comment.