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

Adding CancellationToken to Testhost launch async #917

Merged
merged 9 commits into from Jul 11, 2017

PR Comments

  • Loading branch information...
mayankbansal018 committed Jul 11, 2017
commit 6c78e6b7e41f9dc364753321a3065fc70854b891
@@ -31,11 +31,11 @@ public abstract class ProxyOperationManager
private readonly int connectionTimeout;
private readonly string versionCheckPropertyName = "IsVersionCheckRequired";
private readonly ManualResetEventSlim testHostExited = new ManualResetEventSlim(false);
private readonly ManualResetEventSlim testHostConnectionTimeOut = new ManualResetEventSlim(false);
private bool channelConnected;
private bool initialized;
private string testHostProcessStdError;
private bool testHostLaunchSuccess;
private bool testHostLaunched;
#region Constructors
@@ -52,7 +52,8 @@ protected ProxyOperationManager(ITestRequestSender requestSender, ITestRuntimePr
this.testHostManager = testHostManager;
this.processHelper = new ProcessHelper();
this.initialized = false;
this.testHostLaunchSuccess = false;
this.testHostLaunched = false;
this.channelConnected = false;
}
#endregion
@@ -99,7 +100,7 @@ public virtual void SetupChannel(IEnumerable<string> sources, CancellationToken
{
// Launch the test host.
var hostLaunchedTask = this.testHostManager.LaunchTestHostAsync(testHostStartInfo, cancellationToken);
this.testHostLaunchSuccess = hostLaunchedTask.Result;
this.testHostLaunched = hostLaunchedTask.Result;
}
catch (Exception ex)
{
@@ -108,7 +109,7 @@ public virtual void SetupChannel(IEnumerable<string> sources, CancellationToken
// test host launched failed because of user cancellation or any other runtime exception

This comment has been minimized.

@codito

codito Jul 11, 2017

Contributor

nit: comments start with Caps

@codito

codito Jul 11, 2017

Contributor

nit: comments start with Caps

This comment has been minimized.

@mayankbansal018

mayankbansal018 Jul 11, 2017

Contributor

resolved

@mayankbansal018

mayankbansal018 Jul 11, 2017

Contributor

resolved

// or connection with test host did not happen
if (!this.testHostLaunchSuccess || this.testHostConnectionTimeOut.Wait(0))
if (!this.testHostLaunched || !this.channelConnected)
{
var errorMsg = CrossPlatEngineResources.InitializationFailed;
@@ -148,9 +149,13 @@ public virtual void Close()
try
{
// do not send message if host did not launch
if (this.testHostLaunchSuccess)
if (this.testHostLaunched)
{
this.RequestSender.EndSession();
// We want to give test host a chance to safely close.
// The upper bound for wait should be 100ms.
this.testHostExited.Wait(100);
}
}
catch (Exception ex)
@@ -163,14 +168,10 @@ public virtual void Close()
{
this.initialized = false;
// We don't need to terminate if the test host has already terminated.
// If the host launch fails, please clean test host.
// The upper bound for wait should be 100ms.
if (!this.testHostExited.Wait(100))
{
EqtTrace.Warning("ProxyOperationManager: Timed out waiting for test host to exit. Will terminate process.");
this.testHostManager.CleanTestHostAsync(CancellationToken.None).Wait();
}
EqtTrace.Warning("ProxyOperationManager: Timed out waiting for test host to exit. Will terminate process.");
// please clean up test host.
this.testHostManager.CleanTestHostAsync(CancellationToken.None).Wait();
this.testHostManager.HostExited -= this.TestHostManagerHostExited;
this.testHostManager.HostLaunched -= this.TestHostManagerHostLaunched;
@@ -210,7 +211,7 @@ protected string GetTimestampedLogFile(string logFile)
Path.GetExtension(logFile))).AddDoubleQuote();
}
public void TestHostManagerHostLaunched(object sender, HostProviderEventArgs e)
private void TestHostManagerHostLaunched(object sender, HostProviderEventArgs e)
{
var connTimeout = this.connectionTimeout;
@@ -230,8 +231,11 @@ public void TestHostManagerHostLaunched(object sender, HostProviderEventArgs e)
// Wait for a timeout for the client to connect.
if (!this.RequestSender.WaitForRequestHandlerConnection(connTimeout))
{
this.testHostConnectionTimeOut.Set();
this.channelConnected = false;
return;
}
this.channelConnected = true;
}
private void TestHostManagerHostExited(object sender, HostProviderEventArgs e)
@@ -114,7 +114,7 @@ public void SetCustomLauncher(ITestHostLauncher customLauncher)
}
/// <inheritdoc/>
public virtual async Task<bool> LaunchTestHostAsync(TestProcessStartInfo testHostStartInfo, CancellationToken cancellationToken)
public async Task<bool> LaunchTestHostAsync(TestProcessStartInfo testHostStartInfo, CancellationToken cancellationToken)
{
return await Task.Run(() => this.LaunchHost(testHostStartInfo, cancellationToken), cancellationToken);
}
@@ -222,7 +222,7 @@ public Task CleanTestHostAsync(CancellationToken cancellationToken)
/// Raises HostLaunched event
/// </summary>
/// <param name="e">hostprovider event args</param>
protected virtual void OnHostLaunched(HostProviderEventArgs e)
private void OnHostLaunched(HostProviderEventArgs e)
{
this.HostLaunched.SafeInvoke(this, e, "HostProviderEvents.OnHostLaunched");
}
@@ -231,7 +231,7 @@ protected virtual void OnHostLaunched(HostProviderEventArgs e)
/// Raises HostExited event
/// </summary>
/// <param name="e">hostprovider event args</param>
protected virtual void OnHostExited(HostProviderEventArgs e)
private void OnHostExited(HostProviderEventArgs e)
{
if (!this.hostExitedEventRaised)
{
@@ -142,7 +142,7 @@ public void SetCustomLauncher(ITestHostLauncher customLauncher)
}
/// <inheritdoc/>
public virtual async Task<bool> LaunchTestHostAsync(TestProcessStartInfo testHostStartInfo, CancellationToken cancellationToken)
public async Task<bool> LaunchTestHostAsync(TestProcessStartInfo testHostStartInfo, CancellationToken cancellationToken)
{
return await Task.Run(() => this.LaunchHost(testHostStartInfo, cancellationToken), cancellationToken);
}
@@ -23,6 +23,8 @@ namespace TestPlatform.CrossPlatEngine.UnitTests.Client
[TestClass]
public class ProxyDiscoveryManagerTests
{
private readonly DiscoveryCriteria discoveryCriteria;
private ProxyDiscoveryManager testDiscoveryManager;
private Mock<ITestRuntimeProvider> mockTestHostManager;
@@ -36,8 +38,6 @@ public class ProxyDiscoveryManagerTests
/// </summary>
private int testableClientConnectionTimeout = 400;
private readonly DiscoveryCriteria discoveryCriteria;
public ProxyDiscoveryManagerTests()
{
this.mockTestHostManager = new Mock<ITestRuntimeProvider>();
@@ -52,7 +52,13 @@ public ProxyDiscoveryManagerTests()
// Default setup test host manager as shared (desktop)
this.mockTestHostManager.SetupGet(th => th.Shared).Returns(true);
this.mockTestHostManager.Setup(tmh => tmh.LaunchTestHostAsync(It.IsAny<TestProcessStartInfo>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(true));
this.mockTestHostManager.Setup(tmh => tmh.LaunchTestHostAsync(It.IsAny<TestProcessStartInfo>(), It.IsAny<CancellationToken>()))
.Callback(
() =>
{
this.mockTestHostManager.Raise(thm => thm.HostLaunched += null, new HostProviderEventArgs(string.Empty));
})
.Returns(Task.FromResult(true));
}
[TestMethod]
@@ -48,7 +48,13 @@ public ProxyExecutionManagerTests()
// Default to shared test host
this.mockTestHostManager.SetupGet(th => th.Shared).Returns(true);
this.mockTestHostManager.Setup(tmh => tmh.LaunchTestHostAsync(It.IsAny<TestProcessStartInfo>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(true));
this.mockTestHostManager.Setup(tmh => tmh.LaunchTestHostAsync(It.IsAny<TestProcessStartInfo>(), It.IsAny<CancellationToken>()))
.Callback(
() =>
{
this.mockTestHostManager.Raise(thm => thm.HostLaunched += null, new HostProviderEventArgs(string.Empty));
})
.Returns(Task.FromResult(true));
}
[TestMethod]
@@ -121,7 +127,7 @@ public void InitializeShouldPassAdapterToTestHostManagerFromTestPluginCacheExten
{
// We are updating extension with testadapter only to make it easy to test.
// In product code it filter out testadapter from extension
TestPluginCache.Instance.UpdateExtensions(new List<string> { "abc.TestAdapter.dll", "xyz.TestAdapter.dll"}, false);
TestPluginCache.Instance.UpdateExtensions(new List<string> { "abc.TestAdapter.dll", "xyz.TestAdapter.dll" }, false);
try
{
this.mockRequestSender.Setup(s => s.WaitForRequestHandlerConnection(It.IsAny<int>())).Returns(true);
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.