Skip to content

Commit

Permalink
Runs .NET Core Timeout test in IsolatedContext
Browse files Browse the repository at this point in the history
  • Loading branch information
manfred-brands committed Jun 9, 2024
1 parent 7624029 commit 20df94c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 25 deletions.
28 changes: 13 additions & 15 deletions src/NUnitFramework/framework/Internal/Commands/TimeoutCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,21 @@ public override TestResult Execute(TestExecutionContext context)
{
try
{
var separateContext = new TestExecutionContext(context)
using (new TestExecutionContext.IsolatedContext())
{
CurrentResult = context.CurrentTest.MakeTestResult()
};
var testExecution = Task.Run(() => innerCommand.Execute(separateContext));
var timedOut = Task.WaitAny(new Task[] { testExecution }, _timeout) == -1;
var testExecution = Task.Run(() => innerCommand.Execute(TestExecutionContext.CurrentContext));
var timedOut = Task.WaitAny(new Task[] { testExecution }, _timeout) == -1;

if (timedOut && !_debugger.IsAttached)
{
context.CurrentResult.SetResult(
ResultState.Failure,
$"Test exceeded Timeout value of {_timeout}ms");
}
else
{
context.CurrentResult.CopyOutputTo(separateContext.CurrentResult);
context.CurrentResult = testExecution.GetAwaiter().GetResult();
if (timedOut && !_debugger.IsAttached)
{
context.CurrentResult.SetResult(
ResultState.Failure,
$"Test exceeded Timeout value of {_timeout}ms");
}
else
{
context.CurrentResult = testExecution.GetAwaiter().GetResult();
}
}
}
catch (Exception exception)
Expand Down
10 changes: 0 additions & 10 deletions src/NUnitFramework/framework/Internal/Results/TestResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,6 @@ internal void AddTestAttachment(TestAttachment attachment)
_testAttachments.Add(attachment);
}

/// <summary>
/// Apply the output from the provided TestResult to this one
/// </summary>
/// <param name="result">The TestResult to apply output from</param>
internal void CopyOutputTo(TestResult result)
{
OutWriter.Flush();
result.OutWriter.Write(Output);
}

/// <summary>
/// Gets the collection of files attached to the test
/// </summary>
Expand Down
40 changes: 40 additions & 0 deletions src/NUnitFramework/tests/Attributes/TimeoutTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Abstractions;
Expand Down Expand Up @@ -338,5 +339,44 @@ private class StubDebugger : IDebugger
{
public bool IsAttached { get; set; }
}

[TestFixture]
internal sealed class Issue4723
{
[Test]
#if NETFRAMEWORK
[Timeout(2_000)] // Ok status will be Passed
#else
#pragma warning disable CS0618
[Timeout(2_000)] // Ok status will be Passed
#pragma warning restore CS0618
#endif
public async Task Test_Timeout()
{
await Task.Delay(1_000);
Assert.Pass();
}

[Test]
[CancelAfter(2_000)] // Ok status will be Passed
public async Task Test_CancelAfter(CancellationToken ct)
{
await Task.Delay(1_000, ct);
Assert.Pass();
}

[Test] // Ok status will be Passed
public async Task Test()
{
await Task.Delay(1_000);
Assert.Pass();
}

[TearDown]
public void Cleanup()
{
Assert.That(TestContext.CurrentContext.Result.Outcome.Status, Is.EqualTo(TestStatus.Passed));
}
}
}
}

0 comments on commit 20df94c

Please sign in to comment.