diff --git a/playground/TestPlatform.Playground/Program.cs b/playground/TestPlatform.Playground/Program.cs index 81a3d588e4..400e16c928 100644 --- a/playground/TestPlatform.Playground/Program.cs +++ b/playground/TestPlatform.Playground/Program.cs @@ -9,6 +9,9 @@ using System.Linq; using System.Reflection; using System.Threading; +#pragma warning disable IDE0005 // Using directive is unnecessary. +using System.Threading.Tasks; +#pragma warning restore IDE0005 // Using directive is unnecessary. using Microsoft.TestPlatform.VsTestConsole.TranslationLayer; using Microsoft.VisualStudio.TestPlatform.ObjectModel; @@ -20,7 +23,7 @@ namespace TestPlatform.Playground; internal class Program { - static void Main() + static async Task Main() { // This project references TranslationLayer, vstest.console, TestHostProvider, testhost and MSTest1 projects, to make sure // we build all the dependencies of that are used to run tests via VSTestConsoleWrapper. It then copies the components from @@ -141,15 +144,17 @@ static void Main() #pragma warning restore CS0618 // Type or member is obsolete var discoveryHandler = new PlaygroundTestDiscoveryHandler(detailedOutput); var sw = Stopwatch.StartNew(); + // Discovery - r.DiscoverTests(sources, sourceSettings, options, sessionHandler.TestSessionInfo, discoveryHandler); + await r.DiscoverTestsAsync(sources, sourceSettings, options, sessionHandler.TestSessionInfo, discoveryHandler); + // r.DiscoverTests(sources, sourceSettings, options, sessionHandler.TestSessionInfo, discoveryHandler); var discoveryDuration = sw.ElapsedMilliseconds; Console.WriteLine($"Discovery done in {discoveryDuration} ms"); sw.Restart(); // Run with test cases and custom testhost launcher //r.RunTestsWithCustomTestHost(discoveryHandler.TestCases, sourceSettings, options, sessionHandler.TestSessionInfo, new TestRunHandler(detailedOutput), new DebuggerTestHostLauncher()); //// Run with test cases and without custom testhost launcher - r.RunTests(discoveryHandler.TestCases, sourceSettings, options, sessionHandler.TestSessionInfo, new TestRunHandler(detailedOutput)); + await r.RunTestsAsync(discoveryHandler.TestCases, sourceSettings, options, sessionHandler.TestSessionInfo, new TestRunHandler(detailedOutput)); //// Run with sources and custom testhost launcher and debugging //r.RunTestsWithCustomTestHost(sources, sourceSettings, options, sessionHandler.TestSessionInfo, new TestRunHandler(detailedOutput), new DebuggerTestHostLauncher()); //// Run with sources diff --git a/playground/TestPlatform.Playground/Properties/launchSettings.json b/playground/TestPlatform.Playground/Properties/launchSettings.json new file mode 100644 index 0000000000..fb22884caf --- /dev/null +++ b/playground/TestPlatform.Playground/Properties/launchSettings.json @@ -0,0 +1,11 @@ +{ + "profiles": { + "TestPlatform.Playground": { + "commandName": "Project", + "environmentVariables": { + "DOTNET_ThreadPool_ForceMinWorkerThreads": "1", + "DOTNET_ThreadPool_ForceMaxWorkerThreads": "1" + } + } + } +} diff --git a/playground/TestPlatform.Playground/TestPlatform.Playground.csproj b/playground/TestPlatform.Playground/TestPlatform.Playground.csproj index 7259dc0728..510dfd4e73 100644 --- a/playground/TestPlatform.Playground/TestPlatform.Playground.csproj +++ b/playground/TestPlatform.Playground/TestPlatform.Playground.csproj @@ -33,7 +33,7 @@ - + diff --git a/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/Interfaces/ITranslationLayerRequestSenderAsync.cs b/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/Interfaces/ITranslationLayerRequestSenderAsync.cs index 161ecb31bb..9ab845dcfd 100644 --- a/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/Interfaces/ITranslationLayerRequestSenderAsync.cs +++ b/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/Interfaces/ITranslationLayerRequestSenderAsync.cs @@ -17,6 +17,9 @@ namespace Microsoft.TestPlatform.VsTestConsole.TranslationLayer.Interfaces; /// internal interface ITranslationLayerRequestSenderAsync : IDisposable { + + int StartServer(); + /// /// Asynchronous equivalent of @@ -24,7 +27,7 @@ internal interface ITranslationLayerRequestSenderAsync : IDisposable /// ITranslationLayerRequestSender.WaitForRequestHandlerConnection( /// int)"/>. /// - Task InitializeCommunicationAsync(int clientConnectionTimeout); + Task InitializeCommunicationAsync(int clientConnectionTimeout); /// /// Asynchronous equivalent of ITranslationLayerRequestSender.DiscoverTests/>. diff --git a/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleProcessManager.cs b/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleProcessManager.cs index 9a7cb250f4..77de94124c 100644 --- a/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleProcessManager.cs +++ b/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleProcessManager.cs @@ -160,10 +160,10 @@ public void StartProcess(ConsoleParameters consoleParameters) _process!.EnableRaisingEvents = true; _process.Exited += Process_Exited; - _process.OutputDataReceived += Process_OutputDataReceived; - _process.ErrorDataReceived += Process_ErrorDataReceived; - _process.BeginOutputReadLine(); - _process.BeginErrorReadLine(); + // _process.OutputDataReceived += Process_OutputDataReceived; + // _process.ErrorDataReceived += Process_ErrorDataReceived; + // _process.BeginOutputReadLine(); + // _process.BeginErrorReadLine(); _processExitedEvent.Reset(); } diff --git a/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleRequestSender.cs b/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleRequestSender.cs index 4c623ed472..95221a8e6a 100644 --- a/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleRequestSender.cs +++ b/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleRequestSender.cs @@ -120,36 +120,34 @@ public bool WaitForRequestHandlerConnection(int clientConnectionTimeout) return waitSuccess && _handShakeSuccessful; } + public int StartServer() + { + return _communicationManager.HostServer(new IPEndPoint(IPAddress.Loopback, 0)).Port; + } + /// - public async Task InitializeCommunicationAsync(int clientConnectionTimeout) + public async Task InitializeCommunicationAsync(int clientConnectionTimeout) { EqtTrace.Info($"VsTestConsoleRequestSender.InitializeCommunicationAsync: Started with client connection timeout {clientConnectionTimeout} milliseconds."); _processExitCancellationTokenSource = new CancellationTokenSource(); + _handShakeSuccessful = false; _handShakeComplete.Reset(); - int port = -1; + try { - port = _communicationManager.HostServer(new IPEndPoint(IPAddress.Loopback, 0)).Port; - var timeoutSource = new CancellationTokenSource(clientConnectionTimeout); - await Task.Run(() => - _communicationManager.AcceptClientAsync(), timeoutSource.Token).ConfigureAwait(false); + // todo: report standard error on timeout + await Task.WhenAny(_communicationManager.AcceptClientAsync(), Task.Delay(clientConnectionTimeout)).ConfigureAwait(false); - _handShakeSuccessful = await HandShakeWithVsTestConsoleAsync().ConfigureAwait(false); - _handShakeComplete.Set(); + await HandShakeWithVsTestConsoleAsync().ConfigureAwait(false); + + _handShakeSuccessful = true; } - catch (Exception ex) + finally { - EqtTrace.Error( - "VsTestConsoleRequestSender.InitializeCommunicationAsync: Error initializing communication with VstestConsole: {0}", - ex); _handShakeComplete.Set(); } - - EqtTrace.Info("VsTestConsoleRequestSender.InitializeCommunicationAsync: Ended."); - - return _handShakeSuccessful ? port : -1; } /// diff --git a/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleWrapper.cs b/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleWrapper.cs index e3b99521c7..4e5c77ccb7 100644 --- a/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleWrapper.cs +++ b/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleWrapper.cs @@ -659,7 +659,7 @@ public async Task StartSessionAsync() var timeout = EnvironmentHelper.GetConnectionTimeout(); // Start communication - var port = await _requestSender.InitializeCommunicationAsync(timeout * 1000).ConfigureAwait(false); + var port = _requestSender.StartServer(); if (port > 0) { @@ -681,6 +681,8 @@ public async Task StartSessionAsync() _requestSender.Close(); throw new TransationLayerException("Error hosting communication channel and connecting to console"); } + + await _requestSender.InitializeCommunicationAsync(timeout * 1000).ConfigureAwait(false); } ///