Croway commented on code in PR #24587: URL: https://github.com/apache/camel/pull/24587#discussion_r3559450546
########## components/camel-ai/camel-openai/src/test/java/org/apache/camel/component/openai/OpenAIEndpointMcpReconnectConcurrencyTest.java: ########## @@ -0,0 +1,241 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.openai; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.locks.ReentrantLock; + +import com.openai.models.chat.completions.ChatCompletionFunctionTool; +import io.modelcontextprotocol.client.McpSyncClient; +import io.modelcontextprotocol.spec.McpSchema; +import org.apache.camel.impl.DefaultCamelContext; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Concurrency regression tests for CAMEL-23957: MCP reconnect must not corrupt the shared endpoint tool state. + */ +class OpenAIEndpointMcpReconnectConcurrencyTest { + + private static final String SERVER = "test-server"; + private static final List<String> TOOL_NAMES = List.of("get_weather", "find_location"); + + private final AtomicInteger clientsCreated = new AtomicInteger(); + + private volatile List<String> serverTools = TOOL_NAMES; + + /** + * Overrides the client-creation seam so reconnects return mocks instead of opening real transports. + */ + private class TestEndpoint extends OpenAIEndpoint { + TestEndpoint(OpenAIComponent component) { + super("openai:chat-completion", component, new OpenAIConfiguration()); + } + + @Override + McpSyncClient createMcpClient(String serverName, Map<String, String> props) { + clientsCreated.incrementAndGet(); + return newMockClient(); + } + } + + private McpSyncClient newMockClient() { + McpSyncClient client = mock(McpSyncClient.class); + when(client.listTools()).thenReturn(McpSchema.ListToolsResult.builder(mockTools()).build()); + return client; + } + + private List<McpSchema.Tool> mockTools() { + return serverTools.stream() + .map(n -> McpSchema.Tool.builder(n, Map.of("type", "object")).description("mock " + n).build()) + .toList(); + } + + private TestEndpoint newEndpoint(McpSyncClient initialClient) { + DefaultCamelContext ctx = new DefaultCamelContext(); + OpenAIComponent component = new OpenAIComponent(); + component.setCamelContext(ctx); + TestEndpoint endpoint = new TestEndpoint(component); + endpoint.setCamelContext(ctx); + + endpoint.setMcpTools(new ArrayList<>(McpToolConverter.convert(mockTools()))); + + Map<String, McpSyncClient> clientMap = new ConcurrentHashMap<>(); + Map<String, String> toolToServer = new ConcurrentHashMap<>(); + for (String n : serverTools) { + clientMap.put(n, initialClient); + toolToServer.put(n, SERVER); + } + endpoint.setToolClientMap(clientMap); + endpoint.setToolToServerName(toolToServer); + endpoint.setReturnDirectTools(ConcurrentHashMap.newKeySet()); + + Map<String, Map<String, String>> serverConfigs = new ConcurrentHashMap<>(); + serverConfigs.put(SERVER, Map.of("transportType", "stdio")); + endpoint.setServerConfigs(serverConfigs); + + Map<String, ReentrantLock> locks = new ConcurrentHashMap<>(); + locks.put(SERVER, new ReentrantLock()); + endpoint.setMcpClientLocks(locks); + + return endpoint; + } + + @Test + void concurrentReadersAndReconnectsDoNotThrow() throws Exception { Review Comment: Coverage gap: all tests use a single server, so the cross-server lost-update race (see comment in `doReconnectMcpServer`) isn't exercised. A two-server variant of this test — reconnecting both servers concurrently and asserting each server's tools still map to a live client afterwards — would catch it. Nit: `assertEquals(reconnected, c)` on mocks relies on identity equality; `assertSame` states the intent directly. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
