From 20df94c841622f296f3034190bac29980531193d Mon Sep 17 00:00:00 2001 From: Manfred Brands Date: Sun, 9 Jun 2024 13:51:54 +0800 Subject: [PATCH] Runs .NET Core Timeout test in IsolatedContext --- .../Internal/Commands/TimeoutCommand.cs | 28 ++++++------- .../framework/Internal/Results/TestResult.cs | 10 ----- .../tests/Attributes/TimeoutTests.cs | 40 +++++++++++++++++++ 3 files changed, 53 insertions(+), 25 deletions(-) diff --git a/src/NUnitFramework/framework/Internal/Commands/TimeoutCommand.cs b/src/NUnitFramework/framework/Internal/Commands/TimeoutCommand.cs index 94008784fa..15905f50ec 100644 --- a/src/NUnitFramework/framework/Internal/Commands/TimeoutCommand.cs +++ b/src/NUnitFramework/framework/Internal/Commands/TimeoutCommand.cs @@ -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) diff --git a/src/NUnitFramework/framework/Internal/Results/TestResult.cs b/src/NUnitFramework/framework/Internal/Results/TestResult.cs index 1d11d8ea52..51ff9c08ec 100644 --- a/src/NUnitFramework/framework/Internal/Results/TestResult.cs +++ b/src/NUnitFramework/framework/Internal/Results/TestResult.cs @@ -152,16 +152,6 @@ internal void AddTestAttachment(TestAttachment attachment) _testAttachments.Add(attachment); } - /// - /// Apply the output from the provided TestResult to this one - /// - /// The TestResult to apply output from - internal void CopyOutputTo(TestResult result) - { - OutWriter.Flush(); - result.OutWriter.Write(Output); - } - /// /// Gets the collection of files attached to the test /// diff --git a/src/NUnitFramework/tests/Attributes/TimeoutTests.cs b/src/NUnitFramework/tests/Attributes/TimeoutTests.cs index 7804a78166..3e9d63e67c 100644 --- a/src/NUnitFramework/tests/Attributes/TimeoutTests.cs +++ b/src/NUnitFramework/tests/Attributes/TimeoutTests.cs @@ -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; @@ -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)); + } + } } }