diff --git a/test/Microsoft.DotNet.Cli.Utils.ProcessLifecycle.TestApp/ProcessLifecycleTestApp.cs b/test/Microsoft.DotNet.Cli.Utils.ProcessLifecycle.TestApp/ProcessLifecycleTestApp.cs index 6be7271a24ed..a93585b39061 100644 --- a/test/Microsoft.DotNet.Cli.Utils.ProcessLifecycle.TestApp/ProcessLifecycleTestApp.cs +++ b/test/Microsoft.DotNet.Cli.Utils.ProcessLifecycle.TestApp/ProcessLifecycleTestApp.cs @@ -63,7 +63,12 @@ public static int Main(string[] args) using IDisposable processLifecycle = ProcessLifecycle.Initialize(); CancellationToken cancellationToken = ProcessLifecycle.CancellationToken; - using CancellationTokenRegistration registration = childMode is "uncooperative" or "repeated" + using IDisposable softCancellationTimeout = + childMode is "repeated" or "extended-grace" + ? ProcessLifecycle.OverrideSoftCancellationTimeout(Timeout.InfiniteTimeSpan) + : null!; + using CancellationTokenRegistration registration = + childMode is "cooperative" or "uncooperative" or "repeated" or "extended-grace" ? cancellationToken.Register(() => WriteMarker(markerPath, "signal-cancelled")) : default; @@ -71,8 +76,6 @@ public static int Main(string[] args) if (childMode == "repeated") { - using IDisposable softCancellationTimeout = - ProcessLifecycle.OverrideSoftCancellationTimeout(Timeout.InfiniteTimeSpan); Thread.Sleep(Timeout.Infinite); } @@ -83,17 +86,16 @@ public static int Main(string[] args) if (childMode == "forceful-cooperative") { + using CancellationTokenRegistration forcefulRegistration = + ProcessLifecycle.ForcefulCancellationToken.Register( + () => WriteMarker(markerPath, "forceful-cancelled")); ProcessLifecycle.ForcefulCancellationToken.WaitHandle.WaitOne(); - WriteMarker(markerPath, "forceful-cancelled"); return GetSignalExitCode(cancellationToken); } if (childMode == "extended-grace") { cancellationToken.WaitHandle.WaitOne(); - using IDisposable softCancellationTimeout = - ProcessLifecycle.OverrideSoftCancellationTimeout(Timeout.InfiniteTimeSpan); - WriteMarker(markerPath, "signal-cancelled"); Thread.Sleep(TimeSpan.FromMilliseconds(750)); WriteMarker(markerPath, "override-completed"); return GetSignalExitCode(cancellationToken); @@ -111,6 +113,8 @@ private static void WriteMarker(string path, string value) using FileStream stream = new(path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); using StreamWriter writer = new(stream); writer.Write(value); + writer.Flush(); + File.WriteAllText(path + "." + value, ""); } private static void RunReaperParent(string[] args, string markerPath, string childMode) diff --git a/test/Microsoft.DotNet.Cli.Utils.Tests/ProcessLifecycleTests.cs b/test/Microsoft.DotNet.Cli.Utils.Tests/ProcessLifecycleTests.cs index e97eb85b0a06..2c17f6aaa6de 100644 --- a/test/Microsoft.DotNet.Cli.Utils.Tests/ProcessLifecycleTests.cs +++ b/test/Microsoft.DotNet.Cli.Utils.Tests/ProcessLifecycleTests.cs @@ -11,6 +11,7 @@ namespace Microsoft.DotNet.Cli.Utils.Tests; public class ProcessLifecycleTests : SdkTest { private static readonly TimeSpan s_waitTimeout = TimeSpan.FromSeconds(30); + private static readonly TimeSpan s_processTimingTimeout = TimeSpan.FromSeconds(10); [TestMethod] [OSCondition(ConditionMode.Exclude, OperatingSystems.Windows)] @@ -68,7 +69,7 @@ public void SigIntForcesTerminationAfterSoftCancellationTimeout() child.WaitForExit(s_waitTimeout).Should().BeTrue(); child.ExitCode.Should().Be(130); - stopwatch.Elapsed.Should().BeLessThan(TimeSpan.FromSeconds(1.5)); + stopwatch.Elapsed.Should().BeLessThan(s_processTimingTimeout); } [TestMethod] @@ -114,8 +115,8 @@ public void SecondSigIntBypassesSoftCancellationTimeoutOverride() child.WaitForExit(s_waitTimeout).Should().BeTrue(); child.ExitCode.Should().Be(130); - stopwatch.Elapsed.Should().BeLessThan(TimeSpan.FromSeconds(1)); - File.ReadAllText(GetMarkerPath(testDirectory.Path)).Should().Be("signal-cancelled"); + stopwatch.Elapsed.Should().BeLessThan(s_processTimingTimeout); + MarkerHasValue(GetMarkerPath(testDirectory.Path), "signal-cancelled").Should().BeTrue(); } [TestMethod] @@ -209,7 +210,7 @@ public void ManagedCliWaitsForSecondSigIntBeforeTerminatingExternalCommand() child.WaitForExit(s_waitTimeout).Should().BeTrue(); child.ExitCode.Should().Be(130); - stopwatch.Elapsed.Should().BeLessThan(TimeSpan.FromSeconds(1)); + stopwatch.Elapsed.Should().BeLessThan(s_processTimingTimeout); string standardError = stderrTask.GetAwaiter().GetResult(); standardError.Should().NotContain("Unhandled exception"); standardError.Should().NotContain("The operation was canceled"); @@ -250,7 +251,7 @@ public void SigTermIsForwardedToReapedChild() uncooperativeReaperParent.SafeHandle.Signal(PosixSignal.SIGTERM).Should().BeTrue(); uncooperativeReaperParent.WaitForExit(s_waitTimeout).Should().BeTrue(); uncooperativeReaperParent.ExitCode.Should().NotBe(0); - stopwatch.Elapsed.Should().BeLessThan(TimeSpan.FromSeconds(2)); + stopwatch.Elapsed.Should().BeLessThan(s_processTimingTimeout); } [TestMethod] @@ -319,7 +320,7 @@ private void VerifyRepeatedSignal(PosixSignal signal, int expectedExitCode) SendSignal(child, testDirectory.Path, signal, sequence: 2); child.WaitForExit(s_waitTimeout).Should().BeTrue(); child.ExitCode.Should().Be(expectedExitCode); - stopwatch.Elapsed.Should().BeLessThan(TimeSpan.FromSeconds(1)); + stopwatch.Elapsed.Should().BeLessThan(s_processTimingTimeout); } finally { @@ -331,8 +332,13 @@ private static Process StartChild(string childMode, string testDirectory) { string markerPath = GetMarkerPath(testDirectory); File.Delete(markerPath); - File.Delete(markerPath + ".signal-1"); - File.Delete(markerPath + ".signal-2"); + foreach (string signalMarkerPath in Directory.EnumerateFiles( + testDirectory, + Path.GetFileName(markerPath) + ".*")) + { + File.Delete(signalMarkerPath); + } + Process process = CreateChildProcess(childMode, testDirectory); process.Start(); return process; @@ -395,11 +401,28 @@ private static void WaitForMarker(string testDirectory, string expectedValue) private static bool TryWaitForMarker(string testDirectory, string expectedValue) { string markerPath = GetMarkerPath(testDirectory); - return SpinWait.SpinUntil(() => MarkerHasValue(markerPath, expectedValue), s_waitTimeout); + Stopwatch stopwatch = Stopwatch.StartNew(); + do + { + if (MarkerHasValue(markerPath, expectedValue)) + { + return true; + } + + Thread.Sleep(TimeSpan.FromMilliseconds(10)); + } + while (stopwatch.Elapsed < s_waitTimeout); + + return MarkerHasValue(markerPath, expectedValue); } private static bool MarkerHasValue(string markerPath, string expectedValue) { + if (File.Exists(markerPath + "." + expectedValue)) + { + return true; + } + try { if (!File.Exists(markerPath))