Skip to content

Commit 5b189af

Browse files
authored
Fix Session_TracksActivities test (#1200)
1 parent e4692fa commit 5b189af

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

tests/ModelContextProtocol.Tests/DiagnosticTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ await RunConnected(async (client, server) =>
3232
var tool = tools.First(t => t.Name == "DoubleValue");
3333
await tool.InvokeAsync(new() { ["amount"] = 42 }, TestContext.Current.CancellationToken);
3434
}, clientToServerLog);
35+
36+
// Wait for server-side activities to be exported. The server processes messages
37+
// via fire-and-forget tasks, so activities may not be immediately available
38+
// after the client operation completes.
39+
await WaitForAsync(() => activities.Count(a => a.Kind == ActivityKind.Server) >= 4);
3540
}
3641

3742
Assert.NotEmpty(activities);
@@ -97,6 +102,9 @@ await RunConnected(async (client, server) =>
97102
await client.CallToolAsync("Throw", cancellationToken: TestContext.Current.CancellationToken);
98103
await Assert.ThrowsAsync<McpProtocolException>(async () => await client.CallToolAsync("does-not-exist", cancellationToken: TestContext.Current.CancellationToken));
99104
}, []);
105+
106+
// Wait for server-side activities to be exported.
107+
await WaitForAsync(() => activities.Count(a => a.Kind == ActivityKind.Server) >= 4);
100108
}
101109

102110
Assert.NotEmpty(activities);
@@ -164,6 +172,9 @@ await RunConnected(async (client, server) =>
164172
.First(t => t.Name == "DoubleValue");
165173
await tool.InvokeAsync(new() { ["amount"] = 42 }, TestContext.Current.CancellationToken);
166174
}, []);
175+
176+
// Wait for server-side activities to be exported.
177+
await WaitForAsync(() => activities.Count(a => a.Kind == ActivityKind.Server) >= 3);
167178
}
168179

169180
// The outer activity should have MCP-specific attributes added to it
@@ -215,6 +226,15 @@ private static async Task RunConnected(Func<McpClient, McpServer, Task> action,
215226

216227
await serverTask;
217228
}
229+
230+
private static async Task WaitForAsync(Func<bool> condition, int timeoutMs = 10_000)
231+
{
232+
using var cts = new CancellationTokenSource(timeoutMs);
233+
while (!condition())
234+
{
235+
await Task.Delay(10, cts.Token);
236+
}
237+
}
218238
}
219239

220240
public class LoggingStream : Stream

0 commit comments

Comments
 (0)