From 1fc681af1413a7478f6bd5a611856d0bf753ef07 Mon Sep 17 00:00:00 2001 From: Buddhika Chathuranga Date: Fri, 7 Aug 2020 12:22:57 +0530 Subject: [PATCH] Update RunawayProcessKillerTest.cs --- .../Extensions/RunawayProcessKillerTest.cs | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/Test/winswTests/Extensions/RunawayProcessKillerTest.cs b/src/Test/winswTests/Extensions/RunawayProcessKillerTest.cs index efa4628..b92bcfb 100644 --- a/src/Test/winswTests/Extensions/RunawayProcessKillerTest.cs +++ b/src/Test/winswTests/Extensions/RunawayProcessKillerTest.cs @@ -63,5 +63,61 @@ $@" manager.FireOnWrapperStarted(); manager.FireBeforeWrapperStopped(); } + + [Test] + public void ShouldKillTheSpawnedProcess() + { + Assert.Ignore(); + + var winswId = "myAppWithRunaway"; + var extensionId = "runawayProcessKiller"; + var tmpDir = FilesystemTestHelper.CreateTmpDirectory(); + + // Spawn the test process + Process proc = new Process(); + var ps = proc.StartInfo; + ps.FileName = "cmd.exe"; + ps.Arguments = "/c pause"; + ps.UseShellExecute = false; + ps.RedirectStandardOutput = true; + ps.EnvironmentVariables[WinSWSystem.EnvVarNameServiceId] = winswId; + proc.Start(); + + try + { + // Generate extension and ensure that the roundtrip is correct + var pidfile = Path.Combine(tmpDir, "process.pid"); + var sd = ConfigXmlBuilder.create(id: winswId) + .WithRunawayProcessKiller(new RunawayProcessKillerExtension(pidfile), extensionId) + .ToServiceDescriptor(); + WinSWExtensionManager manager = new WinSWExtensionManager(sd); + manager.LoadExtensions(); + var extension = manager.Extensions[extensionId] as RunawayProcessKillerExtension; + Assert.IsNotNull(extension, "RunawayProcessKillerExtension should be loaded"); + Assert.AreEqual(pidfile, extension.Pidfile, "PidFile should have been retained during the config roundtrip"); + + // Inject PID + File.WriteAllText(pidfile, proc.Id.ToString()); + + // Try to terminate + Assert.That(!proc.HasExited, "Process " + proc + " has exited before the RunawayProcessKiller extension invocation"); + _ = proc.StandardOutput.Read(); + extension.OnWrapperStarted(); + Assert.That(proc.HasExited, "Process " + proc + " should have been terminated by RunawayProcessKiller"); + } + finally + { + if (!proc.HasExited) + { + Console.Error.WriteLine("Test: Killing runaway process with ID=" + proc.Id); + ProcessHelper.StopProcessAndChildren(proc.Id, TimeSpan.FromMilliseconds(100), false); + if (!proc.HasExited) + { + // The test is failed here anyway, but we add additional diagnostics info + Console.Error.WriteLine("Test: ProcessHelper failed to properly terminate process with ID=" + proc.Id); + } + } + } + } } }