From 14b62451fb1754035925749ad0c495f587be5e58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Mon, 10 Jul 2023 12:21:42 +0200 Subject: [PATCH] Fix parallel output isolation (#1705) --- .../Discovery/AssemblyEnumerator.cs | 5 ++++- .../Services/TestContextImplementation.cs | 20 +++++++++---------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Adapter/MSTest.TestAdapter/Discovery/AssemblyEnumerator.cs b/src/Adapter/MSTest.TestAdapter/Discovery/AssemblyEnumerator.cs index 2cc819f77e..d3234616de 100644 --- a/src/Adapter/MSTest.TestAdapter/Discovery/AssemblyEnumerator.cs +++ b/src/Adapter/MSTest.TestAdapter/Discovery/AssemblyEnumerator.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.IO; using System.Linq; using System.Reflection; using System.Runtime.Serialization; @@ -271,7 +272,9 @@ private bool DynamicDataAttached(IDictionary sourceLevelParamet return false; } - using var writer = new ThreadSafeStringWriter(CultureInfo.InvariantCulture, "all"); + // NOTE: From this place we don't have any path that would let the user write a message on the TestContext and we don't do + // anything with what would be printed anyway so we can simply use a simple StringWriter. + using var writer = new StringWriter(); var testMethod = test.TestMethod; var testContext = PlatformServiceProvider.Instance.GetTestContext(testMethod, writer, sourceLevelParameters); var testMethodInfo = _typeCache.GetTestMethodInfo(testMethod, testContext, MSTestSettings.CurrentSettings.CaptureDebugTraces); diff --git a/src/Adapter/MSTestAdapter.PlatformServices/Services/TestContextImplementation.cs b/src/Adapter/MSTestAdapter.PlatformServices/Services/TestContextImplementation.cs index 94c2e8d30c..43471b5c9e 100644 --- a/src/Adapter/MSTestAdapter.PlatformServices/Services/TestContextImplementation.cs +++ b/src/Adapter/MSTestAdapter.PlatformServices/Services/TestContextImplementation.cs @@ -32,7 +32,8 @@ public class TestContextImplementation : TestContext, ITestContext /// /// Writer on which the messages given by the user should be written. /// - private readonly ThreadSafeStringWriter _threadSafeStringWriter; + private readonly StringWriter _stringWriter; + private readonly ThreadSafeStringWriter? _threadSafeStringWriter; /// /// Test Method. @@ -81,12 +82,11 @@ public TestContextImplementation(ITestMethod testMethod, StringWriter stringWrit DebugEx.Assert(stringWriter != null, "StringWriter is not null"); #endif - DebugEx.Assert(stringWriter is ThreadSafeStringWriter, "Was expected stringWriter to be a ThreadSafeStringWriter"); - _testMethod = testMethod; + _stringWriter = stringWriter; // Cannot get this type in constructor directly, because all signatures for all platforms need to be the same. - _threadSafeStringWriter = (ThreadSafeStringWriter)stringWriter; + _threadSafeStringWriter = stringWriter as ThreadSafeStringWriter; _properties = new Dictionary(properties) { [FullyQualifiedTestClassNameLabel] = _testMethod.FullClassName, @@ -172,7 +172,7 @@ public override void Write(string? message) try { var msg = message?.Replace("\0", "\\0"); - _threadSafeStringWriter.Write(msg); + _stringWriter.Write(msg); } catch (ObjectDisposedException) { @@ -196,7 +196,7 @@ public override void Write(string format, params object?[] args) try { string message = string.Format(CultureInfo.CurrentCulture, format.Replace("\0", "\\0"), args); - _threadSafeStringWriter.Write(message); + _stringWriter.Write(message); } catch (ObjectDisposedException) { @@ -219,7 +219,7 @@ public override void WriteLine(string? message) try { var msg = message?.Replace("\0", "\\0"); - _threadSafeStringWriter.WriteLine(msg); + _stringWriter.WriteLine(msg); } catch (ObjectDisposedException) { @@ -243,7 +243,7 @@ public override void WriteLine(string format, params object?[] args) try { string message = string.Format(CultureInfo.CurrentCulture, format.Replace("\0", "\\0"), args); - _threadSafeStringWriter.WriteLine(message); + _stringWriter.WriteLine(message); } catch (ObjectDisposedException) { @@ -334,7 +334,7 @@ public void AddProperty(string propertyName, string propertyValue) /// The test context messages added so far. public string? GetDiagnosticMessages() { - return _threadSafeStringWriter.ToString(); + return _stringWriter.ToString(); } /// @@ -342,7 +342,7 @@ public void AddProperty(string propertyName, string propertyValue) /// public void ClearDiagnosticMessages() { - _threadSafeStringWriter.ToStringAndClear(); + _threadSafeStringWriter?.ToStringAndClear(); } #endregion