예제 #1
0
        public void ShouldKillTheSpawnedProcess()
        {
            var winswId     = "myAppWithRunaway";
            var extensionId = "runawayProcessKiller";
            var tmpDir      = FilesystemTestHelper.CreateTmpDirectory();

            // Prepare the env var
            String varName = WinSWSystem.ENVVAR_NAME_SERVICE_ID;
            var    env     = new Dictionary <string, string>();

            env.Add("varName", winswId);

            // Spawn the test process
            var scriptFile = Path.Combine(tmpDir, "dosleep.bat");
            var envFile    = Path.Combine(tmpDir, "env.txt");

            File.WriteAllText(scriptFile, "set > " + envFile + "\nsleep 100500");
            Process proc = new Process();
            var     ps   = proc.StartInfo;

            ps.FileName = scriptFile;
            ProcessHelper.StartProcessAndCallbackForExit(proc, envVars: env);

            try
            {
                // Generate extension and ensure that the roundtrip is correct
                //TODO: checkWinSWEnvironmentVariable should be true, but it does not work due to proc.StartInfo.EnvironmentVariables
                var pidfile = Path.Combine(tmpDir, "process.pid");
                var sd      = ConfigXmlBuilder.create(id: winswId)
                              .WithRunawayProcessKiller(new RunawayProcessKillerExtension(pidfile, checkWinSWEnvironmentVariable: false), 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");
                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);
                    }
                }
            }
        }
예제 #2
0
        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.ENVVAR_NAME_SERVICE_ID] = 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);
                    }
                }
            }
        }