Skip to content

Commit 57b9aaf

Browse files
Copilotstephentoub
andcommitted
Add ExclusiveExecution collection to prevent DiagnosticTests from running in parallel with tests that create activities
Co-authored-by: stephentoub <[email protected]>
1 parent 259d8df commit 57b9aaf

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

tests/ModelContextProtocol.Tests/DiagnosticTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace ModelContextProtocol.Tests;
1111

12-
[Collection(nameof(DisableParallelization))]
12+
[Collection(nameof(ExclusiveExecution))]
1313
public class DiagnosticTests
1414
{
1515
[Fact]
Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
1-
// Uncomment to disable parallel test execution for the whole assembly
2-
//[assembly: CollectionBehavior(DisableTestParallelization = true)]
1+
/// <summary>
2+
/// Test collection for tests that require exclusive execution (no parallel execution with any other tests).
3+
/// This is needed for tests that use OpenTelemetry TracerProvider with the global static ActivitySource.
4+
/// Without this, the TracerProvider would collect activities from ALL tests running in parallel.
5+
/// </summary>
6+
/// <remarks>
7+
/// The fixture ensures mutual exclusion across ALL tests by using a static semaphore.
8+
/// Tests in this collection will acquire the semaphore before running and release it after,
9+
/// preventing any other test from running concurrently.
10+
/// </remarks>
11+
[CollectionDefinition(nameof(ExclusiveExecution))]
12+
public sealed class ExclusiveExecution : ICollectionFixture<ExclusiveExecutionFixture>;
313

414
/// <summary>
5-
/// Enables test classes to individually be attributed as [Collection(nameof(DisableParallelization))]
6-
/// to have those tests run non-concurrently with any other tests.
15+
/// Fixture that ensures only one test runs at a time across the entire test assembly.
716
/// </summary>
8-
[CollectionDefinition(nameof(DisableParallelization), DisableParallelization = true)]
9-
public sealed class DisableParallelization;
17+
public sealed class ExclusiveExecutionFixture : IAsyncLifetime
18+
{
19+
private static readonly SemaphoreSlim s_exclusiveLock = new(1, 1);
20+
21+
public async ValueTask InitializeAsync()
22+
{
23+
// Acquire the lock before any test in this collection starts
24+
await s_exclusiveLock.WaitAsync();
25+
}
26+
27+
public ValueTask DisposeAsync()
28+
{
29+
// Release the lock after the test completes
30+
s_exclusiveLock.Release();
31+
return default;
32+
}
33+
}

0 commit comments

Comments
 (0)