Skip to content

Commit

Permalink
make test run result collection thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Willich committed Sep 6, 2019
1 parent 8f88335 commit bfcd509
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions TechTalk.SpecFlow/CucumberMessages/TestRunResultCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class TestRunResultCollector : ITestRunResultCollector
private readonly IDictionary<ScenarioInfo, TestResult> _collectedResults = new Dictionary<ScenarioInfo, TestResult>();
public bool IsStarted { get; private set; }

private readonly object _lock = new object();

public void StartCollecting()
{
if (IsStarted)
Expand All @@ -28,7 +30,11 @@ public void CollectTestResultForScenario(ScenarioInfo scenarioInfo, TestResult t
throw new InvalidOperationException("Result collection has not been started.");
}

_collectedResults.Add(scenarioInfo, testResult);

lock (_lock)
{
_collectedResults.Add(scenarioInfo, testResult);
}
}

public IResult<TestRunResult> GetCurrentResult()
Expand All @@ -39,8 +45,12 @@ public IResult<TestRunResult> GetCurrentResult()
}


var groups = _collectedResults.GroupBy(kv => kv.Value.Status, kv => (kv.Key, kv.Value))
.ToArray();
IGrouping<TestResult.Types.Status, (ScenarioInfo Key, TestResult Value)>[] groups;
lock (_lock)
{
groups = _collectedResults.GroupBy(kv => kv.Value.Status, kv => (kv.Key, kv.Value))
.ToArray();
}

var passedCount = groups.SingleOrDefault(g => g.Key == TestResult.Types.Status.Passed)?.Count() ?? 0;
var failedCount = groups.SingleOrDefault(g => g.Key == TestResult.Types.Status.Failed)?.Count() ?? 0;
Expand Down

0 comments on commit bfcd509

Please sign in to comment.