private IAgentLease CreateRemoteAgent(TestPackage package, int waitTime) { var agentId = Guid.NewGuid(); //var process = LaunchAgentProcess(package, agentId); var process = new AgentProcess(this, package, agentId); process.Exited += (sender, e) => OnAgentExit((Process)sender, agentId); process.Start(); log.Debug("Launched Agent process {0} - see nunit-agent_{0}.log", process.Id); log.Debug("Command line: \"{0}\" {1}", process.StartInfo.FileName, process.StartInfo.Arguments); _agentStore.AddAgent(agentId, process); log.Debug($"Waiting for agent {agentId:B} to register"); const int pollTime = 200; // Wait for agent registration based on the agent actually getting processor time to avoid falling over // under process starvation. while (waitTime > process.TotalProcessorTime.TotalMilliseconds && !process.HasExited) { Thread.Sleep(pollTime); if (_agentStore.IsReady(agentId, out var agent)) { log.Debug($"Returning new agent {agentId:B}"); return(new AgentLease(this, agentId, agent)); } } return(null); }
public ITestAgent GetAgent(TestPackage package) { // Target Runtime must be specified by this point string runtimeSetting = package.GetSetting(EnginePackageSettings.TargetRuntimeFramework, ""); Guard.OperationValid(runtimeSetting.Length > 0, "LaunchAgentProcess called with no runtime specified"); // If target runtime is not available, something went wrong earlier. // We list all available frameworks to use in debugging. var targetRuntime = RuntimeFramework.Parse(runtimeSetting); if (!_runtimeService.IsAvailable(targetRuntime.Id)) { string msg = $"The {targetRuntime} framework is not available.\r\nAvailable frameworks:"; foreach (var runtime in RuntimeFramework.AvailableFrameworks) { msg += $" {runtime}"; } throw new ArgumentException(msg); } var agentId = Guid.NewGuid(); var agentProcess = new AgentProcess(this, package, agentId); agentProcess.Exited += (sender, e) => OnAgentExit((Process)sender, agentId); agentProcess.Start(); log.Debug("Launched Agent process {0} - see nunit-agent_{0}.log", agentProcess.Id); log.Debug("Command line: \"{0}\" {1}", agentProcess.StartInfo.FileName, agentProcess.StartInfo.Arguments); _agentStore.AddAgent(agentId, agentProcess); log.Debug($"Waiting for agent {agentId:B} to register"); const int pollTime = 200; // Increase the timeout to give time to attach a debugger bool debug = package.GetSetting(EnginePackageSettings.DebugAgent, false) || package.GetSetting(EnginePackageSettings.PauseBeforeRun, false); int waitTime = debug ? DEBUG_TIMEOUT : NORMAL_TIMEOUT; // Wait for agent registration based on the agent actually getting processor time to avoid falling over // under process starvation. while (waitTime > agentProcess.TotalProcessorTime.TotalMilliseconds && !agentProcess.HasExited) { Thread.Sleep(pollTime); if (_agentStore.IsReady(agentId, out var agent)) { log.Debug($"Returning new agent {agentId:B}"); return(new TestAgentRemotingProxy(agent, agentId)); } } return(null); }