Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
0d5d220
Perf Improvements
thomhurst Aug 8, 2025
dd2cc3b
chore: remove obsolete test result files and reflection log
thomhurst Aug 8, 2025
4216d5c
Remove redundant execution plan
thomhurst Aug 8, 2025
08e0de6
Optimize task handling and property injection for improved performance
thomhurst Aug 8, 2025
cd44cb8
Merge main branch and resolve conflicts
thomhurst Aug 8, 2025
3da8363
Update EnumerableAsyncProcessor to version 3.2.0 and modify GetOnlyDi…
thomhurst Aug 8, 2025
919b7e5
Remove 5 minute timeout from DedicatedThreadExecutor
thomhurst Aug 8, 2025
a0e8a25
Revert "Remove 5 minute timeout from DedicatedThreadExecutor"
thomhurst Aug 8, 2025
b311a46
fix: Resolve async data source deadlock in DedicatedThreadExecutor
thomhurst Aug 8, 2025
48bb732
Merge branch 'main' into feature/perf-08082025
thomhurst Aug 10, 2025
e5eb020
Fix build error: Remove TestMethodParameterTypes property that doesn'…
thomhurst Aug 10, 2025
32795dd
Remove maximum-parallel-tests argument from RunEngineTestsModule
thomhurst Aug 10, 2025
5360509
Merge branch 'main' into feature/perf-08082025
thomhurst Aug 10, 2025
a308c07
chore(deps): update EnumerableAsyncProcessor to version 3.6.0
thomhurst Aug 10, 2025
c99fd5d
refactor: convert synchronous methods to asynchronous and improve tim…
thomhurst Aug 10, 2025
9bae603
fix: handle unexpected test states and ensure TaskCompletionSource is…
thomhurst Aug 10, 2025
729a9d8
chore(deps): update EnumerableAsyncProcessor to version 3.6.3
thomhurst Aug 10, 2025
59ce527
refactor: remove obsolete TryCreateWithInitializer method and impleme…
thomhurst Aug 10, 2025
d0c2340
refactor: implement KeyedConstraintManager to manage test execution w…
thomhurst Aug 10, 2025
29d7884
feat: add hang dump configuration to all test runner modules
thomhurst Aug 10, 2025
3613cbf
feat: add Microsoft.Testing.Extensions.HangDump package to multiple t…
thomhurst Aug 10, 2025
2a0550b
feat: add Microsoft.Testing.Extensions.HangDump package reference to …
thomhurst Aug 10, 2025
9e51ecc
feat: add Microsoft.Testing.Extensions.HangDump package reference to …
thomhurst Aug 10, 2025
a035a98
fix: eliminate deadlock in Context output handling using lock-free de…
thomhurst Aug 10, 2025
2af6f2d
fix: resolve potential deadlock in dependency resolution by setting T…
thomhurst Aug 10, 2025
2ba8871
Merge remote-tracking branch 'origin/main' into feature/perf-08082025
thomhurst Aug 10, 2025
c1bde0d
Fix hanging/deadlock issues in test execution
thomhurst Aug 10, 2025
4b183b3
Fix duplicate test IDs for inherited tests and remove debug logs
thomhurst Aug 11, 2025
94acbaf
Add InheritanceDepth property to API change verification files
thomhurst Aug 11, 2025
b97dc9a
Fix TestContext.Dependencies population to include transitive depende…
thomhurst Aug 11, 2025
d16d6d2
Remove redundant comments from dependency resolution code
thomhurst Aug 11, 2025
7795b68
Fix test scheduler deadlock by proactively starting dependencies
thomhurst Aug 11, 2025
e14930b
Fix test scheduler deadlocks in parallel and semaphore-constrained ex…
thomhurst Aug 11, 2025
d0fb98f
Fix test scheduler deadlocks and ensure unique test IDs
thomhurst Aug 11, 2025
3a64efb
Add InheritanceDepth property to test metadata for various test cases
thomhurst Aug 11, 2025
c44c7f9
Optimize parallel processing and add ConfigureAwait(false) to async c…
thomhurst Aug 11, 2025
bc03969
Update hangdump filename format and timeout for improved diagnostics
thomhurst Aug 11, 2025
8610772
Fix build errors with missing FilePath and LineNumber in TestMetadata
thomhurst Aug 11, 2025
a4b59cf
Prevent unobserved task exceptions by ensuring all background tasks a…
thomhurst Aug 11, 2025
9c930ec
Add FilePath and LineNumber properties to test metadata for improved …
thomhurst Aug 11, 2025
970a9af
Add FilePath and LineNumber to test metadata for enhanced diagnostics
thomhurst Aug 11, 2025
59ef8e3
Update hangdump timeout and test timeout for improved performance
thomhurst Aug 11, 2025
8b21d2b
Refactor circular dependency detection to return detailed dependency …
thomhurst Aug 11, 2025
48faea1
Add Microsoft.Testing.Extensions.HangDump package reference and refac…
thomhurst Aug 11, 2025
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
Prev Previous commit
Next Next commit
refactor: convert synchronous methods to asynchronous and improve tim…
…eout handling
  • Loading branch information
