Skip to content

Commit 527643e

Browse files
committed
use AtomicBooleans to capture state of transportContext and responseBody to peform assertions within JUnit test thread; since callHandler may run on a different thread
1 parent 00b092d commit 527643e

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

mcp/src/test/java/io/modelcontextprotocol/server/AbstractMcpClientServerIntegrationTests.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
1111
import static org.assertj.core.api.Assertions.assertWith;
1212
import static org.awaitility.Awaitility.await;
13-
import static org.junit.jupiter.api.Assertions.assertTrue;
13+
import static org.junit.jupiter.api.Assertions.assertFalse;
1414
import static org.mockito.Mockito.mock;
1515

1616
import java.net.URI;
@@ -24,12 +24,14 @@
2424
import java.util.concurrent.CopyOnWriteArrayList;
2525
import java.util.concurrent.CountDownLatch;
2626
import java.util.concurrent.TimeUnit;
27+
import java.util.concurrent.atomic.AtomicBoolean;
2728
import java.util.concurrent.atomic.AtomicReference;
2829
import java.util.function.BiFunction;
2930
import java.util.function.Function;
3031
import java.util.stream.Collectors;
3132

3233
import jakarta.servlet.http.HttpServletRequest;
34+
import org.assertj.core.util.Strings;
3335
import org.junit.jupiter.params.ParameterizedTest;
3436
import org.junit.jupiter.params.provider.ValueSource;
3537

@@ -748,6 +750,7 @@ void testToolCallSuccess(String clientType) {
748750

749751
var clientBuilder = clientBuilders.get(clientType);
750752

753+
var responseBodyIsNullOrBlank = new AtomicBoolean(false);
751754
var callResponse = new McpSchema.CallToolResult(List.of(new McpSchema.TextContent("CALL RESPONSE")), null);
752755
McpServerFeatures.SyncToolSpecification tool1 = McpServerFeatures.SyncToolSpecification.builder()
753756
.tool(Tool.builder().name("tool1").description("tool1 description").inputSchema(emptyJsonSchema).build())
@@ -761,7 +764,7 @@ void testToolCallSuccess(String clientType) {
761764
.GET()
762765
.build(), HttpResponse.BodyHandlers.ofString());
763766
String responseBody = response.body();
764-
assertThat(responseBody).isNotBlank();
767+
responseBodyIsNullOrBlank.set(Strings.isNullOrEmpty(responseBody));
765768
}
766769
catch (Exception e) {
767770
e.printStackTrace();
@@ -784,6 +787,7 @@ void testToolCallSuccess(String clientType) {
784787

785788
CallToolResult response = mcpClient.callTool(new McpSchema.CallToolRequest("tool1", Map.of()));
786789

790+
assertFalse(responseBodyIsNullOrBlank.get(), "Response body should not be blank");
787791
assertThat(response).isNotNull().isEqualTo(callResponse);
788792
}
789793

@@ -833,15 +837,19 @@ void testToolCallSuccessWithTranportContextExtraction(String clientType) {
833837

834838
var clientBuilder = clientBuilders.get(clientType);
835839

840+
var transportContextIsNull = new AtomicBoolean(false);
841+
var transportContextIsEmpty = new AtomicBoolean(false);
842+
var responseBodyIsNullOrBlank = new AtomicBoolean(false);
843+
836844
var expectedCallResponse = new McpSchema.CallToolResult(
837845
List.of(new McpSchema.TextContent("CALL RESPONSE; ctx=value")), null);
838846
McpServerFeatures.SyncToolSpecification tool1 = McpServerFeatures.SyncToolSpecification.builder()
839847
.tool(Tool.builder().name("tool1").description("tool1 description").inputSchema(emptyJsonSchema).build())
840848
.callHandler((exchange, request) -> {
841849

842850
McpTransportContext transportContext = exchange.transportContext();
843-
assertTrue(transportContext != null, "transportContext should not be null");
844-
assertTrue(!transportContext.equals(McpTransportContext.EMPTY), "transportContext should not be empty");
851+
transportContextIsNull.set(transportContext == null);
852+
transportContextIsEmpty.set(transportContext.equals(McpTransportContext.EMPTY));
845853
String ctxValue = (String) transportContext.get("important");
846854

847855
try {
@@ -852,7 +860,7 @@ void testToolCallSuccessWithTranportContextExtraction(String clientType) {
852860
.GET()
853861
.build(), HttpResponse.BodyHandlers.ofString());
854862
String responseBody = response.body();
855-
assertThat(responseBody).isNotBlank();
863+
responseBodyIsNullOrBlank.set(Strings.isNullOrEmpty(responseBody));
856864
}
857865
catch (Exception e) {
858866
e.printStackTrace();
@@ -876,6 +884,9 @@ void testToolCallSuccessWithTranportContextExtraction(String clientType) {
876884

877885
CallToolResult response = mcpClient.callTool(new McpSchema.CallToolRequest("tool1", Map.of()));
878886

887+
assertFalse(transportContextIsNull.get(), "transportContext should not be null");
888+
assertFalse(transportContextIsEmpty.get(), "transportContext should not be empty");
889+
assertFalse(responseBodyIsNullOrBlank.get(), "Response body should not be blank");
879890
assertThat(response).isNotNull().isEqualTo(expectedCallResponse);
880891
}
881892

0 commit comments

Comments
 (0)