Skip to content

Commit ed25c64

Browse files
authored
Translation Layer connection timeout (#1843)
Increasing the connection timeout, and making it configurable between the translationlayer and vstest.console.
1 parent 15ab092 commit ed25c64

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleWrapper.cs

+29-10
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@ namespace Microsoft.TestPlatform.VsTestConsole.TranslationLayer
55
{
66
using System.Collections.Generic;
77
using System.Diagnostics;
8+
using System.Globalization;
89
using System.Linq;
910

1011
using Microsoft.TestPlatform.VsTestConsole.TranslationLayer.Interfaces;
12+
using Microsoft.VisualStudio.TestPlatform.CoreUtilities.Helpers;
1113
using Microsoft.VisualStudio.TestPlatform.CoreUtilities.Tracing;
1214
using Microsoft.VisualStudio.TestPlatform.CoreUtilities.Tracing.Interfaces;
1315
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
1416
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
1517
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.Interfaces;
18+
using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions;
19+
using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions.Interfaces;
20+
using CommunicationUtilitiesResources = Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Resources.Resources;
21+
using CoreUtilitiesConstants = Microsoft.VisualStudio.TestPlatform.CoreUtilities.Constants;
1622

1723
/// <summary>
1824
/// An implementation of <see cref="IVsTestConsoleWrapper"/> to invoke test operations
@@ -28,6 +34,8 @@ public class VsTestConsoleWrapper : IVsTestConsoleWrapper
2834

2935
private readonly ITranslationLayerRequestSender requestSender;
3036

37+
private readonly IProcessHelper processHelper;
38+
3139
private bool sessionStarted;
3240

3341
/// <summary>
@@ -63,7 +71,7 @@ public VsTestConsoleWrapper(string vstestConsolePath) :
6371
/// <param name="vstestConsolePath">Path to the test runner <c>vstest.console.exe</c>.</param>
6472
/// <param name="consoleParameters">The parameters to be passed onto the runner process</param>
6573
public VsTestConsoleWrapper(string vstestConsolePath, ConsoleParameters consoleParameters) :
66-
this(new VsTestConsoleRequestSender(), new VsTestConsoleProcessManager(vstestConsolePath), consoleParameters, TestPlatformEventSource.Instance)
74+
this(new VsTestConsoleRequestSender(), new VsTestConsoleProcessManager(vstestConsolePath), consoleParameters, TestPlatformEventSource.Instance, new ProcessHelper())
6775
{
6876
}
6977

@@ -74,12 +82,14 @@ public VsTestConsoleWrapper(string vstestConsolePath, ConsoleParameters consoleP
7482
/// <param name="processManager">Process manager.</param>
7583
/// <param name="consoleParameters">The parameters to be passed onto the runner process</param>
7684
/// <param name="testPlatformEventSource">Performance event source</param>
77-
internal VsTestConsoleWrapper(ITranslationLayerRequestSender requestSender, IProcessManager processManager, ConsoleParameters consoleParameters, ITestPlatformEventSource testPlatformEventSource)
85+
/// <param name="processHelper">Helper for process related utilities</param>
86+
internal VsTestConsoleWrapper(ITranslationLayerRequestSender requestSender, IProcessManager processManager, ConsoleParameters consoleParameters, ITestPlatformEventSource testPlatformEventSource, IProcessHelper processHelper)
7887
{
7988
this.requestSender = requestSender;
8089
this.vstestConsoleProcessManager = processManager;
8190
this.consoleParameters = consoleParameters;
8291
this.testPlatformEventSource = testPlatformEventSource;
92+
this.processHelper = processHelper;
8393
this.pathToAdditionalExtensions = new List<string>();
8494

8595
this.vstestConsoleProcessManager.ProcessExited += (sender, args) => this.requestSender.OnProcessExited();
@@ -263,20 +273,29 @@ private void EnsureInitialized()
263273
EqtTrace.Info("VsTestConsoleWrapper.EnsureInitialized: Process Started.");
264274
this.sessionStarted = this.WaitForConnection();
265275
}
266-
267-
if (!this.sessionStarted)
268-
{
269-
throw new TransationLayerException("Error connecting to Vstest Command Line");
270-
}
271276
}
272277

273278
private bool WaitForConnection()
274279
{
275280
EqtTrace.Info("VsTestConsoleWrapper.WaitForConnection: Waiting for connection to command line runner.");
276-
var connected = this.requestSender.WaitForRequestHandlerConnection(ConnectionTimeout);
277-
this.testPlatformEventSource.TranslationLayerInitializeStop();
278281

279-
return connected;
282+
var timeout = EnvironmentHelper.GetConnectionTimeout();
283+
if (!this.requestSender.WaitForRequestHandlerConnection(timeout * 1000))
284+
{
285+
var processName = this.processHelper.GetCurrentProcessFileName();
286+
throw new TransationLayerException(
287+
string.Format(
288+
CultureInfo.CurrentUICulture,
289+
CommunicationUtilitiesResources.ConnectionTimeoutErrorMessage,
290+
processName,
291+
CoreUtilitiesConstants.VstestConsoleProcessName,
292+
timeout,
293+
EnvironmentHelper.VstestConnectionTimeout)
294+
);
295+
}
296+
297+
this.testPlatformEventSource.TranslationLayerInitializeStop();
298+
return true;
280299
}
281300
}
282301
}

