Copilot commented on code in PR #7675:
URL: https://github.com/apache/shenyu/pull/7675#discussion_r4291633020
##########
shenyu-plugin/shenyu-plugin-agent-gateway/src/test/java/org/apache/shenyu/plugin/agent/gateway/remote/RemoteCatalogLifecycleTest.java:
##########
@@ -298,10 +298,48 @@ void
withdrawalAlsoRejectsCandidateCurrentlyInitializing() {
assertEquals(1, client.closes.get());
}
+ @Test
+ void refreshConcurrencyOutsideBudgetIsRejected() {
+ assertThrows(IllegalArgumentException.class, () -> new
RemoteCatalogLifecycle(3, 0));
+ assertThrows(IllegalArgumentException.class, () -> new
RemoteCatalogLifecycle(3, 9));
+ }
+
+ @Test
+ void boundedParallelRefreshPublishesEveryTargetDeterministically() {
+ RemoteCatalogLifecycle manager = new RemoteCatalogLifecycle(3, 4);
+ Fake alpha = new Fake("alpha");
+ Fake beta = new Fake("beta");
+ manager.refresh(List.of(config("alpha", alpha), config("beta",
beta))).block(WAIT);
+ Set<String> exposed = Set.of("alpha.lookup", "alpha.lookup.detail",
"beta.lookup", "beta.lookup.detail");
+ assertEquals(exposed, manager.visible(exposed, exposed));
+ assertEquals(2, manager.diagnosticsState().clients());
+ manager.closeGracefully().block(WAIT);
+ assertEquals(1, alpha.closes.get());
+ assertEquals(1, beta.closes.get());
+ }
+
+ @Test
+ void parallelRefreshClosesEveryAllocatedClientWhenOneTargetFails() {
+ RemoteCatalogLifecycle manager = new RemoteCatalogLifecycle(3, 4);
+ Fake good = new Fake("good");
+ Fake bad = new Fake("bad");
+ bad.failHandshake = true;
+ assertThrows(RuntimeException.class, () ->
manager.refresh(List.of(config("good", good), config("bad", bad))).block(WAIT));
Review Comment:
The good handshake completes synchronously before the bad handshake starts,
so this test never exercises cancellation of an in-flight sibling. Keep
`good.handshake` pending with the existing sink fixture, then verify that
failure cancels its subscription as well as closing both clients without
publication. This covers the cleanup case introduced by parallel initialization.
##########
shenyu-plugin/shenyu-plugin-agent-gateway/src/test/java/org/apache/shenyu/plugin/agent/gateway/remote/RemoteToolDirectoryTest.java:
##########
@@ -0,0 +1,125 @@
+/*
+ * 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.shenyu.plugin.agent.gateway.remote;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import io.modelcontextprotocol.spec.McpSchema;
+import org.junit.jupiter.api.Test;
+import reactor.core.publisher.Mono;
+import reactor.core.scheduler.Schedulers;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class RemoteToolDirectoryTest {
+
+ private static final Duration WAIT = Duration.ofSeconds(5);
+
+ private static final ObjectMapper JSON = new ObjectMapper();
+
+ @Test
+ void serialAndBoundedDiscoveryProduceIdenticalDirectories() {
+ List<RemoteToolDirectory.Target> source = List.of(target("alpha"),
target("beta"), target("gamma"));
+ RemoteToolDirectory serial = RemoteToolDirectory.discover(source, 4,
16, 1).block(WAIT);
+ RemoteToolDirectory bounded = RemoteToolDirectory.discover(source, 4,
16, 3).block(WAIT);
+ assertNotNull(serial);
+ assertNotNull(bounded);
+ assertEquals(serial.names(), bounded.names());
+ assertEquals(Set.of("alpha.lookup", "beta.lookup", "gamma.lookup"),
bounded.names());
+ }
+
+ @Test
+ void boundedConcurrencyOverlapsIndependentTargetDiscovery() {
+ int targets = 4;
+ // Every target blocks until all four have been subscribed; only
genuine overlap can release the barrier.
+ CountDownLatch barrier = new CountDownLatch(targets);
+ List<RemoteToolDirectory.Target> source = new ArrayList<>();
+ for (int index = 0; index < targets; index++) {
+ String name = "srv" + index;
+ source.add(new RemoteToolDirectory.Target(
+ name,
+ cursor -> Mono.fromCallable(() -> {
+ barrier.countDown();
+ assertTrue(barrier.await(3, TimeUnit.SECONDS), "targets
were not discovered concurrently");
+ return page("lookup");
+ }).subscribeOn(Schedulers.boundedElastic()),
+ request -> Mono.error(new UnsupportedOperationException()),
+ request -> Mono.error(new UnsupportedOperationException())
+ ));
+ }
+ RemoteToolDirectory directory = RemoteToolDirectory.discover(source,
4, 16, targets).block(WAIT);
+ assertNotNull(directory);
+ assertEquals(targets, directory.names().size());
+ assertEquals(0, barrier.getCount());
+ }
+
+ @Test
+ void defaultDiscoverOverloadStaysSerialized() {
+ List<RemoteToolDirectory.Target> source = List.of(target("alpha"),
target("beta"));
+ RemoteToolDirectory directory = RemoteToolDirectory.discover(source,
4, 16).block(WAIT);
+ assertNotNull(directory);
+ assertEquals(Set.of("alpha.lookup", "beta.lookup"), directory.names());
Review Comment:
Both targets return synchronous `Mono.just` results, and the assertions only
check names. This test would still pass if the default concurrency changed to
2–8. Hold the first page pending and assert that the second target is
subscribed only after the first completes, so the serialized-default
compatibility guarantee has a regression check.
--
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]