thomhurst committed Aug 10, 2025
commit c99fd5daf8f4c95f1b153eb0e5e69db376a229a2
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageVersion Include="BenchmarkDotNet.Annotations" Version="0.15.2" />
<PackageVersion Include="coverlet.collector" Version="6.0.4" />
<PackageVersion Include="CliWrap" Version="3.9.0" />
<PackageVersion Include="EnumerableAsyncProcessor" Version="3.6.0" />
<PackageVersion Include="EnumerableAsyncProcessor" Version="3.6.2" />
<PackageVersion Include="FluentValidation.DependencyInjectionExtensions" Version="12.0.0" />
<PackageVersion Include="FSharp.Core" Version="9.0.303" />
<PackageVersion Include="Humanizer" Version="2.14.1" />
Expand Down
5 changes: 3 additions & 2 deletions TUnit.Core/Attributes/TestData/MethodDataSourceAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.CompilerServices;
using TUnit.Core.Enums;
using TUnit.Core.Helpers;

Expand Down Expand Up @@ -176,11 +177,11 @@ private static bool IsAsyncEnumerable([DynamicallyAccessedMembers(DynamicallyAcc
}

[UnconditionalSuppressMessage("AOT", "IL2075:UnrecognizedReflectionPattern", Justification = "Data source methods may use dynamic patterns")]
private static async IAsyncEnumerable<object?> ConvertToAsyncEnumerable(object asyncEnumerable)
private static async IAsyncEnumerable<object?> ConvertToAsyncEnumerable(object asyncEnumerable, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
var type = asyncEnumerable.GetType();
var enumeratorMethod = type.GetMethod("GetAsyncEnumerator");
var enumerator = enumeratorMethod!.Invoke(asyncEnumerable, [CancellationToken.None]);
var enumerator = enumeratorMethod!.Invoke(asyncEnumerable, [cancellationToken]);

var moveNextMethod = enumerator!.GetType().GetMethod("MoveNextAsync");
var currentProperty = enumerator.GetType().GetProperty("Current");
Expand Down
47 changes: 37 additions & 10 deletions TUnit.Core/EngineCancellationToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public class EngineCancellationToken : IDisposable
/// Gets the cancellation token.
/// </summary>
public CancellationToken Token { get; private set; }

private CancellationTokenSource? _forcefulExitCts;
private volatile bool _forcefulExitStarted;

/// <summary>
/// Initializes the cancellation token with a linked token source.
Expand All @@ -24,26 +27,50 @@ internal void Initialise(CancellationToken cancellationToken)
CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
Token = CancellationTokenSource.Token;

Console.CancelKeyPress += (sender, e) =>
Console.CancelKeyPress += OnCancelKeyPress;
}

private void OnCancelKeyPress(object? sender, ConsoleCancelEventArgs e)
{
// Cancel the test execution
if (!CancellationTokenSource.IsCancellationRequested)
{
if (!CancellationTokenSource.IsCancellationRequested)
{
CancellationTokenSource.Cancel();
}
CancellationTokenSource.Cancel();
}

_ = Task.Delay(TimeSpan.FromSeconds(10)).ContinueWith(_ =>
// Only start the forceful exit timer once
if (!_forcefulExitStarted)
{
_forcefulExitStarted = true;

// Cancel any previous forceful exit timer
_forcefulExitCts?.Cancel();
_forcefulExitCts?.Dispose();
_forcefulExitCts = new CancellationTokenSource();

// Start a new forceful exit timer
_ = Task.Delay(TimeSpan.FromSeconds(10), _forcefulExitCts.Token).ContinueWith(t =>
{
Console.WriteLine("Forcefully terminating the process due to cancellation request.");
Environment.Exit(1);
});
};
if (!t.IsCanceled)
{
Console.WriteLine("Forcefully terminating the process due to cancellation request.");
Environment.Exit(1);
}
}, TaskScheduler.Default);
}

// Prevent the default behavior (immediate termination)
e.Cancel = true;
}

