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
6 changes: 2 additions & 4 deletions TUnit.Engine/Services/TestExecution/TestCoordinator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,9 @@ public TestCoordinator(
_eventReceiverOrchestrator = eventReceiverOrchestrator;
}

public async ValueTask ExecuteTestAsync(AbstractExecutableTest test, CancellationToken cancellationToken)
{
await _executionGuard.TryStartExecutionAsync(test.TestId,
public ValueTask ExecuteTestAsync(AbstractExecutableTest test, CancellationToken cancellationToken)
=> _executionGuard.TryStartExecutionAsync(test.TestId,
() => ExecuteTestInternalAsync(test, cancellationToken));
}

private async ValueTask ExecuteTestInternalAsync(AbstractExecutableTest test, CancellationToken cancellationToken)
{
Expand Down
12 changes: 5 additions & 7 deletions TUnit.Engine/Services/TestExecution/TestExecutionGuard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,36 @@ internal sealed class TestExecutionGuard
{
private readonly ConcurrentDictionary<string, TaskCompletionSource<bool>> _executingTests = new();

public ValueTask<bool> TryStartExecutionAsync(string testId, Func<ValueTask> executionFunc)
public ValueTask TryStartExecutionAsync(string testId, Func<ValueTask> executionFunc)
{
// Fast path: check if test is already executing without allocating a TCS
if (_executingTests.TryGetValue(testId, out var existingTcs))
{
return new ValueTask<bool>(WaitForExistingExecutionAsync(existingTcs));
return new ValueTask(WaitForExistingExecutionAsync(existingTcs));
}

var tcs = new TaskCompletionSource<bool>();
existingTcs = _executingTests.GetOrAdd(testId, tcs);

if (existingTcs != tcs)
{
return new ValueTask<bool>(WaitForExistingExecutionAsync(existingTcs));
return new ValueTask(WaitForExistingExecutionAsync(existingTcs));
}

return ExecuteAndCompleteAsync(testId, tcs, executionFunc);
}

private static async Task<bool> WaitForExistingExecutionAsync(TaskCompletionSource<bool> tcs)
private static async Task WaitForExistingExecutionAsync(TaskCompletionSource<bool> tcs)
{
await tcs.Task.ConfigureAwait(false);
return false;
}

private async ValueTask<bool> ExecuteAndCompleteAsync(string testId, TaskCompletionSource<bool> tcs, Func<ValueTask> executionFunc)
private async ValueTask ExecuteAndCompleteAsync(string testId, TaskCompletionSource<bool> tcs, Func<ValueTask> executionFunc)
{
try
{
await executionFunc().ConfigureAwait(false);
tcs.SetResult(true);
return true;
}
catch (Exception ex)
{
Expand Down
Loading