test/TranslationLayer.UnitTests/VsTestConsoleWrapperTests.cs

+12-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace Microsoft.TestPlatform.VsTestConsole.TranslationLayer.UnitTests
1111
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
1212
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
1313
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client.Interfaces;
14+
using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions.Interfaces;
1415
using Microsoft.VisualStudio.TestTools.UnitTesting;
1516

1617
using Moq;
@@ -23,6 +24,8 @@ public class VsTestConsoleWrapperTests
2324

2425
private Mock<IProcessManager> mockProcessManager;
2526

27+
private Mock<IProcessHelper> mockProcessHelper;
28+
2629
private Mock<ITranslationLayerRequestSender> mockRequestSender;
2730

2831
private readonly List<string> testSources = new List<string> { "Hello", "World" };
@@ -42,11 +45,13 @@ public void TestInitialize()
4245

4346
this.mockRequestSender = new Mock<ITranslationLayerRequestSender>();
4447
this.mockProcessManager = new Mock<IProcessManager>();
48+
this.mockProcessHelper = new Mock<IProcessHelper>();
4549
this.consoleWrapper = new VsTestConsoleWrapper(
4650
this.mockRequestSender.Object,
4751
this.mockProcessManager.Object,
4852
this.consoleParameters,
49-
new Mock<ITestPlatformEventSource>().Object);
53+
new Mock<ITestPlatformEventSource>().Object,
54+
this.mockProcessHelper.Object);
5055

5156
this.mockRequestSender.Setup(rs => rs.WaitForRequestHandlerConnection(It.IsAny<int>())).Returns(true);
5257
this.mockRequestSender.Setup(rs => rs.InitializeCommunication()).Returns(100);
@@ -124,9 +129,11 @@ public void InitializeExtensionsShouldSucceed()
124129
[TestMethod]
125130
public void InitializeExtensionsShouldThrowExceptionOnBadConnection()
126131
{
132+
this.mockProcessHelper.Setup(x => x.GetCurrentProcessFileName()).Returns("DummyProcess");
127133
this.mockRequestSender.Setup(rs => rs.WaitForRequestHandlerConnection(It.IsAny<int>())).Returns(false);
128134

129-
Assert.ThrowsException<TransationLayerException>(() => this.consoleWrapper.InitializeExtensions(new List<string> { "Hello", "World" }));
135+
var exception = Assert.ThrowsException<TransationLayerException>(() => this.consoleWrapper.InitializeExtensions(new List<string> { "Hello", "World" }));
136+
Assert.AreEqual("DummyProcess process failed to connect to vstest.console process after 90 seconds. This may occur due to machine slowness, please set environment variable VSTEST_CONNECTION_TIMEOUT to increase timeout.", exception.Message);
130137
this.mockRequestSender.Verify(rs => rs.InitializeExtensions(It.IsAny<IEnumerable<string>>()), Times.Never);
131138
}
132139

@@ -158,9 +165,11 @@ public void DiscoverTestsShouldCallTestDiscoveryHandler2IfTestDiscoveryHandler1I
158165
[TestMethod]
159166
public void DiscoverTestsShouldThrowExceptionOnBadConnection()
160167
{
168+
this.mockProcessHelper.Setup(x => x.GetCurrentProcessFileName()).Returns("DummyProcess");
161169
this.mockRequestSender.Setup(rs => rs.WaitForRequestHandlerConnection(It.IsAny<int>())).Returns(false);
162170

163-
Assert.ThrowsException<TransationLayerException>(() => this.consoleWrapper.DiscoverTests(new List<string> { "Hello", "World" }, null, null, new Mock<ITestDiscoveryEventsHandler2>().Object));
171+
var exception = Assert.ThrowsException<TransationLayerException>(() => this.consoleWrapper.DiscoverTests(new List<string> { "Hello", "World" }, null, null, new Mock<ITestDiscoveryEventsHandler2>().Object));
172+
Assert.AreEqual("DummyProcess process failed to connect to vstest.console process after 90 seconds. This may occur due to machine slowness, please set environment variable VSTEST_CONNECTION_TIMEOUT to increase timeout.", exception.Message);
164173
this.mockRequestSender.Verify(rs => rs.DiscoverTests(It.IsAny<IEnumerable<string>>(), It.IsAny<string>(), null, It.IsAny<ITestDiscoveryEventsHandler2>()), Times.Never);
165174
}
166175

0 commit comments

Comments
 (0)