/// <summary>
/// Disposes the cancellation token source.
/// </summary>
public void Dispose()
{
Console.CancelKeyPress -= OnCancelKeyPress;
_forcefulExitCts?.Cancel();
_forcefulExitCts?.Dispose();
CancellationTokenSource.Dispose();
}
}
12 changes: 9 additions & 3 deletions TUnit.Core/Executors/DedicatedThreadExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ public override void Send(SendOrPostCallback d, object? state)
else
{
// For Send, we need to block until completion
// This is less ideal but necessary for the Send semantics
var tcs = new TaskCompletionSource<object?>();
// Use Task.Run to avoid potential deadlocks by ensuring we don't capture any synchronization context
var tcs = new TaskCompletionSource<object?>(TaskCreationOptions.RunContinuationsAsynchronously);

Post(_ =>
{
Expand All @@ -301,7 +301,13 @@ public override void Send(SendOrPostCallback d, object? state)
}
}, null);

// Wait for completion (this will block)
// Wait with a timeout to prevent infinite hangs
if (!tcs.Task.Wait(TimeSpan.FromMinutes(30)))
{
throw new TimeoutException("Synchronous operation on dedicated thread timed out after 30 minutes");
}

// Re-throw any exception that occurred
tcs.Task.GetAwaiter().GetResult();
}
}
Expand Down
26 changes: 19 additions & 7 deletions TUnit.Core/Helpers/DataSourceHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -411,20 +411,32 @@ public static bool IsTuple(object? obj)
/// Tries to create an instance using a generated creation method that handles init-only properties.
/// Returns true if successful, false if no creator is available.
/// </summary>
public static bool TryCreateWithInitializer(Type type, MethodMetadata testInformation, string testSessionId, out object createdInstance)
public static async Task<(bool success, object? createdInstance)> TryCreateWithInitializerAsync(Type type, MethodMetadata testInformation, string testSessionId)
{
createdInstance = null!;

// Check if we have a registered creator for this type
if (!TypeCreators.TryGetValue(type, out var creator))
{
return false;
return (false, null);
}

// Use the creator to create and initialize the instance
var task = creator(testInformation, testSessionId);
createdInstance = task.ConfigureAwait(false).GetAwaiter().GetResult();
return true;
var createdInstance = await creator(testInformation, testSessionId).ConfigureAwait(false);
return (true, createdInstance);
}

/// <summary>
/// Tries to create an instance using a generated creation method that handles init-only properties.
/// Returns true if successful, false if no creator is available.
/// This method is kept for backward compatibility but should be avoided in favor of TryCreateWithInitializerAsync.
/// </summary>
[Obsolete("Use TryCreateWithInitializerAsync instead to avoid potential deadlocks")]
public static bool TryCreateWithInitializer(Type type, MethodMetadata testInformation, string testSessionId, out object createdInstance)
{
// Use Task.Run to avoid potential deadlocks by running on thread pool
var task = Task.Run(async () => await TryCreateWithInitializerAsync(type, testInformation, testSessionId).ConfigureAwait(false));
var result = task.GetAwaiter().GetResult();
createdInstance = result.createdInstance!;
return result.success;
}

