Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using GoogleTestAdapter.DiaResolver;
using GoogleTestAdapter.Model;
Expand Down Expand Up @@ -395,6 +396,73 @@ public void OutputHandling_OneLine_IsParsedCorrectly()
testResult.ErrorMessage.Should().Be(expectedErrorMessage);
}

[TestMethod]
[TestCategory(Unit)]
public void ReportLine_ConcurrentFlush_DoesNotOvertakeIncompleteStateTransition()
{
const int timeoutMilliseconds = 5000;
var cases = new List<TestCase>
{
TestDataCreator.ToTestCase("Test.First", TestDataCreator.DummyExecutable, ""),
TestDataCreator.ToTestCase("Test.Second", TestDataCreator.DummyExecutable, "")
};
var reportedResults = new List<TestResult>();

using (var reportLinePaused = new ManualResetEventSlim())
using (var releaseReportLine = new ManualResetEventSlim())
using (var flushStarted = new ManualResetEventSlim())
{
MockFrameworkReporter
.Setup(r => r.ReportTestResults(It.IsAny<IEnumerable<TestResult>>()))
.Callback<IEnumerable<TestResult>>(results => reportedResults.AddRange(results));
MockFrameworkReporter
.Setup(r => r.ReportTestsStarted(
It.Is<IEnumerable<TestCase>>(testCases =>
testCases.Single().FullyQualifiedName == "Test.Second")))
.Callback(() =>
{
reportLinePaused.Set();
releaseReportLine.Wait(timeoutMilliseconds).Should().BeTrue();
});

var parser = new StreamingStandardOutputTestResultParser(
cases, MockLogger.Object, MockFrameworkReporter.Object, String.Empty);
parser.ReportLine("[ RUN ] Test.First");
parser.ReportLine("[ OK ] Test.First (1 ms)");

Task reportLineTask = Task.Run(() =>
parser.ReportLine("[ RUN ] Test.Second[ OK ] Test.Second (2 ms)"));
reportLinePaused.Wait(timeoutMilliseconds).Should().BeTrue();

Task flushTask = Task.Run(() =>
{
flushStarted.Set();
parser.Flush();
});
flushStarted.Wait(timeoutMilliseconds).Should().BeTrue();

bool flushCompletedBeforeRelease;
try
{
flushCompletedBeforeRelease = flushTask.Wait(500);
}
finally
{
releaseReportLine.Set();
}

Task.WaitAll(new[] { reportLineTask, flushTask }, timeoutMilliseconds).Should().BeTrue();

flushCompletedBeforeRelease.Should().BeFalse();
reportedResults.Select(result => result.TestCase.FullyQualifiedName)
.Should().Equal("Test.First", "Test.Second");
parser.TestResults.Select(result => result.TestCase.FullyQualifiedName)
.Should().Equal("Test.First", "Test.Second");
reportedResults.Should().OnlyContain(result => result.ErrorMessage == null);
parser.CrashedTestCase.Should().BeNull();
}
}

private IList<TestResult> GetTestResultsFromCompleteOutputFile()
{
var testCases = new GoogleTestDiscoverer(MockLogger.Object, MockOptions.Object, new DefaultDiaResolverFactory())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class StreamingStandardOutputTestResultParser
private readonly ILogger _logger;
private readonly ITestFrameworkReporter _reporter;

private readonly object _syncObject = new object();
private readonly List<string> _consoleOutput = new List<string>();
private readonly string _executable;

Expand Down Expand Up @@ -58,19 +59,22 @@ public List<TestResult> GetXMLResults(IEnumerable<TestCase> testCasesRun)

public void ReportLine(string line)
{
Match testEndMatch = PrefixedLineRegex.Match(line);
if (testEndMatch.Success)
lock (_syncObject)
{
string restOfErrorMessage = testEndMatch.Groups[1].Value;
if (!string.IsNullOrEmpty(restOfErrorMessage))
DoReportLine(restOfErrorMessage);
Match testEndMatch = PrefixedLineRegex.Match(line);
if (testEndMatch.Success)
{
string restOfErrorMessage = testEndMatch.Groups[1].Value;
if (!string.IsNullOrEmpty(restOfErrorMessage))
DoReportLine(restOfErrorMessage);

string testEndPart = testEndMatch.Groups[2].Value;
DoReportLine(testEndPart);
}
else
{
DoReportLine(line);
string testEndPart = testEndMatch.Groups[2].Value;
DoReportLine(testEndPart);
}
else
{
DoReportLine(line);
}
}
}

Expand All @@ -95,10 +99,13 @@ private void DoReportLine(string line)

public void Flush()
{
if (_consoleOutput.Count > 0)
lock (_syncObject)
{
ReportTestResult();
_consoleOutput.Clear();
if (_consoleOutput.Count > 0)
{
ReportTestResult();
_consoleOutput.Clear();
}
}
}

Expand Down