Skip to content

Commit c8e01c2

Browse files
authored
Stop trying to connect if the test host exits unexpectedly (#1853)
* Fixed the issue 1689. Taking into consideration host launch failures while waiting to setup connection, so we bail out sooner.
1 parent e382908 commit c8e01c2

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

src/Microsoft.TestPlatform.CommunicationUtilities/TestRequestSender.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,12 @@ public bool WaitForRequestHandlerConnection(int connectionTimeout, CancellationT
146146
EqtTrace.Verbose("TestRequestSender.WaitForRequestHandlerConnection: waiting for connection with timeout: {0}", connectionTimeout);
147147
}
148148

149-
var waitIndex = WaitHandle.WaitAny(new WaitHandle[] { this.connected.WaitHandle, cancellationToken.WaitHandle }, connectionTimeout);
149+
// Wait until either connection is successful, handled by connected.WaitHandle
150+
// or operation is cancelled, handled by cancellationToken.WaitHandle
151+
// or testhost exits unexpectedly, handled by clientExited.WaitHandle
152+
var waitIndex = WaitHandle.WaitAny(new WaitHandle[] { this.connected.WaitHandle, cancellationToken.WaitHandle, this.clientExited.WaitHandle }, connectionTimeout);
150153

151-
// Return true if wait is because of waitHandle.
154+
// Return true if connection was successful.
152155
return waitIndex == 0;
153156
}
154157

src/Microsoft.TestPlatform.CrossPlatEngine/Client/ProxyOperationManager.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ public virtual bool SetupChannel(IEnumerable<string> sources)
155155
connTimeout *= 5;
156156
}
157157

158-
// Wait for a timeout for the client to connect.
158+
// If TestHost does not launch then throw exception
159+
// If Testhost launches, wait for connection.
159160
if (!this.testHostLaunched ||
160161
!this.RequestSender.WaitForRequestHandlerConnection(connTimeout * 1000, this.CancellationTokenSource.Token))
161162
{
@@ -300,7 +301,6 @@ private void TestHostManagerHostLaunched(object sender, HostProviderEventArgs e)
300301
private void TestHostManagerHostExited(object sender, HostProviderEventArgs e)
301302
{
302303
this.testHostProcessStdError = e.Data;
303-
304304
this.RequestSender.OnClientProcessExit(this.testHostProcessStdError);
305305

306306
this.testHostExited.Set();

test/Microsoft.TestPlatform.CommunicationUtilities.UnitTests/TestRequestSenderTests.cs

+21-2
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,33 @@ public void WaitForRequestHandlerConnectionShouldNotConnectIfExceptionWasThrownB
9898
}
9999

100100
[TestMethod]
101-
public void WaitForRequestHandlerConnectionWithInfiniteTimeoutShouldReturnImmediatelyWhenCancellationRequested()
101+
public void WaitForRequestHandlerConnectionWithTimeoutShouldReturnImmediatelyWhenCancellationRequested()
102102
{
103103
var cancellationTokenSource = new CancellationTokenSource();
104104
cancellationTokenSource.Cancel();
105105

106-
var connected = this.testRequestSender.WaitForRequestHandlerConnection(-1, cancellationTokenSource.Token);
106+
var connectionTimeout = 5000;
107+
var watch = System.Diagnostics.Stopwatch.StartNew();
108+
var connected = this.testRequestSender.WaitForRequestHandlerConnection(connectionTimeout, cancellationTokenSource.Token);
109+
watch.Stop();
107110

108111
Assert.IsFalse(connected);
112+
Assert.IsTrue(watch.ElapsedMilliseconds < connectionTimeout);
113+
}
114+
115+
[TestMethod]
116+
public void WaitForRequestHandlerConnectionWithTimeoutShouldReturnImmediatelyIfHostExitedUnexpectedly()
117+
{
118+
var cancellationTokenSource = new CancellationTokenSource();
119+
this.testRequestSender.OnClientProcessExit("DummyError");
120+
121+
var connectionTimeout = 5000;
122+
var watch = System.Diagnostics.Stopwatch.StartNew();
123+
var connected = this.testRequestSender.WaitForRequestHandlerConnection(connectionTimeout, cancellationTokenSource.Token);
124+
watch.Stop();
125+
126+
Assert.IsFalse(connected);
127+
Assert.IsTrue(watch.ElapsedMilliseconds < connectionTimeout);
109128
}
110129

111130
[TestMethod]

0 commit comments

Comments
 (0)