Skip to content

Commit e996a5d

Browse files
chemicLtzolov
authored andcommitted
Follow-up fix client tests reliability
Signed-off-by: Dariusz Jędrzejczyk <[email protected]>
1 parent 37120f2 commit e996a5d

File tree

4 files changed

+22
-30
lines changed

4 files changed

+22
-30
lines changed

mcp-test/src/main/java/io/modelcontextprotocol/client/AbstractMcpAsyncClientTests.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -446,18 +446,9 @@ void testNotificationHandlers() {
446446
resources -> Mono.fromRunnable(() -> resourcesNotificationReceived.set(true)))
447447
.promptsChangeConsumer(prompts -> Mono.fromRunnable(() -> promptsNotificationReceived.set(true))),
448448
mcpAsyncClient -> {
449-
450-
var transport = createMcpTransport();
451-
var client = McpClient.async(transport)
452-
.requestTimeout(getRequestTimeout())
453-
.toolsChangeConsumer(tools -> Mono.fromRunnable(() -> toolsNotificationReceived.set(true)))
454-
.resourcesChangeConsumer(
455-
resources -> Mono.fromRunnable(() -> resourcesNotificationReceived.set(true)))
456-
.promptsChangeConsumer(
457-
prompts -> Mono.fromRunnable(() -> promptsNotificationReceived.set(true)))
458-
.build();
459-
460-
StepVerifier.create(client.initialize()).expectNextMatches(Objects::nonNull).verifyComplete();
449+
StepVerifier.create(mcpAsyncClient.initialize())
450+
.expectNextMatches(Objects::nonNull)
451+
.verifyComplete();
461452
});
462453
}
463454

mcp-test/src/main/java/io/modelcontextprotocol/client/AbstractMcpSyncClientTests.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,20 @@ <T> void verifyNotificationTimesOut(Consumer<McpSyncClient> operation, String ac
119119
}, action);
120120
}
121121

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

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

mcp/src/test/java/io/modelcontextprotocol/client/AbstractMcpAsyncClientTests.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -402,18 +402,9 @@ void testNotificationHandlers() {
402402
resources -> Mono.fromRunnable(() -> resourcesNotificationReceived.set(true)))
403403
.promptsChangeConsumer(prompts -> Mono.fromRunnable(() -> promptsNotificationReceived.set(true))),
404404
mcpAsyncClient -> {
405-
406-
var transport = createMcpTransport();
407-
var client = McpClient.async(transport)
408-
.requestTimeout(getRequestTimeout())
409-
.toolsChangeConsumer(tools -> Mono.fromRunnable(() -> toolsNotificationReceived.set(true)))
410-
.resourcesChangeConsumer(
411-
resources -> Mono.fromRunnable(() -> resourcesNotificationReceived.set(true)))
412-
.promptsChangeConsumer(
413-
prompts -> Mono.fromRunnable(() -> promptsNotificationReceived.set(true)))
414-
.build();
415-
416-
StepVerifier.create(client.initialize()).expectNextMatches(Objects::nonNull).verifyComplete();
405+
StepVerifier.create(mcpAsyncClient.initialize())
406+
.expectNextMatches(Objects::nonNull)
407+
.verifyComplete();
417408
});
418409
}
419410

mcp/src/test/java/io/modelcontextprotocol/client/AbstractMcpSyncClientTests.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,20 @@ <T> void verifyNotificationTimesOut(Consumer<McpSyncClient> operation, String ac
120120
}, action);
121121
}
122122

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

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

0 commit comments

Comments
 (0)