Skip to content

Follow-up fix client tests reliability #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -446,18 +446,9 @@ void testNotificationHandlers() {
resources -> Mono.fromRunnable(() -> resourcesNotificationReceived.set(true)))
.promptsChangeConsumer(prompts -> Mono.fromRunnable(() -> promptsNotificationReceived.set(true))),
mcpAsyncClient -> {

var transport = createMcpTransport();
var client = McpClient.async(transport)
.requestTimeout(getRequestTimeout())
.toolsChangeConsumer(tools -> Mono.fromRunnable(() -> toolsNotificationReceived.set(true)))
.resourcesChangeConsumer(
resources -> Mono.fromRunnable(() -> resourcesNotificationReceived.set(true)))
.promptsChangeConsumer(
prompts -> Mono.fromRunnable(() -> promptsNotificationReceived.set(true)))
.build();

StepVerifier.create(client.initialize()).expectNextMatches(Objects::nonNull).verifyComplete();
StepVerifier.create(mcpAsyncClient.initialize())
.expectNextMatches(Objects::nonNull)
.verifyComplete();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,20 @@ <T> void verifyNotificationTimesOut(Consumer<McpSyncClient> operation, String ac
}, action);
}

<T> void verifyCallTimesOut(Function<McpSyncClient, T> operation, String action) {
<T> void verifyCallTimesOut(Function<McpSyncClient, T> blockingOperation, String action) {
withClient(createMcpTransport(), mcpSyncClient -> {
// This scheduler is not replaced by virtual time scheduler
Scheduler customScheduler = Schedulers.newBoundedElastic(1, 1, "actualBoundedElastic");

StepVerifier.withVirtualTime(() -> Mono.fromSupplier(() -> operation.apply(mcpSyncClient))
// offload the blocking call to the real scheduler
StepVerifier.withVirtualTime(() -> Mono.fromSupplier(() -> blockingOperation.apply(mcpSyncClient))
// Offload the blocking call to the real scheduler
.subscribeOn(customScheduler))
.expectSubscription()
// This works without actually waiting but executes all the
// tasks pending execution on the VirtualTimeScheduler.
// It is possible to execute the blocking code from the operation
// because it is blocked on a dedicated Scheduler and the main
// flow is not blocked and uses the VirtualTimeScheduler.
.thenAwait(getInitializationTimeout())
.consumeErrorWith(e -> assertThat(e).isInstanceOf(McpError.class)
.hasMessage("Client must be initialized before " + action))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,18 +402,9 @@ void testNotificationHandlers() {
resources -> Mono.fromRunnable(() -> resourcesNotificationReceived.set(true)))
.promptsChangeConsumer(prompts -> Mono.fromRunnable(() -> promptsNotificationReceived.set(true))),
mcpAsyncClient -> {

var transport = createMcpTransport();
var client = McpClient.async(transport)
.requestTimeout(getRequestTimeout())
.toolsChangeConsumer(tools -> Mono.fromRunnable(() -> toolsNotificationReceived.set(true)))
.resourcesChangeConsumer(
resources -> Mono.fromRunnable(() -> resourcesNotificationReceived.set(true)))
.promptsChangeConsumer(
prompts -> Mono.fromRunnable(() -> promptsNotificationReceived.set(true)))
.build();

StepVerifier.create(client.initialize()).expectNextMatches(Objects::nonNull).verifyComplete();
StepVerifier.create(mcpAsyncClient.initialize())
.expectNextMatches(Objects::nonNull)
.verifyComplete();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,20 @@ <T> void verifyNotificationTimesOut(Consumer<McpSyncClient> operation, String ac
}, action);
}

<T> void verifyCallTimesOut(Function<McpSyncClient, T> operation, String action) {
<T> void verifyCallTimesOut(Function<McpSyncClient, T> blockingOperation, String action) {
withClient(createMcpTransport(), mcpSyncClient -> {
// This scheduler is not replaced by virtual time scheduler
Scheduler customScheduler = Schedulers.newBoundedElastic(1, 1, "actualBoundedElastic");

StepVerifier.withVirtualTime(() -> Mono.fromSupplier(() -> operation.apply(mcpSyncClient))
// offload the blocking call to the real scheduler
StepVerifier.withVirtualTime(() -> Mono.fromSupplier(() -> blockingOperation.apply(mcpSyncClient))
// Offload the blocking call to the real scheduler
.subscribeOn(customScheduler))
.expectSubscription()
// This works without actually waiting but executes all the
// tasks pending execution on the VirtualTimeScheduler.
// It is possible to execute the blocking code from the operation
// because it is blocked on a dedicated Scheduler and the main
// flow is not blocked and uses the VirtualTimeScheduler.
.thenAwait(getInitializationTimeout())
.consumeErrorWith(e -> assertThat(e).isInstanceOf(McpError.class)
.hasMessage("Client must be initialized before " + action))
Expand Down