Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some race condition issue #1477

Merged
merged 1 commit into from
Dec 19, 2022
Merged
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
26 changes: 17 additions & 9 deletions src/Adapter/MSTest.TestAdapter/Execution/UnitTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
Expand Down Expand Up @@ -288,33 +289,40 @@ private class ClassCleanupManager
private readonly ClassCleanupBehavior? _lifecycleFromMsTest;
private readonly ClassCleanupBehavior _lifecycleFromAssembly;
private readonly ReflectHelper _reflectHelper;
private readonly Dictionary<string, HashSet<string>> _remainingTestsByClass;
private readonly ConcurrentDictionary<string, HashSet<string>> _remainingTestsByClass;

public ClassCleanupManager(
IEnumerable<UnitTestElement> testsToRun,
ClassCleanupBehavior? lifecycleFromMsTest,
ClassCleanupBehavior lifecycleFromAssembly,
ReflectHelper? reflectHelper = null)
{
_remainingTestsByClass = testsToRun.GroupBy(t => t.TestMethod.FullClassName)
.ToDictionary(
g => g.Key,
g => new HashSet<string>(g.Select(t => t.TestMethod.UniqueName)));
_remainingTestsByClass =
new(testsToRun.GroupBy(t => t.TestMethod.FullClassName)
.ToDictionary(
g => g.Key,
g => new HashSet<string>(g.Select(t => t.TestMethod.UniqueName))));
_lifecycleFromMsTest = lifecycleFromMsTest;
_lifecycleFromAssembly = lifecycleFromAssembly;
_reflectHelper = reflectHelper ?? new ReflectHelper();
}

public void MarkTestComplete(TestMethodInfo testMethodInfo, TestMethod testMethod, out bool shouldRunEndOfClassCleanup, out bool shouldRunEndOfAssemblyCleanup)
public void MarkTestComplete(TestMethodInfo testMethodInfo, TestMethod testMethod, out bool shouldRunEndOfClassCleanup,
out bool shouldRunEndOfAssemblyCleanup)
{
shouldRunEndOfClassCleanup = false;
var testsByClass = _remainingTestsByClass[testMethodInfo.TestClassName];
shouldRunEndOfAssemblyCleanup = false;
if (!_remainingTestsByClass.TryGetValue(testMethodInfo.TestClassName, out var testsByClass))
Evangelink marked this conversation as resolved.
Show resolved Hide resolved
{
return;
}

lock (testsByClass)
{
testsByClass.Remove(testMethod.UniqueName);
if (testsByClass.Count == 0)
{
_remainingTestsByClass.Remove(testMethodInfo.TestClassName);
_remainingTestsByClass.TryRemove(testMethodInfo.TestClassName, out _);
if (testMethodInfo.Parent.HasExecutableCleanupMethod)
{
var cleanupLifecycle = _reflectHelper.GetClassCleanupBehavior(testMethodInfo.Parent)
Expand All @@ -325,7 +333,7 @@ public void MarkTestComplete(TestMethodInfo testMethodInfo, TestMethod testMetho
}
}

shouldRunEndOfAssemblyCleanup = _remainingTestsByClass.Count == 0;
shouldRunEndOfAssemblyCleanup = _remainingTestsByClass.IsEmpty;
}
}
}
Expand Down