private static readonly Dictionary<Type, Func<MethodMetadata, string, Task<object>>> TypeCreators = new();
Expand Down
6 changes: 4 additions & 2 deletions TUnit.Engine/Building/TestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -624,8 +624,9 @@ private ValueTask<TestContext> CreateTestContextAsync(string testId, TestMetadat
MethodMetadata = metadata.MethodMetadata,
Attributes = attributes,
MethodGenericArguments = testData.ResolvedMethodGenericArguments,
ClassGenericArguments = testData.ResolvedClassGenericArguments
// Don't set Timeout and RetryLimit here - let discovery event receivers set them
ClassGenericArguments = testData.ResolvedClassGenericArguments,
Timeout = TimeSpan.FromMinutes(30) // Default 30-minute timeout (can be overridden by TimeoutAttribute)
// Don't set RetryLimit here - let discovery event receivers set it
};

var context = _contextProvider.CreateTestContext(
Expand Down Expand Up @@ -691,6 +692,7 @@ private static TestDetails CreateFailedTestDetails(TestMetadata metadata, string
ReturnType = typeof(Task),
MethodMetadata = metadata.MethodMetadata,
Attributes = metadata.AttributeFactory.Invoke(),
Timeout = TimeSpan.FromMinutes(30) // Default 30-minute timeout (can be overridden by TimeoutAttribute)
};
}

Expand Down
10 changes: 7 additions & 3 deletions TUnit.Engine/Building/TestBuilderPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,9 @@ private async Task<AbstractExecutableTest[]> GenerateDynamicTests(TestMetadata m
TestLineNumber = metadata.LineNumber ?? 0,
ReturnType = typeof(Task),
MethodMetadata = metadata.MethodMetadata,
Attributes = attributes
// Don't set Timeout and RetryLimit here - let discovery event receivers set them
Attributes = attributes,
Timeout = TimeSpan.FromMinutes(30) // Default 30-minute timeout (can be overridden by TimeoutAttribute)
// Don't set RetryLimit here - let discovery event receivers set it
};

var context = _contextProvider.CreateTestContext(
Expand Down Expand Up @@ -265,7 +266,8 @@ private async IAsyncEnumerable<AbstractExecutableTest> BuildTestsFromSingleMetad
TestLineNumber = resolvedMetadata.LineNumber ?? 0,
ReturnType = typeof(Task),
MethodMetadata = resolvedMetadata.MethodMetadata,
Attributes = attributes
Attributes = attributes,
Timeout = TimeSpan.FromMinutes(30) // Default 30-minute timeout (can be overridden by TimeoutAttribute)
// Don't set Timeout and RetryLimit here - let discovery event receivers set them
};

Expand Down Expand Up @@ -339,6 +341,7 @@ private AbstractExecutableTest CreateFailedTestForDataGenerationError(TestMetada
ReturnType = typeof(Task),
MethodMetadata = metadata.MethodMetadata,
Attributes = [],
Timeout = TimeSpan.FromMinutes(30) // Default 30-minute timeout
};

