From f04fab7e7932de03029864bc86b07db89b507400 Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Tue, 7 May 2024 11:03:10 +0200 Subject: [PATCH] WebSockets Next: fix flaky BasicConnectorTest --- .../next/test/client/BasicConnectorTest.java | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/extensions/websockets-next/deployment/src/test/java/io/quarkus/websockets/next/test/client/BasicConnectorTest.java b/extensions/websockets-next/deployment/src/test/java/io/quarkus/websockets/next/test/client/BasicConnectorTest.java index fc22881e6ced4..6bf3377d2fba6 100644 --- a/extensions/websockets-next/deployment/src/test/java/io/quarkus/websockets/next/test/client/BasicConnectorTest.java +++ b/extensions/websockets-next/deployment/src/test/java/io/quarkus/websockets/next/test/client/BasicConnectorTest.java @@ -44,12 +44,6 @@ public class BasicConnectorTest { @TestHTTPResource("/end") URI uri; - static final CountDownLatch MESSAGE_LATCH = new CountDownLatch(2); - - static final List MESSAGES = new CopyOnWriteArrayList<>(); - - static final CountDownLatch CLOSED_LATCH = new CountDownLatch(1); - @Test void testClient() throws InterruptedException { @@ -68,6 +62,9 @@ void testClient() throws InterruptedException { assertThrows(NullPointerException.class, () -> connector.onPong(null)); assertThrows(NullPointerException.class, () -> connector.onError(null)); + CountDownLatch messageLatch = new CountDownLatch(2); + List messages = new CopyOnWriteArrayList<>(); + CountDownLatch closedLatch = new CountDownLatch(1); WebSocketClientConnection connection1 = connector .baseUri(uri) .path("/{name}") @@ -75,24 +72,24 @@ void testClient() throws InterruptedException { .onTextMessage((c, m) -> { assertTrue(Context.isOnWorkerThread()); String name = c.pathParam("name"); - MESSAGE_LATCH.countDown(); - MESSAGES.add(name + ":" + m); + messages.add(name + ":" + m); + messageLatch.countDown(); }) - .onClose((c, s) -> CLOSED_LATCH.countDown()) + .onClose((c, s) -> closedLatch.countDown()) .connectAndAwait(); assertEquals("Lu", connection1.pathParam("name")); connection1.sendTextAndAwait("Hi!"); - assertTrue(MESSAGE_LATCH.await(5, TimeUnit.SECONDS)); + assertTrue(messageLatch.await(5, TimeUnit.SECONDS)); // Note that ordering is not guaranteed - assertThat(MESSAGES.get(0)).isIn("Lu:Hello Lu!", "Lu:Hi!"); - assertThat(MESSAGES.get(1)).isIn("Lu:Hello Lu!", "Lu:Hi!"); + assertThat(messages.get(0)).isIn("Lu:Hello Lu!", "Lu:Hi!"); + assertThat(messages.get(1)).isIn("Lu:Hello Lu!", "Lu:Hi!"); connection1.closeAndAwait(); - assertTrue(CLOSED_LATCH.await(5, TimeUnit.SECONDS)); + assertTrue(closedLatch.await(5, TimeUnit.SECONDS)); assertTrue(ServerEndpoint.CLOSED_LATCH.await(5, TimeUnit.SECONDS)); - CountDownLatch CONN2_LATCH = new CountDownLatch(1); + CountDownLatch conn2Latch = new CountDownLatch(1); WebSocketClientConnection connection2 = BasicWebSocketConnector .create() .baseUri(uri) @@ -105,11 +102,11 @@ void testClient() throws InterruptedException { assertNull(c.pathParam("name")); assertTrue(c.handshakeRequest().path().endsWith("Cool")); assertEquals("foo", c.handshakeRequest().header("X-Test")); - CONN2_LATCH.countDown(); + conn2Latch.countDown(); }) .connectAndAwait(); assertNotNull(connection2); - assertTrue(CONN2_LATCH.await(5, TimeUnit.SECONDS)); + assertTrue(conn2Latch.await(5, TimeUnit.SECONDS)); } @WebSocket(path = "/end/{name}")