Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exit call back to custom test host process #1358

Merged
merged 5 commits into from
Jan 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ public void OnClientProcessExit(string stdError)
this.clientExitErrorMessage = stdError;
this.clientExited.Set();

// Note that we're not explicitly disconnecting the communication channel; wait for the
// channel to determine the disconnection on its own.
// Break communication loop. In somecases(E.g: When tests creates child processes to testhost) communication channel won't break if testhost exits.
this.communicationEndpoint.Stop();
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public interface IProcessHelper
/// <param name="callbackAction">
/// Callback on process exit.
/// </param>
void SetExitCallback(int processId, Action callbackAction);
void SetExitCallback(int processId, Action<object> callbackAction);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

object [](start = 51, length = 6)

explicitly as Process or have an eventArgs..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Process API's are not available in UWP, using it explicitly here would mean we cannot built uwp platform abstraction.


/// <summary>
/// Terminates a process.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ public object LaunchProcess(string processPath, string arguments, string working
process.Exited += (sender, args) =>
{
// Call WaitForExit without again to ensure all streams are flushed,
// Add timeout to avoid indefinite waiting on child process exist.
var p = sender as Process;
try
{
// Add timeout to avoid indefinite waiting on child process exit.
p.WaitForExit(500);
}
catch (InvalidOperationException)
Expand Down Expand Up @@ -131,15 +131,20 @@ public bool TryGetExitCode(object process, out int exitCode)
}

/// <inheritdoc/>
public void SetExitCallback(int processId, Action callbackAction)
public void SetExitCallback(int processId, Action<object> callbackAction)
{
var process = Process.GetProcessById(processId);

process.EnableRaisingEvents = true;
process.Exited += (sender, args) =>
try
{
var process = Process.GetProcessById(processId);
process.EnableRaisingEvents = true;
process.Exited += (sender, args) => callbackAction?.Invoke(sender);
}
catch (ArgumentException)
{
callbackAction.Invoke();
};
// Process.GetProcessById() throws ArgumentException if process is not running(identifier might be expired).
// Invoke callback immediately.
callbackAction?.Invoke(null);
}
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public bool TryGetExitCode(object process, out int exitCode)
}

/// <inheritdoc/>
public void SetExitCallback(int parentProcessId, Action callbackAction)
public void SetExitCallback(int parentProcessId, Action<object> callbackAction)
{
throw new NotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public bool TryGetExitCode(object process, out int exitCode)
}

/// <inheritdoc/>
public void SetExitCallback(int parentProcessId, Action callbackAction)
public void SetExitCallback(int parentProcessId, Action<object> callbackAction)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ private bool LaunchHost(TestProcessStartInfo testHostStartInfo, CancellationToke
{
int processId = this.customTestHostLauncher.LaunchTestHost(testHostStartInfo);
this.testHostProcess = Process.GetProcessById(processId);
this.processHelper.SetExitCallback(processId, this.ExitCallBack);
}
}
catch (OperationCanceledException ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ private bool LaunchHost(TestProcessStartInfo testHostStartInfo, CancellationToke
{
var processId = this.testHostLauncher.LaunchTestHost(testHostStartInfo);
this.testHostProcess = Process.GetProcessById(processId);
this.processHelper.SetExitCallback(processId, this.ExitCallBack);
}
}
catch (OperationCanceledException ex)
Expand Down
2 changes: 1 addition & 1 deletion src/datacollector/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private static void Run(string[] args)
var processHelper = new ProcessHelper();
processHelper.SetExitCallback(
parentProcessId,
() =>
(obj) =>
{
EqtTrace.Info("DataCollector: ParentProcess '{0}' Exited.", parentProcessId);
Environment.Exit(1);
Expand Down
2 changes: 1 addition & 1 deletion src/testhost.x86/DefaultEngineInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void Invoke(IDictionary<string, string> argsDictionary)
var processHelper = new ProcessHelper();
processHelper.SetExitCallback(
parentProcessId,
() =>
(obj) =>
{
EqtTrace.Info("DefaultEngineInvoker: ParentProcess '{0}' Exited.", parentProcessId);
new PlatformEnvironment().Exit(1);
Expand Down
2 changes: 1 addition & 1 deletion src/vstest.console/Processors/PortArgumentProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private static IDesignModeClient InitializeDesignMode(int parentProcessId, IProc
{
if (parentProcessId > 0)
{
processHelper.SetExitCallback(parentProcessId, () =>
processHelper.SetExitCallback(parentProcessId, (obj) =>
{
EqtTrace.Info($"PortArgumentProcessor: parent process:{parentProcessId} exited.");
DesignModeClient.Instance?.HandleParentProcessExit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,18 @@ public void LaunchTestHostShouldUseCustomHostIfSet()
Assert.AreEqual(currentProcess.Id, this.testHostId);
}

[TestMethod]
public void LaunchTestHostShouldSetExitCallbackInCaseCustomHost()
{
var mockCustomLauncher = new Mock<ITestHostLauncher>();
this.testHostManager.SetCustomLauncher(mockCustomLauncher.Object);
var currentProcess = Process.GetCurrentProcess();
mockCustomLauncher.Setup(mc => mc.LaunchTestHost(It.IsAny<TestProcessStartInfo>())).Returns(currentProcess.Id);
this.testHostManager.LaunchTestHostAsync(this.startInfo, CancellationToken.None).Wait();

this.mockProcessHelper.Verify(ph => ph.SetExitCallback(currentProcess.Id, It.IsAny<Action<object>>()));
}

[TestMethod]
public void GetTestSourcesShouldReturnAppropriateSourceIfAppxRecipeIsProvided()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,20 @@ public async Task LaunchTestHostShouldLaunchProcessWithConnectionInfo()
this.mockTestHostLauncher.Verify(thl => thl.LaunchTestHost(It.Is<TestProcessStartInfo>(x => x.Arguments.Equals(expectedArgs))), Times.Once);
}

[TestMethod]
public void LaunchTestHostShouldSetExitCallBackInCaseCustomHost()
{
var expectedProcessId = Process.GetCurrentProcess().Id;
this.mockTestHostLauncher.Setup(thl => thl.LaunchTestHost(It.IsAny<TestProcessStartInfo>())).Returns(expectedProcessId);
this.mockFileHelper.Setup(ph => ph.Exists("testhost.dll")).Returns(true);

var startInfo = this.GetDefaultStartInfo();
this.dotnetHostManager.SetCustomLauncher(this.mockTestHostLauncher.Object);
this.dotnetHostManager.LaunchTestHostAsync(startInfo, CancellationToken.None).Wait();

this.mockProcessHelper.Verify(ph => ph.SetExitCallback(expectedProcessId, It.IsAny<Action<object>>()));
}

[TestMethod]
public void GetTestHostProcessStartInfoShouldIncludeTestHostPathFromSourceDirectoryIfDepsFileNotFound()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void ExecutorInitializeShouldSetProcessExitCallback()

this.executor.Initialize(port.ToString());

this.mockProcessHelper.Verify(ph => ph.SetExitCallback(processId, It.IsAny<Action>()), Times.Once);
this.mockProcessHelper.Verify(ph => ph.SetExitCallback(processId, It.IsAny<Action<object>>()), Times.Once);
}

[TestMethod]
Expand Down