Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSockets Next: fix flaky BasicConnectorTest #40491

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@ public class BasicConnectorTest {
@TestHTTPResource("/end")
URI uri;

static final CountDownLatch MESSAGE_LATCH = new CountDownLatch(2);

static final List<String> MESSAGES = new CopyOnWriteArrayList<>();

static final CountDownLatch CLOSED_LATCH = new CountDownLatch(1);

@Test
void testClient() throws InterruptedException {

Expand All @@ -68,31 +62,34 @@ void testClient() throws InterruptedException {
assertThrows(NullPointerException.class, () -> connector.onPong(null));
assertThrows(NullPointerException.class, () -> connector.onError(null));

CountDownLatch messageLatch = new CountDownLatch(2);
List<String> messages = new CopyOnWriteArrayList<>();
CountDownLatch closedLatch = new CountDownLatch(1);
WebSocketClientConnection connection1 = connector
.baseUri(uri)
.path("/{name}")
.pathParam("name", "Lu")
.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)
Expand All @@ -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}")
Expand Down