diff --git a/src/tests/Microsoft.Diagnostics.NETCore.Client/EventPipeSessionTests.cs b/src/tests/Microsoft.Diagnostics.NETCore.Client/EventPipeSessionTests.cs index 6344f47491..011e507be8 100644 --- a/src/tests/Microsoft.Diagnostics.NETCore.Client/EventPipeSessionTests.cs +++ b/src/tests/Microsoft.Diagnostics.NETCore.Client/EventPipeSessionTests.cs @@ -55,7 +55,7 @@ public void BasicEventPipeSessionTest() public void EventPipeSessionStreamTest() { TestRunner runner = new TestRunner(CommonHelper.GetTraceePath(), output); - runner.Start(5000); + runner.Start(3000); DiagnosticsClient client = new DiagnosticsClient(runner.Pid); runner.PrintStatus(); output.WriteLine($"[{DateTime.Now.ToString()}] Trying to start an EventPipe session on process {runner.Pid}"); diff --git a/src/tests/Microsoft.Diagnostics.NETCore.Client/GetPublishedProcessesTests.cs b/src/tests/Microsoft.Diagnostics.NETCore.Client/GetPublishedProcessesTests.cs index e2945f73a3..d108b64fac 100644 --- a/src/tests/Microsoft.Diagnostics.NETCore.Client/GetPublishedProcessesTests.cs +++ b/src/tests/Microsoft.Diagnostics.NETCore.Client/GetPublishedProcessesTests.cs @@ -4,6 +4,8 @@ using System; using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Threading; using Xunit; using Xunit.Abstractions; @@ -27,6 +29,13 @@ public void PublishedProcessTest1() { TestRunner runner = new TestRunner(CommonHelper.GetTraceePath(), output); runner.Start(3000); + // On Windows, runner.Start will not wait for named pipe creation since for other tests, NamedPipeClientStream will + // just wait until the named pipe is created. + // For these tests, we need to sleep an arbitrary time before pipe is created. + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + Thread.Sleep(5000); + } List publishedProcesses = new List(DiagnosticsClient.GetPublishedProcesses()); foreach (int p in publishedProcesses) { @@ -48,7 +57,10 @@ public void MultiplePublishedProcessTest() runner[i].Start(); pids[i] = runner[i].Pid; } - System.Threading.Thread.Sleep(2000); + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + Thread.Sleep(5000); + } List publishedProcesses = new List(DiagnosticsClient.GetPublishedProcesses()); foreach (int p in publishedProcesses) { diff --git a/src/tests/Microsoft.Diagnostics.NETCore.Client/TestRunner.cs b/src/tests/Microsoft.Diagnostics.NETCore.Client/TestRunner.cs index 420bde839b..653d4d8bdb 100644 --- a/src/tests/Microsoft.Diagnostics.NETCore.Client/TestRunner.cs +++ b/src/tests/Microsoft.Diagnostics.NETCore.Client/TestRunner.cs @@ -2,17 +2,20 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. + +using Microsoft.Diagnostics.NETCore.Client; +using Microsoft.Diagnostics.TestHelpers; using System; using System.Collections.Generic; using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; using System.Threading; +using System.Threading.Tasks; using Xunit; using Xunit.Abstractions; -using Microsoft.Diagnostics.TestHelpers; - -using Microsoft.Diagnostics.NETCore.Client; - namespace Microsoft.Diagnostics.NETCore.Client { public class TestRunner @@ -34,7 +37,7 @@ public void AddEnvVar(string key, string value) startInfo.EnvironmentVariables[key] = value; } - public void Start(int timeoutInMS=0) + public void Start(int timeoutInMS=15000) { if (outputHelper != null) outputHelper.WriteLine("$[{DateTime.Now.ToString()}] Launching test: " + startInfo.FileName); @@ -57,9 +60,30 @@ public void Start(int timeoutInMS=0) outputHelper.WriteLine($"Have total {testProcess.Modules.Count} modules loaded"); } - outputHelper.WriteLine($"[{DateTime.Now.ToString()}] Sleeping for {timeoutInMS} ms."); - Thread.Sleep(timeoutInMS); - outputHelper.WriteLine($"[{DateTime.Now.ToString()}] Done sleeping. Ready to test."); + // Block until we see the IPC channel created, or until timeout specified. + Task monitorSocketTask = Task.Run(() => + { + while (true) + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + // On Windows, namedpipe connection will block until the named pipe is ready to connect so no need to block here + break; + } + else + { + // On Linux, we wait until the socket is created. + var matchingFiles = Directory.GetFiles(Path.GetTempPath(), $"dotnet-diagnostic-{testProcess.Id}-*-socket"); // Try best match. + if (matchingFiles.Length > 0) + { + break; + } + } + Task.Delay(100); + } + }); + + monitorSocketTask.Wait(TimeSpan.FromMilliseconds(timeoutInMS)); } public void Stop()