Skip to content

Commit 259d8df

Browse files
Copilotstephentoub
andcommitted
Revert to original DisableParallelization collection, remove Activity.Current and ForceFlush changes
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent d92872a commit 259d8df

File tree

2 files changed

+7
-42
lines changed

2 files changed

+7
-42
lines changed

tests/ModelContextProtocol.Tests/DiagnosticTests.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@
99

1010
namespace ModelContextProtocol.Tests;
1111

12-
[Collection(nameof(ExclusiveExecution))]
12+
[Collection(nameof(DisableParallelization))]
1313
public class DiagnosticTests
1414
{
1515
[Fact]
1616
public async Task Session_TracksActivities()
1717
{
18-
// Ensure no ambient activity from previous tests
19-
Activity.Current = null;
20-
2118
var activities = new List<Activity>();
2219
var clientToServerLog = new List<string>();
2320

@@ -35,9 +32,6 @@ await RunConnected(async (client, server) =>
3532
var tool = tools.First(t => t.Name == "DoubleValue");
3633
await tool.InvokeAsync(new() { ["amount"] = 42 }, TestContext.Current.CancellationToken);
3734
}, clientToServerLog);
38-
39-
// Ensure all activities are flushed before disposal
40-
tracerProvider.ForceFlush();
4135
}
4236

4337
Assert.NotEmpty(activities);
@@ -83,9 +77,6 @@ await RunConnected(async (client, server) =>
8377
[Fact]
8478
public async Task Session_FailedToolCall()
8579
{
86-
// Ensure no ambient activity from previous tests
87-
Activity.Current = null;
88-
8980
var activities = new List<Activity>();
9081

9182
using (var tracerProvider = OpenTelemetry.Sdk.CreateTracerProviderBuilder()
@@ -98,9 +89,6 @@ await RunConnected(async (client, server) =>
9889
await client.CallToolAsync("Throw", cancellationToken: TestContext.Current.CancellationToken);
9990
await Assert.ThrowsAsync<McpProtocolException>(async () => await client.CallToolAsync("does-not-exist", cancellationToken: TestContext.Current.CancellationToken));
10091
}, []);
101-
102-
// Ensure all activities are flushed before disposal
103-
tracerProvider.ForceFlush();
10492
}
10593

10694
Assert.NotEmpty(activities);
Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,9 @@
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 interact with global static state like ActivitySource in OpenTelemetry.
4-
/// </summary>
5-
/// <remarks>
6-
/// The fixture ensures mutual exclusion across ALL tests by using a static semaphore.
7-
/// Tests in this collection will acquire the semaphore before running and release it after,
8-
/// preventing any other test from running concurrently.
9-
/// </remarks>
10-
[CollectionDefinition(nameof(ExclusiveExecution))]
11-
public sealed class ExclusiveExecution : ICollectionFixture<ExclusiveExecutionFixture>;
1+
// Uncomment to disable parallel test execution for the whole assembly
2+
//[assembly: CollectionBehavior(DisableTestParallelization = true)]
123

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

0 commit comments

Comments
 (0)