|
| 1 | +package io.quarkus.websockets.next.test.connection; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | + |
| 6 | +import java.net.URI; |
| 7 | +import java.util.concurrent.CountDownLatch; |
| 8 | +import java.util.concurrent.ExecutorService; |
| 9 | +import java.util.concurrent.Executors; |
| 10 | +import java.util.concurrent.TimeUnit; |
| 11 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 12 | + |
| 13 | +import jakarta.inject.Inject; |
| 14 | + |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 17 | + |
| 18 | +import io.quarkus.test.QuarkusUnitTest; |
| 19 | +import io.quarkus.test.common.http.TestHTTPResource; |
| 20 | +import io.quarkus.websockets.next.OnClose; |
| 21 | +import io.quarkus.websockets.next.OnOpen; |
| 22 | +import io.quarkus.websockets.next.OnTextMessage; |
| 23 | +import io.quarkus.websockets.next.WebSocket; |
| 24 | +import io.quarkus.websockets.next.WebSocketClient; |
| 25 | +import io.quarkus.websockets.next.WebSocketClientConnection; |
| 26 | +import io.quarkus.websockets.next.WebSocketConnector; |
| 27 | +import io.quarkus.websockets.next.test.utils.WSClient; |
| 28 | + |
| 29 | +public class ConnectionIdleTimeoutTest { |
| 30 | + |
| 31 | + @RegisterExtension |
| 32 | + public static final QuarkusUnitTest test = new QuarkusUnitTest() |
| 33 | + .withApplicationRoot(root -> { |
| 34 | + root.addClasses(ServerEndpoint.class, ClientEndpoint.class, WSClient.class); |
| 35 | + }).overrideConfigKey("quarkus.websockets-next.client.connection-idle-timeout", "500ms");; |
| 36 | + |
| 37 | + @TestHTTPResource("/") |
| 38 | + URI uri; |
| 39 | + |
| 40 | + @Inject |
| 41 | + WebSocketConnector<ClientEndpoint> connector; |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testTimeout() throws InterruptedException { |
| 45 | + WebSocketClientConnection conn = connector.baseUri(uri.toString()).connectAndAwait(); |
| 46 | + ExecutorService executor = Executors.newSingleThreadExecutor(); |
| 47 | + try { |
| 48 | + TimeUnit.MILLISECONDS.sleep(500); |
| 49 | + executor.execute(() -> { |
| 50 | + try { |
| 51 | + conn.sendTextAndAwait("ok"); |
| 52 | + } catch (Throwable ignored) { |
| 53 | + } |
| 54 | + }); |
| 55 | + } finally { |
| 56 | + executor.shutdownNow(); |
| 57 | + } |
| 58 | + assertTrue(ServerEndpoint.CLOSED.await(5, TimeUnit.SECONDS)); |
| 59 | + assertTrue(ClientEndpoint.CLOSED.await(5, TimeUnit.SECONDS)); |
| 60 | + assertFalse(ServerEndpoint.MESSAGE.get()); |
| 61 | + } |
| 62 | + |
| 63 | + @WebSocket(path = "/end") |
| 64 | + public static class ServerEndpoint { |
| 65 | + |
| 66 | + static final CountDownLatch CLOSED = new CountDownLatch(1); |
| 67 | + static final AtomicBoolean MESSAGE = new AtomicBoolean(); |
| 68 | + |
| 69 | + @OnTextMessage |
| 70 | + void onText(String message) { |
| 71 | + MESSAGE.set(true); |
| 72 | + } |
| 73 | + |
| 74 | + @OnClose |
| 75 | + void close() { |
| 76 | + CLOSED.countDown(); |
| 77 | + } |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + @WebSocketClient(path = "/end") |
| 82 | + public static class ClientEndpoint { |
| 83 | + |
| 84 | + static final CountDownLatch CLOSED = new CountDownLatch(1); |
| 85 | + |
| 86 | + @OnOpen |
| 87 | + void open() { |
| 88 | + } |
| 89 | + |
| 90 | + @OnClose |
| 91 | + void close(WebSocketClientConnection conn) { |
| 92 | + CLOSED.countDown(); |
| 93 | + } |
| 94 | + |
| 95 | + } |
| 96 | +} |
0 commit comments