Repository navigation
Expand file tree
/
Copy pathTestApplicationActionQueue.cs
More file actions
273 lines (246 loc) · 11.1 KB
/
Copy pathTestApplicationActionQueue.cs
File metadata and controls
273 lines (246 loc) · 11.1 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Threading.Channels;
using Microsoft.DotNet.Cli.Commands.Test.IPC.Models;
using Microsoft.DotNet.Cli.Commands.Test.Terminal;
using Microsoft.DotNet.Cli.Utils;
namespace Microsoft.DotNet.Cli.Commands.Test;
internal class TestApplicationActionQueue
{
private readonly Channel<ParallelizableTestModuleGroupWithSequentialInnerModules> _channel;
private readonly Task[] _readers;
private readonly CancellationToken _cancellationToken;
private readonly TestApplicationSettings _fallbackTestApplicationSettings;
private readonly BuildOptions _buildOptions;
private readonly OutputOptions _outputOptions;
private readonly bool _outputOptionSpecified;
private int? _aggregateExitCode;
private readonly List<TestApplicationPolicy> _effectivePolicies = [];
private readonly Dictionary<TestModule, TestApplicationSettings> _effectiveSettings = [];
private readonly Lock _lock = new();
public TestApplicationActionQueue(
int degreeOfParallelism,
BuildOptions buildOptions,
TestOptions testOptions,
TestResultsDirectoryResolver resultsDirectoryResolver,
TerminalTestReporter output,
Action<CommandLineOptionMessages> onHelpRequested,
CtrlCCancellationManager ctrlC,
ArtifactPostProcessingManager artifactPostProcessingManager,
TestRunPolicy testRunPolicy,
CancellationToken cancellationToken,
OutputOptions outputOptions,
bool outputOptionSpecified,
TestApplicationSettings fallbackTestApplicationSettings)
{
_channel = Channel.CreateUnbounded<ParallelizableTestModuleGroupWithSequentialInnerModules>(new UnboundedChannelOptions { SingleReader = false, SingleWriter = false });
_readers = new Task[degreeOfParallelism];
_cancellationToken = cancellationToken;
_fallbackTestApplicationSettings = fallbackTestApplicationSettings;
_buildOptions = buildOptions;
_outputOptions = outputOptions;
_outputOptionSpecified = outputOptionSpecified;
for (int i = 0; i < degreeOfParallelism; i++)
{
_readers[i] = Task.Run(async () => await Read(
buildOptions,
testOptions,
resultsDirectoryResolver,
output,
onHelpRequested,
ctrlC,
artifactPostProcessingManager,
testRunPolicy));
}
}
public void Enqueue(ParallelizableTestModuleGroupWithSequentialInnerModules testApplication)
{
lock (_lock)
{
foreach (TestModule module in testApplication)
{
TestApplicationSettings settings =
MicrosoftTestingPlatformTestCommand.GetEffectiveTestApplicationSettings(
module,
_buildOptions,
_outputOptions,
_outputOptionSpecified);
_effectiveSettings[module] = settings;
_effectivePolicies.Add(settings.Policy);
}
}
if (_cancellationToken.IsCancellationRequested)
{
return;
}
if (!_channel.Writer.TryWrite(testApplication))
{
throw new InvalidOperationException($"Failed to write to channel for test application: {testApplication}");
}
}
public int CompleteEnqueueAndWait()
{
// Notify readers that no more data will be written
_channel.Writer.Complete();
Task.WaitAll(_readers);
// If _aggregateExitCode is null, that means we didn't get any results.
// So, we exit with "zero tests".
return _aggregateExitCode ?? ExitCode.ZeroTests;
}
private async Task Read(
BuildOptions buildOptions,
TestOptions testOptions,
TestResultsDirectoryResolver resultsDirectoryResolver,
TerminalTestReporter output,
Action<CommandLineOptionMessages> onHelpRequested,
CtrlCCancellationManager ctrlC,
ArtifactPostProcessingManager artifactPostProcessingManager,
TestRunPolicy testRunPolicy)
{
try
{
await foreach (var nonParallelizedGroup in _channel.Reader.ReadAllAsync(_cancellationToken))
{
foreach (var module in nonParallelizedGroup)
{
_cancellationToken.ThrowIfCancellationRequested();
int result = ExitCode.GenericFailure;
var testApp = new TestApplication(
module,
buildOptions,
testOptions,
resultsDirectoryResolver,
output,
onHelpRequested,
artifactPostProcessingManager,
testRunPolicy: testRunPolicy,
testApplicationSettings: GetEffectiveSettings(module));
try
{
output.TestMapCollectionModuleStarted(module.TargetPath);
using (testApp)
{
result = await testApp.RunAsync(ctrlC);
}
}
catch (Exception ex)
{
output.MarkModuleExecutionIncomplete();
output.MarkCoverageIncomplete();
var exAsString = ex.ToString();
Logger.LogTrace($"Exception running test module {module.RunProperties?.Command} {module.RunProperties?.Arguments}: {exAsString}");
Reporter.Error.WriteLine(string.Format(CliCommandStrings.ErrorRunningTestModule, module.RunProperties?.Command, module.RunProperties?.Arguments, exAsString));
result = ExitCode.GenericFailure;
}
finally
{
output.TestMapCollectionModuleCompleted();
}
// A module that ran zero tests (exit code 8) is not, by itself, a whole-run failure.
// With --test-modules or a global --filter, some modules may legitimately match no tests.
// Normalize it to success here; the aggregate "zero tests ran" verdict is decided once at
// the whole-run level in MicrosoftTestingPlatformTestCommand from the total test count. A
// stricter per-module minimum requested via -- --minimum-expected-tests N still fails that
// module with ExitCode.MinimumExpectedTestsPolicyViolation (9) and is preserved.
// See https://github.com/microsoft/testfx/issues/7457.
result = NormalizeExitCode(result, testApp.HasFailureDuringDispose, output);
lock (_lock)
{
if (_aggregateExitCode is null)
{
// This is the first result we are getting.
// So we assign the exit code, regardless of whether it's failure or success.
_aggregateExitCode = result;
}
else if (_aggregateExitCode.Value != result)
{
if (_aggregateExitCode == ExitCode.Success)
{
// The current result we are dealing with is the first failure after previous Success.
// So we assign the current failure.
_aggregateExitCode = result;
}
else if (result != ExitCode.Success)
{
// If we get a new failure result, which is different from a previous failure, we use GenericFailure.
_aggregateExitCode = ExitCode.GenericFailure;
}
else
{
// The current result is a success, but we already have a failure.
// So, we keep the failure exit code.
}
}
}
}
}
}
catch (OperationCanceledException) when (_cancellationToken.IsCancellationRequested)
{
// Stop scheduling new test apps once cancellation is requested.
// Already-running test apps are left alone so they can gracefully cancel themselves
// and report final session state via IPC.
}
}
internal bool FailOnAllSkippedTests
{
get
{
lock (_lock)
{
IReadOnlyList<TestApplicationPolicy> effectivePolicies = GetEffectivePolicies();
return ShouldFailOnAllSkippedTests(effectivePolicies);
}
}
}
internal int ApplyExitCodeIgnorePolicy(int exitCode)
{
lock (_lock)
{
IReadOnlyList<TestApplicationPolicy> effectivePolicies = GetEffectivePolicies();
return ApplyExitCodeIgnorePolicy(exitCode, effectivePolicies);
}
}
internal static bool ShouldFailOnAllSkippedTests(IReadOnlyList<TestApplicationPolicy> effectivePolicies)
=> effectivePolicies.Any(policy =>
policy.FailOnAllSkippedTests &&
MicrosoftTestingPlatformTestCommand.ApplyExitCodeIgnorePolicy(
ExitCode.ZeroTests,
policy.IgnoredExitCodes) != ExitCode.Success);
internal static int ApplyExitCodeIgnorePolicy(
int exitCode,
IReadOnlyList<TestApplicationPolicy> effectivePolicies)
=> effectivePolicies.All(policy =>
MicrosoftTestingPlatformTestCommand.ApplyExitCodeIgnorePolicy(
exitCode,
policy.IgnoredExitCodes) == ExitCode.Success)
? ExitCode.Success
: exitCode;
private IReadOnlyList<TestApplicationPolicy> GetEffectivePolicies()
=> _effectivePolicies.Count == 0
? [_fallbackTestApplicationSettings.Policy]
: _effectivePolicies;
private TestApplicationSettings GetEffectiveSettings(TestModule module)
{
lock (_lock)
{
return _effectiveSettings.TryGetValue(module, out TestApplicationSettings settings)
? settings
: _fallbackTestApplicationSettings;
}
}
internal static int NormalizeExitCode(int result, bool hasFailureDuringDispose, TerminalTestReporter output)
{
if (hasFailureDuringDispose)
{
output.MarkModuleExecutionIncomplete();
}
if (result == ExitCode.ZeroTests)
{
result = ExitCode.Success;
}
return result == ExitCode.Success && hasFailureDuringDispose
? ExitCode.GenericFailure
: result;
}
}