Repository navigation
Expand file tree
/
Copy pathClassCleanupManager.cs
More file actions
91 lines (76 loc) · 3.89 KB
/
Copy pathClassCleanupManager.cs
File metadata and controls
91 lines (76 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface;
using AdapterApplicationStateGuard = Microsoft.VisualStudio.TestPlatform.MSTestAdapter.ApplicationStateGuard;
namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution;
internal sealed class ClassCleanupManager
{
private readonly ConcurrentDictionary<string, int> _remainingTestCountsByClass;
public ClassCleanupManager(IEnumerable<UnitTestElement> testsToRun)
{
_remainingTestCountsByClass =
new(testsToRun.GroupBy(t => t.TestMethod.FullClassName)
.ToDictionary(
g => g.Key,
g => g.Count()));
}
public bool ShouldRunEndOfAssemblyCleanup => _remainingTestCountsByClass.IsEmpty;
public void MarkTestComplete(TestMethod testMethod, out bool isLastTestInClass)
{
lock (_remainingTestCountsByClass)
{
if (!_remainingTestCountsByClass.TryGetValue(testMethod.FullClassName, out int remainingCount))
{
throw AdapterApplicationStateGuard.Unreachable();
}
remainingCount--;
_remainingTestCountsByClass[testMethod.FullClassName] = remainingCount;
isLastTestInClass = remainingCount == 0;
}
}
public void MarkClassComplete(string fullClassName)
{
lock (_remainingTestCountsByClass)
{
if (!_remainingTestCountsByClass.TryRemove(fullClassName, out int remainingTests) ||
remainingTests != 0)
{
// We failed to remove the class, or we are incorrectly marking the class as complete while there are remaining tests.
// This should never happen.
throw AdapterApplicationStateGuard.Unreachable();
}
}
}
internal static void ForceCleanup(TypeCache typeCache, IDictionary<string, object?> sourceLevelParameters, IAdapterMessageLogger logger)
{
IEnumerable<TestClassInfo> classInfoCache = typeCache.ClassInfoListWithExecutableCleanupMethods;
foreach (TestClassInfo classInfo in classInfoCache)
{
var testContext = new TestContextImplementation(null, classInfo.ClassType.FullName, sourceLevelParameters, logger, testRunCancellationToken: null);
// Flow properties set during AssemblyInitialize and ClassInitialize so the
// ClassCleanup method observes them when invoked via the fallback path.
testContext.MergeProperties(classInfo.Parent.PostAssemblyInitProperties);
testContext.MergeProperties(classInfo.PostClassInitProperties);
TestFailedException? ex = classInfo.ExecuteClassCleanupAsync(testContext).GetAwaiter().GetResult();
if (ex is not null)
{
throw ex;
}
}
IEnumerable<TestAssemblyInfo> assemblyInfoCache = typeCache.AssemblyInfoListWithExecutableCleanupMethods;
foreach (TestAssemblyInfo assemblyInfo in assemblyInfoCache)
{
var testContext = new TestContextImplementation(null, null, sourceLevelParameters, logger, testRunCancellationToken: null);
// Flow properties set during AssemblyInitialize so the AssemblyCleanup method observes
// them when invoked via the fallback path.
testContext.MergeProperties(assemblyInfo.PostAssemblyInitProperties);
TestFailedException? ex = assemblyInfo.ExecuteAssemblyCleanupAsync(testContext).GetAwaiter().GetResult();
if (ex is not null)
{
throw ex;
}
}
}
}