-
-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathTestBuilderPipeline.cs
More file actions
581 lines (512 loc) · 24.9 KB
/
Copy pathTestBuilderPipeline.cs
File metadata and controls
581 lines (512 loc) · 24.9 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using EnumerableAsyncProcessor.Extensions;
using Microsoft.Testing.Platform.Requests;
using TUnit.Core;
using TUnit.Core.Helpers;
using TUnit.Core.Interfaces;
using TUnit.Core.Services;
using TUnit.Engine.Building.Interfaces;
using TUnit.Engine.Services;
using TUnit.Engine.Utilities;
namespace TUnit.Engine.Building;
/// <summary>
/// Threshold below which sequential processing is used instead of parallel.
/// For small test sets, the overhead of task scheduling exceeds parallelization benefits.
/// </summary>
internal static class ParallelThresholds
{
/// <summary>
/// Minimum number of items before parallel processing is used.
/// Below this threshold, sequential processing avoids task scheduling overhead.
/// </summary>
public const int MinItemsForParallel = 8;
}
internal sealed class TestBuilderPipeline
{
private readonly ITestDataCollector _dataCollector;
private readonly ITestBuilder _testBuilder;
private readonly IContextProvider _contextProvider;
private readonly EventReceiverOrchestrator _eventReceiverOrchestrator;
public TestBuilderPipeline(
ITestDataCollector dataCollector,
ITestBuilder testBuilder,
IContextProvider contextBuilder,
EventReceiverOrchestrator eventReceiverOrchestrator)
{
_dataCollector = dataCollector ?? throw new ArgumentNullException(nameof(dataCollector));
_testBuilder = testBuilder ?? throw new ArgumentNullException(nameof(testBuilder));
_contextProvider = contextBuilder;
_eventReceiverOrchestrator = eventReceiverOrchestrator ?? throw new ArgumentNullException(nameof(eventReceiverOrchestrator));
}
private TestBuilderContext CreateTestBuilderContext(TestMetadata metadata)
{
var testBuilderContext = new TestBuilderContext
{
TestMetadata = metadata.MethodMetadata,
Events = new TestContextEvents(),
StateBag = new ConcurrentDictionary<string, object?>()
};
// Check for ClassConstructor attribute and set it early if present
var attributes = metadata.GetOrCreateAttributes();
// Look for any attribute that inherits from ClassConstructorAttribute
// This handles both ClassConstructorAttribute and ClassConstructorAttribute<T>
var classConstructorAttribute = attributes
.Where(a => a is ClassConstructorAttribute)
.Cast<ClassConstructorAttribute>()
.FirstOrDefault();
if (classConstructorAttribute != null)
{
testBuilderContext.ClassConstructor = (IClassConstructor)Activator.CreateInstance(classConstructorAttribute.ClassConstructorType)!;
}
return testBuilderContext;
}
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "Reflection mode is not used in AOT/trimmed scenarios")]
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = "Reflection mode is not used in AOT scenarios")]
public async Task<IEnumerable<AbstractExecutableTest>> BuildTestsAsync(string testSessionId)
{
var collectedMetadata = await _dataCollector.CollectTestsAsync(testSessionId).ConfigureAwait(false);
// For this method (non-streaming), we're not in execution mode so no filter optimization
var buildingContext = new TestBuildingContext(IsForExecution: false, Filter: null);
return await BuildTestsFromMetadataAsync(collectedMetadata, buildingContext).ConfigureAwait(false);
}
/// <summary>
/// Collects all test metadata without building tests.
/// This is a lightweight operation used for dependency analysis.
/// </summary>
public async Task<IEnumerable<TestMetadata>> CollectTestMetadataAsync(string testSessionId)
{
return await _dataCollector.CollectTestsAsync(testSessionId).ConfigureAwait(false);
}
/// <summary>
/// Collects test metadata without building tests, with optional filter-aware pre-filtering.
/// When a filter with extractable hints is provided, only test sources that could match are enumerated.
/// </summary>
public async Task<IEnumerable<TestMetadata>> CollectTestMetadataAsync(string testSessionId, ITestExecutionFilter? filter)
{
return await _dataCollector.CollectTestsAsync(testSessionId, filter).ConfigureAwait(false);
}
/// <summary>
/// Streaming version that yields tests as they're built without buffering
/// </summary>
/// <param name="testSessionId">The test session identifier</param>
/// <param name="buildingContext">Context for test building</param>
/// <param name="metadataFilter">Optional predicate to filter which metadata should be built (null means build all)</param>
/// <param name="cancellationToken">Cancellation token</param>
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "Reflection mode is not used in AOT/trimmed scenarios")]
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = "Reflection mode is not used in AOT scenarios")]
public async Task<IEnumerable<AbstractExecutableTest>> BuildTestsStreamingAsync(
string testSessionId,
TestBuildingContext buildingContext,
Func<TestMetadata, bool>? metadataFilter = null,
CancellationToken cancellationToken = default)
{
// Get metadata streaming if supported
// Fall back to non-streaming collection
var collectedMetadata = await _dataCollector.CollectTestsAsync(testSessionId).ConfigureAwait(false);
// Apply metadata filter if provided (for dependency-aware filtering optimization)
if (metadataFilter != null)
{
collectedMetadata = collectedMetadata.Where(metadataFilter);
}
return await collectedMetadata
.SelectManyAsync(metadata => BuildTestsFromSingleMetadataAsync(metadata, buildingContext, cancellationToken), cancellationToken: cancellationToken)
.ProcessInParallel(cancellationToken: cancellationToken);
}
/// <summary>
/// Builds tests from pre-collected metadata.
/// Use this when metadata has already been collected with filter-aware optimization.
/// </summary>
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "Reflection mode is not used in AOT/trimmed scenarios")]
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = "Reflection mode is not used in AOT scenarios")]
public async Task<IEnumerable<AbstractExecutableTest>> BuildTestsFromMetadataAsync(
IEnumerable<TestMetadata> testMetadata,
TestBuildingContext buildingContext,
CancellationToken cancellationToken = default)
{
// Materialize to check count - for small sets, sequential processing is faster
var metadataList = testMetadata as IList<TestMetadata> ?? testMetadata.ToList();
IEnumerable<IEnumerable<AbstractExecutableTest>> testGroups;
if (metadataList.Count < ParallelThresholds.MinItemsForParallel)
{
// Sequential processing for small sets - avoids task scheduling overhead
var results = new List<IEnumerable<AbstractExecutableTest>>(metadataList.Count);
foreach (var metadata in metadataList)
{
cancellationToken.ThrowIfCancellationRequested();
try
{
if (metadata is IDynamicTestMetadata)
{
results.Add(await GenerateDynamicTests(metadata).ConfigureAwait(false));
}
else
{
results.Add(await _testBuilder.BuildTestsFromMetadataAsync(metadata, buildingContext, cancellationToken).ConfigureAwait(false));
}
}
catch (Exception ex)
{
var failedTest = CreateFailedTestForDataGenerationError(metadata, ex);
results.Add([failedTest]);
}
}
testGroups = results;
}
else
{
// Parallel processing for larger sets
testGroups = await metadataList.SelectAsync(async metadata =>
{
try
{
// Check if this is a dynamic test metadata that should bypass normal test building
if (metadata is IDynamicTestMetadata)
{
return await GenerateDynamicTests(metadata).ConfigureAwait(false);
}
return await _testBuilder.BuildTestsFromMetadataAsync(metadata, buildingContext, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
var failedTest = CreateFailedTestForDataGenerationError(metadata, ex);
return (IEnumerable<AbstractExecutableTest>)[failedTest];
}
}, cancellationToken: cancellationToken)
.ProcessInParallel(Environment.ProcessorCount);
}
return testGroups.SelectMany(x => x);
}
private async Task<AbstractExecutableTest[]> GenerateDynamicTests(TestMetadata metadata)
{
// Use pre-extracted repeat count from metadata (avoids instantiating attributes)
var repeatCount = metadata.RepeatCount ?? 0;
// Get dynamic test metadata for DisplayName support
var dynamicTestMetadata = metadata as IDynamicTestMetadata;
return await Enumerable.Range(0, repeatCount + 1)
.SelectAsync(async repeatIndex =>
{
// Create a simple TestData for ID generation
// Use DynamicTestIndex from the metadata to ensure unique test IDs for multiple dynamic tests
var dynamicTestIndex = dynamicTestMetadata?.DynamicTestIndex ?? 0;
var testData = new TestBuilder.TestData
{
TestClassInstanceFactory = () => Task.FromResult(metadata.InstanceFactory(Type.EmptyTypes, [])),
ClassDataSourceAttributeIndex = 0,
ClassDataLoopIndex = 0,
ClassData = [],
MethodDataSourceAttributeIndex = 0,
MethodDataLoopIndex = dynamicTestIndex,
MethodData = [],
RepeatIndex = repeatIndex,
InheritanceDepth = metadata.InheritanceDepth,
ResolvedClassGenericArguments = Type.EmptyTypes,
ResolvedMethodGenericArguments = Type.EmptyTypes
};
var testId = TestIdentifierService.GenerateTestId(metadata, testData);
// Use custom DisplayName if specified, otherwise fall back to TestName
var baseDisplayName = dynamicTestMetadata?.DisplayName ?? metadata.TestName;
var displayName = repeatCount > 0
? $"{baseDisplayName} (Repeat {repeatIndex + 1}/{repeatCount + 1})"
: baseDisplayName;
// Get attributes first
var attributes = metadata.GetOrCreateAttributes();
// Create TestDetails for dynamic tests
var testDetails = new TestDetails(attributes)
{
TestId = testId,
TestName = metadata.TestName,
ClassType = metadata.TestClassType,
MethodName = metadata.TestMethodName,
ClassInstance = PlaceholderInstance.Instance,
TestMethodArguments = [],
TestClassArguments = [],
TestFilePath = metadata.FilePath ?? "Unknown",
TestLineNumber = metadata.LineNumber,
ReturnType = typeof(Task),
MethodMetadata = metadata.MethodMetadata,
AttributesByType = attributes.ToAttributeDictionary(),
Timeout = Core.Settings.TUnitSettings.Default.Timeouts.DefaultTestTimeout
};
var testBuilderContext = CreateTestBuilderContext(metadata);
var context = _contextProvider.CreateTestContext(
metadata.TestName,
metadata.TestClassType,
testBuilderContext,
CancellationToken.None);
// Set the TestDetails on the context
context.Metadata.TestDetails = testDetails;
// Set custom display name for dynamic tests if specified
if (dynamicTestMetadata?.DisplayName != null)
{
context.Metadata.DisplayName = dynamicTestMetadata.DisplayName;
}
// Invoke discovery event receivers to properly handle all attribute behaviors
await InvokeDiscoveryEventReceiversAsync(context).ConfigureAwait(false);
var executableTestContext = new ExecutableTestCreationContext
{
TestId = testId,
DisplayName = displayName,
Arguments = [],
ClassArguments = [],
Context = context,
TestClassInstanceFactory = testData.TestClassInstanceFactory
};
return metadata.CreateExecutableTestFactory(executableTestContext, metadata);
})
.ProcessInParallel(Environment.ProcessorCount);
}
/// <summary>
/// Build tests from a single metadata item, yielding them as they're created
/// </summary>
#if NET8_0_OR_GREATER
[RequiresUnreferencedCode("Test building in reflection mode uses generic type resolution which requires unreferenced code")]
#endif
private async IAsyncEnumerable<AbstractExecutableTest> BuildTestsFromSingleMetadataAsync(TestMetadata metadata, TestBuildingContext buildingContext, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
{
TestMetadata resolvedMetadata;
Exception? resolutionError = null;
try
{
resolvedMetadata = metadata;
}
catch (Exception ex)
{
resolutionError = ex;
resolvedMetadata = metadata; // Use original for error reporting
}
if (resolutionError != null)
{
yield return CreateFailedTestForGenericResolutionError(metadata, resolutionError);
yield break;
}
List<AbstractExecutableTest>? testsToYield = null;
Exception? buildError = null;
try
{
// Check if this is a dynamic test metadata that should bypass normal test building
if (resolvedMetadata is IDynamicTestMetadata)
{
testsToYield =
[
];
// Use pre-extracted repeat count from metadata (avoids instantiating attributes)
var repeatCount = resolvedMetadata.RepeatCount ?? 0;
// Get attributes for test details
var attributes = resolvedMetadata.GetOrCreateAttributes();
// Dynamic tests need to honor attributes like RepeatCount, RetryCount, etc.
// We'll create multiple test instances based on RepeatCount
// Use DynamicTestIndex from the metadata to ensure unique test IDs for multiple dynamic tests
var dynamicTestIndex = ((IDynamicTestMetadata)resolvedMetadata).DynamicTestIndex;
for (var repeatIndex = 0; repeatIndex < repeatCount + 1; repeatIndex++)
{
// Create a simple TestData for ID generation
var testData = new TestBuilder.TestData
{
TestClassInstanceFactory = () => Task.FromResult(resolvedMetadata.InstanceFactory(Type.EmptyTypes, [])),
ClassDataSourceAttributeIndex = 0,
ClassDataLoopIndex = 0,
ClassData = [],
MethodDataSourceAttributeIndex = 0,
MethodDataLoopIndex = dynamicTestIndex,
MethodData = [],
RepeatIndex = repeatIndex,
InheritanceDepth = resolvedMetadata.InheritanceDepth,
ResolvedClassGenericArguments = Type.EmptyTypes,
ResolvedMethodGenericArguments = Type.EmptyTypes
};
var testId = TestIdentifierService.GenerateTestId(resolvedMetadata, testData);
var dynamicMetadata = (IDynamicTestMetadata)resolvedMetadata;
var baseDisplayName = dynamicMetadata.DisplayName ?? resolvedMetadata.TestName;
var displayName = repeatCount > 0
? $"{baseDisplayName} (Repeat {repeatIndex + 1}/{repeatCount + 1})"
: baseDisplayName;
// Create TestDetails for dynamic tests
var testDetails = new TestDetails(attributes)
{
TestId = testId,
TestName = resolvedMetadata.TestName,
ClassType = resolvedMetadata.TestClassType,
MethodName = resolvedMetadata.TestMethodName,
ClassInstance = PlaceholderInstance.Instance,
TestMethodArguments = [],
TestClassArguments = [],
TestFilePath = resolvedMetadata.FilePath ?? "Unknown",
TestLineNumber = resolvedMetadata.LineNumber,
ReturnType = typeof(Task),
MethodMetadata = resolvedMetadata.MethodMetadata,
AttributesByType = attributes.ToAttributeDictionary(),
Timeout = Core.Settings.TUnitSettings.Default.Timeouts.DefaultTestTimeout
};
var context = _contextProvider.CreateTestContext(
resolvedMetadata.TestName,
resolvedMetadata.TestClassType,
CreateTestBuilderContext(resolvedMetadata),
CancellationToken.None);
// Set the TestDetails on the context
context.Metadata.TestDetails = testDetails;
// Set custom display name for dynamic tests if specified
if (dynamicMetadata.DisplayName != null)
{
context.Metadata.DisplayName = dynamicMetadata.DisplayName;
}
// Invoke discovery event receivers to properly handle all attribute behaviors
await InvokeDiscoveryEventReceiversAsync(context).ConfigureAwait(false);
var executableTestContext = new ExecutableTestCreationContext
{
TestId = testId,
DisplayName = displayName,
Arguments = [],
ClassArguments = [],
Context = context,
TestClassInstanceFactory = testData.TestClassInstanceFactory
};
var executableTest = resolvedMetadata.CreateExecutableTestFactory(executableTestContext, resolvedMetadata);
testsToYield.Add(executableTest);
}
}
else
{
// Normal test metadata goes through the standard test builder
var testsFromMetadata = await _testBuilder.BuildTestsFromMetadataAsync(resolvedMetadata, buildingContext, cancellationToken).ConfigureAwait(false);
testsToYield = new List<AbstractExecutableTest>(testsFromMetadata);
}
}
catch (Exception ex)
{
buildError = ex;
}
if (buildError != null)
{
yield return CreateFailedTestForDataGenerationError(resolvedMetadata, buildError);
}
else if (testsToYield != null)
{
foreach (var test in testsToYield)
{
yield return test;
}
}
}
private AbstractExecutableTest CreateFailedTestForDataGenerationError(TestMetadata metadata, Exception exception)
{
var testId = TestIdentifierService.GenerateFailedTestId(metadata);
var displayName = $"{metadata.TestClassType.Name}.{metadata.TestName}";
var testDetails = new TestDetails([])
{
TestId = testId,
TestName = metadata.TestName,
ClassType = metadata.TestClassType,
MethodName = metadata.TestMethodName,
ClassInstance = null!,
TestMethodArguments = [],
TestClassArguments = [],
TestFilePath = metadata.FilePath ?? "Unknown",
TestLineNumber = metadata.LineNumber,
ReturnType = typeof(Task),
MethodMetadata = metadata.MethodMetadata,
AttributesByType = AttributeDictionaryHelper.Empty,
Timeout = Core.Settings.TUnitSettings.Default.Timeouts.DefaultTestTimeout
};
var context = _contextProvider.CreateTestContext(
metadata.TestName,
metadata.TestClassType,
CreateTestBuilderContext(metadata),
CancellationToken.None);
context.Metadata.TestDetails = testDetails;
var now = DateTimeOffset.UtcNow;
return new FailedExecutableTest(exception)
{
TestId = testId,
Metadata = metadata,
Arguments = [],
ClassArguments = [],
Context = context,
State = TestState.Failed,
Result = new TestResult
{
State = TestState.Failed,
Start = now,
End = now,
Duration = TimeSpan.Zero,
Exception = exception,
ComputerName = EnvironmentHelper.MachineName,
TestContext = context
}
};
}
private AbstractExecutableTest CreateFailedTestForGenericResolutionError(TestMetadata metadata, Exception exception)
{
var testId = TestIdentifierService.GenerateFailedTestId(metadata);
var displayName = $"{metadata.TestName} [GENERIC RESOLUTION ERROR]";
var testDetails = new TestDetails([])
{
TestId = testId,
TestName = metadata.TestName,
ClassType = metadata.TestClassType,
MethodName = metadata.TestMethodName,
ClassInstance = null!,
TestMethodArguments = [],
TestClassArguments = [],
TestFilePath = metadata.FilePath ?? "Unknown",
TestLineNumber = metadata.LineNumber,
ReturnType = typeof(Task),
MethodMetadata = metadata.MethodMetadata,
AttributesByType = AttributeDictionaryHelper.Empty,
Timeout = Core.Settings.TUnitSettings.Default.Timeouts.DefaultTestTimeout
};
var context = _contextProvider.CreateTestContext(
metadata.TestName,
metadata.TestClassType,
CreateTestBuilderContext(metadata),
CancellationToken.None);
context.Metadata.TestDetails = testDetails;
var now = DateTimeOffset.UtcNow;
return new FailedExecutableTest(exception)
{
TestId = testId,
Metadata = metadata,
Arguments = [],
ClassArguments = [],
Context = context,
State = TestState.Failed,
Result = new TestResult
{
State = TestState.Failed,
Start = now,
End = now,
Duration = TimeSpan.Zero,
Exception = exception,
ComputerName = EnvironmentHelper.MachineName,
TestContext = context
}
};
}
private async Task InvokeDiscoveryEventReceiversAsync(TestContext context)
{
var discoveredContext = new DiscoveredTestContext(
context.Metadata.TestDetails.TestName,
context);
await _eventReceiverOrchestrator.InvokeTestDiscoveryEventReceiversAsync(context, discoveredContext, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Invokes event receivers after dependencies have been resolved.
/// Delegates to TestBuilder.InvokePostResolutionEventsAsync.
/// </summary>
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "Reflection mode is not used in AOT/trimmed scenarios")]
public ValueTask InvokePostResolutionEventsAsync(AbstractExecutableTest test)
=> _testBuilder.InvokePostResolutionEventsAsync(test);
/// <summary>
/// Populates dependencies for all tests without invoking event receivers.
/// This should be called before After(TestDiscovery) hooks run, so that hooks
/// can access dependency information on any TestContext.
/// </summary>
/// <param name="tests">All tests that have had their dependencies resolved</param>
public void PopulateAllDependencies(IEnumerable<AbstractExecutableTest> tests)
{
foreach (var test in tests)
{
_testBuilder.PopulateDependenciesOnly(test);
}
}
}