From 4f7d337742d1cdb56eb8c6125a4153914688f7ea Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Sat, 11 Jan 2020 15:23:33 -0800 Subject: [PATCH 1/4] Make the diagnostics client library tests not sleep --- .../TestRunner.cs | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/tests/Microsoft.Diagnostics.NETCore.Client/TestRunner.cs b/src/tests/Microsoft.Diagnostics.NETCore.Client/TestRunner.cs index 420bde839b..f1b5873b16 100644 --- a/src/tests/Microsoft.Diagnostics.NETCore.Client/TestRunner.cs +++ b/src/tests/Microsoft.Diagnostics.NETCore.Client/TestRunner.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Linq; using System.Threading; using Xunit; using Xunit.Abstractions; @@ -12,6 +13,9 @@ using Microsoft.Diagnostics.TestHelpers; using Microsoft.Diagnostics.NETCore.Client; +using System.Threading.Tasks; +using System.Runtime.InteropServices; +using System.IO; namespace Microsoft.Diagnostics.NETCore.Client { @@ -57,9 +61,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; + } + } + Thread.Sleep(100); + } + }); + + monitorSocketTask.Wait(TimeSpan.FromMilliseconds(timeoutInMS)); } public void Stop() From 7ebd84d0d6bd3e55972dbd05625afc5879c19644 Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Sat, 11 Jan 2020 15:24:39 -0800 Subject: [PATCH 2/4] Remove thread.sleep --- .../EventPipeSessionTests.cs | 2 +- .../GetPublishedProcessesTests.cs | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) 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..9b2f08d15d 100644 --- a/src/tests/Microsoft.Diagnostics.NETCore.Client/GetPublishedProcessesTests.cs +++ b/src/tests/Microsoft.Diagnostics.NETCore.Client/GetPublishedProcessesTests.cs @@ -48,7 +48,6 @@ public void MultiplePublishedProcessTest() runner[i].Start(); pids[i] = runner[i].Pid; } - System.Threading.Thread.Sleep(2000); List publishedProcesses = new List(DiagnosticsClient.GetPublishedProcesses()); foreach (int p in publishedProcesses) { From 2af337390f7ba264a12f008848a668d6acc0cc28 Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Mon, 13 Jan 2020 16:27:44 -0800 Subject: [PATCH 3/4] task.delay instead of thread.sleep --- .../TestRunner.cs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/tests/Microsoft.Diagnostics.NETCore.Client/TestRunner.cs b/src/tests/Microsoft.Diagnostics.NETCore.Client/TestRunner.cs index f1b5873b16..653d4d8bdb 100644 --- a/src/tests/Microsoft.Diagnostics.NETCore.Client/TestRunner.cs +++ b/src/tests/Microsoft.Diagnostics.NETCore.Client/TestRunner.cs @@ -2,21 +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; -using System.Threading.Tasks; -using System.Runtime.InteropServices; -using System.IO; - namespace Microsoft.Diagnostics.NETCore.Client { public class TestRunner @@ -38,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); @@ -80,7 +79,7 @@ public void Start(int timeoutInMS=0) break; } } - Thread.Sleep(100); + Task.Delay(100); } }); From bd6918d4fac6bda8429e16d27cf9ad4d40397597 Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Wed, 15 Jan 2020 08:36:16 -0800 Subject: [PATCH 4/4] Fix failing test --- .../GetPublishedProcessesTests.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/tests/Microsoft.Diagnostics.NETCore.Client/GetPublishedProcessesTests.cs b/src/tests/Microsoft.Diagnostics.NETCore.Client/GetPublishedProcessesTests.cs index 9b2f08d15d..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,6 +57,10 @@ public void MultiplePublishedProcessTest() runner[i].Start(); pids[i] = runner[i].Pid; } + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + Thread.Sleep(5000); + } List publishedProcesses = new List(DiagnosticsClient.GetPublishedProcesses()); foreach (int p in publishedProcesses) {