|
| 1 | +package io.quarkus.websockets.next.test.errors; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | + |
| 6 | +import java.net.URI; |
| 7 | +import java.time.Duration; |
| 8 | +import java.util.List; |
| 9 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 10 | +import java.util.concurrent.CountDownLatch; |
| 11 | +import java.util.concurrent.TimeUnit; |
| 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.logging.Log; |
| 19 | +import io.quarkus.test.QuarkusUnitTest; |
| 20 | +import io.quarkus.test.common.http.TestHTTPResource; |
| 21 | +import io.quarkus.websockets.next.OnError; |
| 22 | +import io.quarkus.websockets.next.OnOpen; |
| 23 | +import io.quarkus.websockets.next.WebSocket; |
| 24 | +import io.quarkus.websockets.next.WebSocketConnection; |
| 25 | +import io.quarkus.websockets.next.test.utils.WSClient; |
| 26 | +import io.smallrye.mutiny.Multi; |
| 27 | +import io.vertx.core.Vertx; |
| 28 | +import io.vertx.core.impl.NoStackTraceThrowable; |
| 29 | + |
| 30 | +public class MultiClosedConnectionTest { |
| 31 | + |
| 32 | + @RegisterExtension |
| 33 | + public static final QuarkusUnitTest test = new QuarkusUnitTest() |
| 34 | + .withApplicationRoot(root -> { |
| 35 | + root.addClasses(Echo.class, WSClient.class); |
| 36 | + }); |
| 37 | + |
| 38 | + @Inject |
| 39 | + Vertx vertx; |
| 40 | + |
| 41 | + @TestHTTPResource("echo") |
| 42 | + URI testUri; |
| 43 | + |
| 44 | + @Test |
| 45 | + void testError() throws InterruptedException { |
| 46 | + WSClient client = WSClient.create(vertx).connect(testUri); |
| 47 | + client.waitForMessages(1); |
| 48 | + client.close(); |
| 49 | + assertTrue(Echo.TERMINATION_LATCH.await(5, TimeUnit.SECONDS)); |
| 50 | + assertTrue(Echo.ERROR_LATCH.await(5, TimeUnit.SECONDS)); |
| 51 | + // Connection is closed and the returned Multi should be cancelled |
| 52 | + int numOfMessages = Echo.MESSAGES.size(); |
| 53 | + Thread.sleep(600); |
| 54 | + // No more ticks are emitted |
| 55 | + assertEquals(numOfMessages, Echo.MESSAGES.size()); |
| 56 | + } |
| 57 | + |
| 58 | + @WebSocket(path = "/echo") |
| 59 | + public static class Echo { |
| 60 | + |
| 61 | + static final CountDownLatch TERMINATION_LATCH = new CountDownLatch(1); |
| 62 | + static final CountDownLatch ERROR_LATCH = new CountDownLatch(1); |
| 63 | + |
| 64 | + static final List<String> MESSAGES = new CopyOnWriteArrayList<>(); |
| 65 | + |
| 66 | + @OnOpen |
| 67 | + Multi<String> onOpen() { |
| 68 | + return Multi.createFrom() |
| 69 | + .ticks() |
| 70 | + .every(Duration.ofMillis(300)) |
| 71 | + .map(tick -> tick + "") |
| 72 | + .invoke(s -> { |
| 73 | + Log.infof("Next tick: %s", s); |
| 74 | + MESSAGES.add(s); |
| 75 | + }) |
| 76 | + .onTermination() |
| 77 | + .invoke(() -> { |
| 78 | + Log.info("Terminated!"); |
| 79 | + TERMINATION_LATCH.countDown(); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + @OnError |
| 84 | + void onConnectionClosedError(NoStackTraceThrowable t, WebSocketConnection conn) { |
| 85 | + Log.info("Error callback!"); |
| 86 | + if (conn.isClosed()) { |
| 87 | + String message = t.getMessage(); |
| 88 | + if (message != null && message.contains("WebSocket is closed")) { |
| 89 | + ERROR_LATCH.countDown(); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments