Fix flaky AddIncomingMessageFilter_Multiple_Filters_Execute_In_Order test (#1627)
Co-authored-by: Tarek Mahmoud Sayed <tarekms@ntdev.microsoft.com>
This commit is contained in:
parent
7ba084acf8
commit
719eade5e0
1 changed files with 15 additions and 0 deletions
|
|
@ -100,6 +100,12 @@ public class McpServerBuilderExtensionsMessageFilterTests(ITestOutputHelper test
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task AddIncomingMessageFilter_Multiple_Filters_Execute_In_Order()
|
public async Task AddIncomingMessageFilter_Multiple_Filters_Execute_In_Order()
|
||||||
{
|
{
|
||||||
|
// The client sends notifications/initialized fire-and-forget, so unlike the initialize and
|
||||||
|
// tools/list request/response exchanges it has no synchronization point the test can await.
|
||||||
|
// Signal once the outermost filter finishes processing it so the strict counts below observe a
|
||||||
|
// complete, stable log instead of racing the still-in-flight notification.
|
||||||
|
var initializedNotificationProcessed = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||||
|
|
||||||
McpServerBuilder
|
McpServerBuilder
|
||||||
.WithMessageFilters(filters =>
|
.WithMessageFilters(filters =>
|
||||||
{
|
{
|
||||||
|
|
@ -109,6 +115,11 @@ public class McpServerBuilderExtensionsMessageFilterTests(ITestOutputHelper test
|
||||||
logger.LogInformation("MessageFilter1 before");
|
logger.LogInformation("MessageFilter1 before");
|
||||||
await next(context, cancellationToken);
|
await next(context, cancellationToken);
|
||||||
logger.LogInformation("MessageFilter1 after");
|
logger.LogInformation("MessageFilter1 after");
|
||||||
|
|
||||||
|
if (context.JsonRpcMessage is JsonRpcNotification { Method: NotificationMethods.InitializedNotification })
|
||||||
|
{
|
||||||
|
initializedNotificationProcessed.TrySetResult(true);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
filters.AddIncomingFilter((next) => async (context, cancellationToken) =>
|
filters.AddIncomingFilter((next) => async (context, cancellationToken) =>
|
||||||
|
|
@ -127,6 +138,10 @@ public class McpServerBuilderExtensionsMessageFilterTests(ITestOutputHelper test
|
||||||
|
|
||||||
await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
|
await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
|
// Wait for the fire-and-forget initialized notification to flow through the filter pipeline
|
||||||
|
// before snapshotting the log; otherwise the strict counts below can race the notification.
|
||||||
|
await initializedNotificationProcessed.Task.WaitAsync(TestConstants.DefaultTimeout, TestContext.Current.CancellationToken);
|
||||||
|
|
||||||
var logMessages = MockLoggerProvider.LogMessages
|
var logMessages = MockLoggerProvider.LogMessages
|
||||||
.Where(m => m.Category.StartsWith("MessageFilter"))
|
.Where(m => m.Category.StartsWith("MessageFilter"))
|
||||||
.Select(m => m.Message)
|
.Select(m => m.Message)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue