Skip to content

Commit

Permalink
Fix parallel output isolation (#1705)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evangelink authored Jul 10, 2023
1 parent c570d67 commit 14b6245
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -271,7 +272,9 @@ private bool DynamicDataAttached(IDictionary<string, object?> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public class TestContextImplementation : TestContext, ITestContext
/// <summary>
/// Writer on which the messages given by the user should be written.
/// </summary>
private readonly ThreadSafeStringWriter _threadSafeStringWriter;
private readonly StringWriter _stringWriter;
private readonly ThreadSafeStringWriter? _threadSafeStringWriter;

/// <summary>
/// Test Method.
Expand Down Expand Up @@ -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<string, object?>(properties)
{
[FullyQualifiedTestClassNameLabel] = _testMethod.FullClassName,
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -334,15 +334,15 @@ public void AddProperty(string propertyName, string propertyValue)
/// <returns>The test context messages added so far.</returns>
public string? GetDiagnosticMessages()
{
return _threadSafeStringWriter.ToString();
return _stringWriter.ToString();
}

/// <summary>
/// Clears the previous testContext writeline messages.
/// </summary>
public void ClearDiagnosticMessages()
{
_threadSafeStringWriter.ToStringAndClear();
_threadSafeStringWriter?.ToStringAndClear();
}

#endregion
Expand Down

0 comments on commit 14b6245

Please sign in to comment.