diff --git a/src/tests/profiler/common/ProfilerTestRunner.cs b/src/tests/profiler/common/ProfilerTestRunner.cs index 2dfc2530c59d6..e95817116c90e 100644 --- a/src/tests/profiler/common/ProfilerTestRunner.cs +++ b/src/tests/profiler/common/ProfilerTestRunner.cs @@ -52,7 +52,7 @@ public static int Run(string profileePath, if (loadAsNotification) { StringBuilder builder = new StringBuilder(); - for(int i = 0; i < notificationCopies; ++i) + for (int i = 0; i < notificationCopies; ++i) { builder.Append(profilerPath); builder.Append("="); @@ -94,13 +94,11 @@ public static int Run(string profileePath, envVars.Add("Profiler_Test_Name", testName); - if(!File.Exists(profilerPath)) + if (!File.Exists(profilerPath)) { FailFastWithMessage("Profiler library not found at expected path: " + profilerPath); } - ProfileeOutputVerifier verifier = new ProfileeOutputVerifier(); - Process process = new Process(); process.StartInfo.FileName = program; process.StartInfo.Arguments = arguments; @@ -117,15 +115,10 @@ public static int Run(string profileePath, process.StartInfo.EnvironmentVariables[key] = envVars[key]; } - process.OutputDataReceived += (sender, args) => - { - Console.WriteLine(args.Data); - verifier.WriteLine(args.Data); - }; process.Start(); + ProfileeOutputVerifier verifier = new ProfileeOutputVerifier(process.StandardOutput); - process.BeginOutputReadLine(); - + verifier.VerifyOutput(); process.WaitForExit(); // There are two conditions for profiler tests to pass, the output of the profiled program @@ -198,21 +191,26 @@ class ProfileeOutputVerifier private volatile bool _hasPassingOutput; public string SuccessPhrase = "PROFILER TEST PASSES"; - public bool HasPassingOutput => _hasPassingOutput; + private StreamReader standardOutput; - public void WriteLine(string message) + public ProfileeOutputVerifier(StreamReader standardOutput) { - if (message != null && message.Contains(SuccessPhrase)) - { - _hasPassingOutput = true; - } + this.standardOutput = standardOutput; } - public void WriteLine(string format, params object[] args) + public bool HasPassingOutput => _hasPassingOutput; + + internal void VerifyOutput() { - if (string.Format(format,args).Contains(SuccessPhrase)) + string line; + while ((line = standardOutput.ReadLine()) != null) { - _hasPassingOutput = true; + if (line.Contains(SuccessPhrase)) + { + _hasPassingOutput = true; + } + + Console.WriteLine($"Profilee STDOUT: {line}"); } } } diff --git a/src/tests/profiler/multiple/multiple.cs b/src/tests/profiler/multiple/multiple.cs index a0952687021bf..7f2a5a5494929 100644 --- a/src/tests/profiler/multiple/multiple.cs +++ b/src/tests/profiler/multiple/multiple.cs @@ -10,16 +10,17 @@ namespace Profiler.Tests { class MultiplyLoaded { - static readonly Guid MultipleProfilerGuid = new Guid("BFA8EF13-E144-49B9-B95C-FC1C150C7651"); - static readonly string ProfilerPath = ProfilerTestRunner.GetProfilerPath(); + private static readonly Guid MultipleProfilerGuid = new Guid("BFA8EF13-E144-49B9-B95C-FC1C150C7651"); + private static readonly string ProfilerPath = ProfilerTestRunner.GetProfilerPath(); [DllImport("Profiler")] private static extern void PassCallbackToProfiler(ProfilerCallback callback); public static int RunTest(String[] args) { - ManualResetEvent _profilerDone = new ManualResetEvent(false); - PassCallbackToProfiler(() => _profilerDone.Set()); + ManualResetEvent profilerDone = new ManualResetEvent(false); + ProfilerCallback profilerDoneDelegate = () => profilerDone.Set(); + PassCallbackToProfiler(profilerDoneDelegate); ProfilerControlHelpers.AttachProfilerToSelf(MultipleProfilerGuid, ProfilerPath); @@ -35,11 +36,12 @@ public static int RunTest(String[] args) } Console.WriteLine("Waiting for profilers to all detach"); - if (!_profilerDone.WaitOne(TimeSpan.FromMinutes(5))) + if (!profilerDone.WaitOne(TimeSpan.FromMinutes(5))) { throw new Exception("Test timed out waiting for the profilers to set the callback, test will fail."); } + GC.KeepAlive(profilerDoneDelegate); return 100; } diff --git a/src/tests/profiler/native/multiple/multiple.cpp b/src/tests/profiler/native/multiple/multiple.cpp index c36e4a8efa491..054cb3e1e4265 100644 --- a/src/tests/profiler/native/multiple/multiple.cpp +++ b/src/tests/profiler/native/multiple/multiple.cpp @@ -57,11 +57,14 @@ HRESULT MultiplyLoaded::ProfilerDetachSucceeded() ++_detachCount; printf("ProfilerDetachSucceeded _detachCount=%d\n", _detachCount.load()); - if (_detachCount == MAX_PROFILERS - && _exceptionThrownSeenCount >= MAX_PROFILERS - && _failures == 0) + if (_detachCount == MAX_PROFILERS) { - printf("PROFILER TEST PASSES\n"); + if (_exceptionThrownSeenCount >= MAX_PROFILERS && _failures == 0) + { + printf("PROFILER TEST PASSES\n"); + fflush(stdout); + } + NotifyManagedCodeViaCallback(pCorProfilerInfo); } diff --git a/src/tests/profiler/unittest/releaseondetach.cs b/src/tests/profiler/unittest/releaseondetach.cs index e50190b1872df..0e5ec490984cc 100644 --- a/src/tests/profiler/unittest/releaseondetach.cs +++ b/src/tests/profiler/unittest/releaseondetach.cs @@ -15,7 +15,7 @@ class ReleaseOnShutdown [DllImport("Profiler")] private static extern void PassCallbackToProfiler(ProfilerCallback callback); - + public unsafe static int RunTest(string[] args) { string profilerName; @@ -35,16 +35,18 @@ public unsafe static int RunTest(string[] args) string rootPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); string profilerPath = Path.Combine(rootPath, profilerName); - ManualResetEvent _profilerDone = new ManualResetEvent(false); Console.WriteLine($"Attaching profiler {profilerPath} to self."); ProfilerControlHelpers.AttachProfilerToSelf(ReleaseOnShutdownGuid, profilerPath); - PassCallbackToProfiler(() => _profilerDone.Set()); - if (!_profilerDone.WaitOne(TimeSpan.FromMinutes(5))) + ManualResetEvent profilerDone = new ManualResetEvent(false); + ProfilerCallback profilerDoneDelegate = () => profilerDone.Set(); + PassCallbackToProfiler(profilerDoneDelegate); + if (!profilerDone.WaitOne(TimeSpan.FromMinutes(5))) { Console.WriteLine("Profiler did not set the callback, test will fail."); } + GC.KeepAlive(profilerDoneDelegate); return 100; }