diff --git a/src/NUnitEngine/nunit-agent/Program.cs b/src/NUnitEngine/nunit-agent/Program.cs index d5140d397..d132ba094 100644 --- a/src/NUnitEngine/nunit-agent/Program.cs +++ b/src/NUnitEngine/nunit-agent/Program.cs @@ -41,7 +41,9 @@ public class NUnitTestAgent static Guid AgentId; static string AgencyUrl; + static Process AgencyProcess; static ITestAgency Agency; + static RemoteTestAgent Agent; /// /// Channel used for communications with the agency @@ -65,7 +67,7 @@ public static int Main(string[] args) for (int i = 2; i < args.Length; i++) { string arg = args[i]; - + // NOTE: we can test these strings exactly since // they originate from the engine itself. if (arg == "--debug-agent") @@ -77,6 +79,11 @@ public static int Main(string[] args) { traceLevel = (InternalTraceLevel)Enum.Parse(typeof(InternalTraceLevel), arg.Substring(8)); } + else if (arg.StartsWith("--pid=")) + { + int agencyProcessId = int.Parse(arg.Substring(6)); + AgencyProcess = Process.GetProcessById(agencyProcessId); + } } // Initialize trace so we can see what's happening @@ -141,16 +148,12 @@ public static int Main(string[] args) if (Channel != null) { log.Info("Starting RemoteTestAgent"); - RemoteTestAgent agent = new RemoteTestAgent(AgentId, Agency, engine.Services); + Agent = new RemoteTestAgent(AgentId, Agency, engine.Services); try { - if (agent.Start()) - { - log.Debug("Waiting for stopSignal"); - agent.WaitForStop(); - log.Debug("Stop signal received"); - } + if (Agent.Start()) + WaitForStop(); else log.Error("Failed to start RemoteTestAgent"); } @@ -174,5 +177,21 @@ public static int Main(string[] args) return 0; } + + private static void WaitForStop() + { + log.Debug("Waiting for stopSignal"); + + while (!Agent.WaitForStop(500)) + { + if (AgencyProcess.HasExited) + { + log.Error("Parent process has been terminated."); + Environment.Exit(-1); + } + } + + log.Debug("Stop signal received"); + } } } diff --git a/src/NUnitEngine/nunit.engine/Agents/RemoteTestAgent.cs b/src/NUnitEngine/nunit.engine/Agents/RemoteTestAgent.cs index ae549a239..8daa525d9 100644 --- a/src/NUnitEngine/nunit.engine/Agents/RemoteTestAgent.cs +++ b/src/NUnitEngine/nunit.engine/Agents/RemoteTestAgent.cs @@ -105,9 +105,9 @@ public override void Stop() stopSignal.Set(); } - public void WaitForStop() + public bool WaitForStop(int timeout) { - stopSignal.WaitOne(); + return stopSignal.WaitOne(timeout); } #endregion diff --git a/src/NUnitEngine/nunit.engine/Services/TestAgency.cs b/src/NUnitEngine/nunit.engine/Services/TestAgency.cs index 64af818c7..c637cad44 100644 --- a/src/NUnitEngine/nunit.engine/Services/TestAgency.cs +++ b/src/NUnitEngine/nunit.engine/Services/TestAgency.cs @@ -167,10 +167,10 @@ private Guid LaunchAgentProcess(TestPackage package) bool debugAgent = package.GetSetting(EnginePackageSettings.DebugAgent, false); string traceLevel = package.GetSetting(EnginePackageSettings.InternalTraceLevel, "Off"); bool loadUserProfile = package.GetSetting(EnginePackageSettings.LoadUserProfile, false); - + // Set options that need to be in effect before the package // is loaded by using the command line. - string agentArgs = string.Empty; + string agentArgs = "--pid=" + Process.GetCurrentProcess().Id.ToString(); if (debugAgent) agentArgs += " --debug-agent"; if (traceLevel != "Off")