Skip to content

Commit 548bec1

Browse files
authored
test: stabilize asynchronous Admin CI regressions (#8348)
1 parent d7e8282 commit 548bec1

2 files changed

Lines changed: 24 additions & 13 deletions

File tree

‎shenyu-admin/src/test/java/org/apache/shenyu/admin/discovery/ZookeeperClientDiscoveryIntegrationTest.java‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.shenyu.admin.discovery;
1919

2020
import com.google.gson.JsonObject;
21+
import com.sun.net.httpserver.HttpServer;
2122
import jakarta.annotation.Resource;
2223
import org.apache.curator.test.TestingServer;
2324
import org.apache.shenyu.admin.AbstractSpringIntegrationTest;
@@ -42,7 +43,7 @@
4243

4344
import java.io.InputStream;
4445
import java.io.IOException;
45-
import java.net.ServerSocket;
46+
import java.net.InetSocketAddress;
4647
import java.net.URI;
4748
import java.net.http.HttpClient;
4849
import java.net.http.HttpRequest;
@@ -81,7 +82,7 @@ public class ZookeeperClientDiscoveryIntegrationTest extends AbstractSpringInteg
8182

8283
private final List<ShenyuInstanceRegisterRepository> clients = new CopyOnWriteArrayList<>();
8384

84-
private final List<ServerSocket> backends = new CopyOnWriteArrayList<>();
85+
private final List<HttpServer> backends = new CopyOnWriteArrayList<>();
8586

8687
private final List<DiscoverySyncData> snapshots = new CopyOnWriteArrayList<>();
8788

@@ -95,8 +96,8 @@ public void cleanup() throws IOException {
9596
websocket.abort();
9697
}
9798
clients.forEach(this::stopInstance);
98-
for (ServerSocket backend : backends) {
99-
backend.close();
99+
for (HttpServer backend : backends) {
100+
backend.stop(0);
100101
}
101102
List<DiscoveryDO> discoveries = registeredDiscoveries();
102103
discoveries.forEach(discovery -> processorHolder.chooseProcessor("zookeeper").removeDiscovery(discovery));
@@ -180,8 +181,20 @@ public CompletionStage<?> onText(final WebSocket socket, final CharSequence data
180181

181182
private ShenyuInstanceRegisterRepository startInstance(final TestingServer server, final String app, final int port) throws Exception {
182183
// The Admin health checker remains active while testing registry removal.
183-
// Keep registered backends reachable so health eviction cannot replace that event.
184-
backends.add(new ServerSocket(port));
184+
// Answer document pulls too: a listening socket alone blocks the registry event thread.
185+
HttpServer backend = HttpServer.create(new InetSocketAddress("127.0.0.1", port), 0);
186+
backend.createContext("/", exchange -> {
187+
byte[] document = "{\"openapi\":\"3.0.3\",\"info\":{\"title\":\"Discovery fixture\",\"version\":\"1\"},\"paths\":{}}".getBytes(StandardCharsets.UTF_8);
188+
exchange.getResponseHeaders().set("Content-Type", "application/json");
189+
exchange.sendResponseHeaders(200, document.length);
190+
try (java.io.OutputStream output = exchange.getResponseBody()) {
191+
output.write(document);
192+
} finally {
193+
exchange.close();
194+
}
195+
});
196+
backends.add(backend);
197+
backend.start();
185198
String appName = "zk-6525-" + app;
186199
Properties properties = new Properties();
187200
properties.setProperty("name", appName);

‎shenyu-admin/src/test/java/org/apache/shenyu/admin/mode/cluster/service/ShenyuClusterServiceTest.java‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.concurrent.TimeUnit;
3838
import java.util.concurrent.atomic.AtomicInteger;
3939

40+
import static org.awaitility.Awaitility.await;
4041
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
4142
import static org.junit.jupiter.api.Assertions.assertEquals;
4243
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -226,15 +227,12 @@ public void testSelectMasterFailureWaitsForNextPeriod() throws InterruptedExcept
226227

227228
@Test
228229
public void testSelectMasterErrorCountTracking() throws Exception {
229-
CountDownLatch errorsOccurred = new CountDownLatch(2);
230-
when(clusterSelectMasterService.selectMaster(anyString(), anyString(), anyString())).thenAnswer(invocation -> {
231-
errorsOccurred.countDown();
232-
throw new RuntimeException("consecutive failure");
233-
});
230+
when(clusterSelectMasterService.selectMaster(anyString(), anyString(), anyString()))
231+
.thenThrow(new RuntimeException("consecutive failure"));
234232

235233
clusterService.start("127.0.0.1", 9195, "/shenyu");
236-
assertTrue(errorsOccurred.await(5, TimeUnit.SECONDS));
237-
assertTrue(clusterService.getSelectMasterErrorCount() >= 2);
234+
// Wait for the catch block to update the count, rather than for entry into the mock.
235+
await().atMost(5, TimeUnit.SECONDS).until(() -> clusterService.getSelectMasterErrorCount() >= 2);
238236

239237
clusterService.shutdown();
240238
}

0 commit comments

Comments
 (0)