var context = _contextProvider.CreateTestContext(
Expand Down Expand Up @@ -393,6 +396,7 @@ private AbstractExecutableTest CreateFailedTestForGenericResolutionError(TestMet
ReturnType = typeof(Task),
MethodMetadata = metadata.MethodMetadata,
Attributes = [],
Timeout = TimeSpan.FromMinutes(30) // Default 30-minute timeout
};

var context = _contextProvider.CreateTestContext(
Expand Down
78 changes: 33 additions & 45 deletions TUnit.Engine/Discovery/ReflectionTestDataCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,60 +46,48 @@ public async Task<IEnumerable<TestMetadata>> CollectTestsAsync(string testSessio

Console.WriteLine($"Scanning {assemblies.Count} assemblies for tests...");

// Use indexed collection to maintain order
var resultsByIndex = new ConcurrentDictionary<int, List<TestMetadata>>();

// Use true parallel processing with thread pool threads
var parallelOptions = new ParallelOptions
{
MaxDegreeOfParallelism = Environment.ProcessorCount
};

Parallel.ForEach(assemblies.Select((assembly, index) => new
{
assembly, index
}), parallelOptions, item =>
// Use async parallel processing with proper task-based approach
var tasks = new Task<List<TestMetadata>>[assemblies.Count];

for (var i = 0; i < assemblies.Count; i++)
{
var assembly = item.assembly;
var index = item.index;

lock (_lock)
var assembly = assemblies[i];
var index = i;
tasks[index] = Task.Run(async () =>
{
if (!_scannedAssemblies.Add(assembly))
lock (_lock)
{
resultsByIndex[index] =
[
];
return;
if (!_scannedAssemblies.Add(assembly))
{
return new List<TestMetadata>();
}
}
}

try
{
Console.WriteLine($"Scanning assembly: {assembly.GetName().Name}");
// Run async method synchronously since we're in parallel processing context
var testsInAssembly = DiscoverTestsInAssembly(assembly).ConfigureAwait(false).GetAwaiter().GetResult();
resultsByIndex[index] = testsInAssembly;
}
catch (Exception ex)
{
// Create a failed test metadata for the assembly that couldn't be scanned
var failedTest = CreateFailedTestMetadataForAssembly(assembly, ex);
resultsByIndex[index] =
[
failedTest
];
}
});
try
{
Console.WriteLine($"Scanning assembly: {assembly.GetName().Name}");
// Now we can properly await the async method
var testsInAssembly = await DiscoverTestsInAssembly(assembly).ConfigureAwait(false);
return testsInAssembly;
}
catch (Exception ex)
{
// Create a failed test metadata for the assembly that couldn't be scanned
var failedTest = CreateFailedTestMetadataForAssembly(assembly, ex);
return new List<TestMetadata> { failedTest };
}
});
}

// Wait for all tasks to complete
var results = await Task.WhenAll(tasks).ConfigureAwait(false);

// Reassemble results in original order
var newTests = new List<TestMetadata>();
for (var i = 0; i < assemblies.Count; i++)
foreach (var tests in results)
{
if (resultsByIndex.TryGetValue(i, out var tests))
{
newTests.AddRange(tests);
}
newTests.AddRange(tests);
}

// Discover dynamic tests from DynamicTestBuilderAttribute methods
Expand Down
10 changes: 8 additions & 2 deletions TUnit.Engine/Events/EventBatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,18 @@ public async ValueTask DisposeAsync()

try
{
// Properly await the task instead of blocking
// Properly await the task with timeout
#if NET6_0_OR_GREATER
await _processingTask.WaitAsync(TimeSpan.FromSeconds(5), CancellationToken.None);
#else
// For .NET Framework, use Task.WhenAny to implement timeout
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
await _processingTask.ConfigureAwait(false);
var completedTask = await Task.WhenAny(_processingTask, Task.Delay(Timeout.Infinite, cts.Token)).ConfigureAwait(false);
if (completedTask == _processingTask)
{
await _processingTask.ConfigureAwait(false);
}
// If timeout occurred, we just continue without waiting
#endif
}
catch
Expand Down
19 changes: 9 additions & 10 deletions TUnit.Engine/Services/SingleTestExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -446,19 +446,18 @@ private Func<ValueTask> CreateTimeoutTestAction(AbstractExecutableTest test, obj
return async () =>
{
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter((int)test.Context.TestDetails.Timeout!.Value.TotalMilliseconds);
var timeoutMs = (int)test.Context.TestDetails.Timeout!.Value.TotalMilliseconds;
cts.CancelAfter(timeoutMs);

var testTask = test.InvokeTestAsync(instance, cts.Token);
var timeoutTask = Task.Delay((int)test.Context.TestDetails.Timeout!.Value.TotalMilliseconds, cancellationToken);
var completedTask = await Task.WhenAny(testTask, timeoutTask);

if (completedTask == timeoutTask)
try
{
cts.Cancel();
throw new OperationCanceledException($"Test '{test.Context.GetDisplayName()}' exceeded timeout of {(int)test.Context.TestDetails.Timeout!.Value.TotalMilliseconds}ms");
await test.InvokeTestAsync(instance, cts.Token);
}
catch (OperationCanceledException) when (cts.IsCancellationRequested && !cancellationToken.IsCancellationRequested)
{
// This was our timeout, not an external cancellation
throw new System.TimeoutException($"Test '{test.Context.GetDisplayName()}' exceeded timeout of {timeoutMs}ms");
}

await testTask;
};
}

Expand Down