Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<PackageVersion Include="BenchmarkDotNet.Annotations" Version="0.15.8" />
<PackageVersion Include="coverlet.collector" Version="10.0.1" />
<PackageVersion Include="CliWrap" Version="3.10.2" />
<PackageVersion Include="EnumerableAsyncProcessor" Version="3.8.4" />
<PackageVersion Include="FakeItEasy" Version="9.0.1" />
<PackageVersion Include="FluentValidation.DependencyInjectionExtensions" Version="12.1.1" />
<PackageVersion Include="FsCheck" Version="3.3.3" />
Expand Down
341 changes: 49 additions & 292 deletions src/TUnit.Engine/Building/TestBuilderPipeline.cs

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion src/TUnit.Engine/TUnit.Engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="EnumerableAsyncProcessor" />
<PackageReference Include="Microsoft.Testing.Extensions.TrxReport.Abstractions" />
<PackageReference Include="Microsoft.Testing.Platform" />
</ItemGroup>
Expand Down
32 changes: 5 additions & 27 deletions src/TUnit.Engine/TestDiscoveryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ private async IAsyncEnumerable<AbstractExecutableTest> DiscoverTestsStreamAsync(
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(EngineDefaults.DiscoveryTimeout);

var tests = await _testBuilderPipeline.BuildTestsStreamingAsync(testSessionId, buildingContext, metadataFilter: null, cts.Token).ConfigureAwait(false);
var tests = await _testBuilderPipeline.BuildTestsAsync(testSessionId, buildingContext, metadataFilter: null, cts.Token).ConfigureAwait(false);

foreach (var test in tests)
{
Expand Down Expand Up @@ -382,34 +382,12 @@ public async IAsyncEnumerable<AbstractExecutableTest> DiscoverTestsFullyStreamin



private async Task InvokePostResolutionEventsInParallelAsync(List<AbstractExecutableTest> allTests)
private Task InvokePostResolutionEventsInParallelAsync(List<AbstractExecutableTest> allTests)
{
if (allTests.Count < Building.ParallelThresholds.MinItemsForParallel)
{
foreach (var test in allTests)
{
await _testBuilderPipeline.InvokePostResolutionEventsAsync(test).ConfigureAwait(false);
}
return;
}

#if NET8_0_OR_GREATER
await Parallel.ForEachAsync(
return Utilities.ParallelMap.ForEachParallelAsync(
allTests,
new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount },
async (test, _) =>
{
await _testBuilderPipeline.InvokePostResolutionEventsAsync(test).ConfigureAwait(false);
}
).ConfigureAwait(false);
#else
var tasks = new Task[allTests.Count];
for (var i = 0; i < allTests.Count; i++)
{
tasks[i] = _testBuilderPipeline.InvokePostResolutionEventsAsync(allTests[i]).AsTask();
}
await Task.WhenAll(tasks).ConfigureAwait(false);
#endif
test => _testBuilderPipeline.InvokePostResolutionEventsAsync(test),
Environment.ProcessorCount);
}

public IEnumerable<TestContext> GetCachedTestContexts()
Expand Down
164 changes: 164 additions & 0 deletions src/TUnit.Engine/Utilities/ParallelMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
namespace TUnit.Engine.Utilities;

/// <summary>
/// Allocation-light bounded-parallelism async map.
/// Results preserve source order. A fixed set of workers pulls items via an interlocked
/// cursor, so no partitioner, channel, or per-item task is allocated.
/// Sets smaller than <see cref="SequentialThreshold"/> run sequentially, where task
/// scheduling overhead would exceed the parallelization benefit.
/// </summary>
internal static class ParallelMap
{
/// <summary>
/// Minimum number of items before parallel processing is used.
/// </summary>
public const int SequentialThreshold = 8;

/// <summary>
/// Void counterpart of <see cref="SelectParallelAsync{TSource,TResult}"/> — same worker
/// model without the results array. Takes a ValueTask action so synchronously-completing
/// callees don't allocate a Task per item.
/// </summary>
public static async Task ForEachParallelAsync<TSource>(
IReadOnlyList<TSource> source,
Func<TSource, ValueTask> action,
int maxDegreeOfParallelism,
CancellationToken cancellationToken = default)
{
var count = source.Count;

if (count == 0)
{
return;
}

var workerCount = Math.Min(maxDegreeOfParallelism, count);

if (workerCount <= 1 || count < SequentialThreshold)
{
for (var i = 0; i < count; i++)
{
cancellationToken.ThrowIfCancellationRequested();
await action(source[i]).ConfigureAwait(false);
}

return;
}

var cursor = -1;

Func<Task> worker = WorkerAsync;
var workers = new Task[workerCount];
for (var w = 0; w < workerCount; w++)
{
workers[w] = Task.Run(worker, CancellationToken.None);
}

await Task.WhenAll(workers).ConfigureAwait(false);

return;

async Task WorkerAsync()
{
while (true)
{
var index = Interlocked.Increment(ref cursor);

if (index >= count)
{
return;
}

cancellationToken.ThrowIfCancellationRequested();

try
{
await action(source[index]).ConfigureAwait(false);
}
catch
{
// Park the cursor so sibling workers stop pulling new items;
// Task.WhenAll surfaces this fault once in-flight items finish.
Volatile.Write(ref cursor, count);
throw;
}
}
}
}

public static Task<TResult[]> SelectParallelAsync<TSource, TResult>(
IReadOnlyList<TSource> source,
Func<TSource, Task<TResult>> selector,
int maxDegreeOfParallelism,
CancellationToken cancellationToken = default)
=> ForParallelAsync(source.Count, index => selector(source[index]), maxDegreeOfParallelism, cancellationToken);

public static async Task<TResult[]> ForParallelAsync<TResult>(
int count,
Func<int, Task<TResult>> selector,
int maxDegreeOfParallelism,
CancellationToken cancellationToken = default)
{
if (count == 0)
{
return [];
}

var results = new TResult[count];
var workerCount = Math.Min(maxDegreeOfParallelism, count);

if (workerCount <= 1 || count < SequentialThreshold)
{
for (var i = 0; i < count; i++)
{
cancellationToken.ThrowIfCancellationRequested();
results[i] = await selector(i).ConfigureAwait(false);
}

return results;
}

var cursor = -1;

// Task.Run rather than invoking WorkerAsync directly: a selector that completes
// synchronously would otherwise run the entire loop inline on this thread and
// serialize all the other workers.
Func<Task> worker = WorkerAsync;
var workers = new Task[workerCount];
for (var w = 0; w < workerCount; w++)
{
workers[w] = Task.Run(worker, CancellationToken.None);
}

await Task.WhenAll(workers).ConfigureAwait(false);

return results;

async Task WorkerAsync()
{
while (true)
{
var index = Interlocked.Increment(ref cursor);

if (index >= count)
{
return;
}

cancellationToken.ThrowIfCancellationRequested();

try
{
results[index] = await selector(index).ConfigureAwait(false);
}
catch
{
// Park the cursor so sibling workers stop pulling new items;
// Task.WhenAll surfaces this fault once in-flight items finish.
Volatile.Write(ref cursor, count);
throw;
}
}
}
}
}
Loading
Loading