Skip to content

Commit 0078b15

Browse files
authored
Fix additional intermittent SockJS test failures (#2929)
In SockJSHandlerTest, setupSockJsClient blocked on receiving the SockJS "o" frame via a Promise. If the frame was lost, the test hung. Remove the dependency on receiving the "o" frame since it is not needed for the tests to function correctly. In SockJSRawTransportTest, disableHost and goodOrigin wrote data immediately in the socket handler. Use the same ready-message protocol as testWriteText so the server waits for the client to signal readiness. Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
1 parent 9f894fb commit 0078b15

2 files changed

Lines changed: 40 additions & 42 deletions

File tree

vertx-web/src/test/java/io/vertx/ext/web/tests/handler/sockjs/SockJSHandlerTest.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package io.vertx.ext.web.tests.handler.sockjs;
1818

1919
import io.vertx.core.MultiMap;
20-
import io.vertx.core.Promise;
2120
import io.vertx.core.Vertx;
2221
import io.vertx.core.buffer.Buffer;
2322
import io.vertx.core.http.*;
@@ -306,22 +305,17 @@ private void setupSockJsServer(String serverPath, BiConsumer<SockJSSocket, Buffe
306305
private WebSocket setupSockJsClient(String serverPath, List<Buffer> receivedMessagesCollector, Runnable onClose) {
307306
String requestURI = serverPath + "/000/000/websocket";
308307

309-
Promise<WebSocket> promise = Promise.promise();
310-
wsClient.connect(requestURI).onComplete(TestUtils.onSuccess(ws -> {
311-
ws.handler(replyBuffer -> {
312-
log.debug("Client received " + replyBuffer);
313-
String textReply = replyBuffer.toString();
314-
if ("o".equals(textReply)) {
315-
promise.complete(ws);
316-
} else {
317-
receivedMessagesCollector.add(replyBuffer);
318-
}
319-
});
320-
ws.endHandler(v -> onClose.run());
321-
ws.exceptionHandler(err -> fail(err.getMessage()));
322-
}));
323-
324-
return promise.future().await();
308+
WebSocket ws = wsClient.connect(requestURI).await();
309+
ws.handler(replyBuffer -> {
310+
log.debug("Client received " + replyBuffer);
311+
String textReply = replyBuffer.toString();
312+
if (!"o".equals(textReply)) {
313+
receivedMessagesCollector.add(replyBuffer);
314+
}
315+
});
316+
ws.endHandler(v -> onClose.run());
317+
ws.exceptionHandler(err -> fail(err.getMessage()));
318+
return ws;
325319
}
326320

327321
/**

vertx-web/src/test/java/io/vertx/ext/web/tests/handler/sockjs/SockJSRawTransportTest.java

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,30 @@ public void testWriteBinary(Checkpoint checkpoint) throws Exception {
4646
public void disableHost(Checkpoint checkpoint) throws Exception {
4747
String expected = TestUtils.randomAlphaString(64);
4848
socketHandler = () -> socket -> {
49-
socket.write(expected);
49+
socket.handler(msg -> {
50+
socket.write(expected);
51+
});
5052
socket.endHandler(v -> {
5153
checkpoint.flag();
5254
});
5355
};
5456
startServers(new SockJSHandlerOptions());
55-
wsClient.connect(
57+
WebSocket ws = wsClient.connect(
5658
new WebSocketConnectOptions()
5759
.setHost("localhost")
5860
.setPort(8080)
5961
.setURI("/test/websocket")
60-
.setAllowOriginHeader(false)).onComplete(TestUtils.onSuccess(ws -> {
61-
ws.frameHandler(frame -> {
62-
if (frame.isClose()) {
63-
//
64-
} else {
65-
assertTrue(frame.isText());
66-
assertEquals(expected, frame.textData());
67-
ws.end();
68-
}
69-
});
70-
}));
62+
.setAllowOriginHeader(false)).await();
63+
ws.frameHandler(frame -> {
64+
if (frame.isClose()) {
65+
//
66+
} else {
67+
assertTrue(frame.isText());
68+
assertEquals(expected, frame.textData());
69+
ws.end();
70+
}
71+
});
72+
ws.writeTextMessage("ready");
7173
}
7274

7375
@Test
@@ -92,23 +94,25 @@ public void disableHostFailWhenOriginIsRequired(Checkpoint checkpoint) throws Ex
9294
public void goodOrigin(Checkpoint checkpoint) throws Exception {
9395
String expected = TestUtils.randomAlphaString(64);
9496
socketHandler = () -> socket -> {
95-
socket.write(expected);
97+
socket.handler(msg -> {
98+
socket.write(expected);
99+
});
96100
socket.endHandler(v -> {
97101
checkpoint.flag();
98102
});
99103
};
100104
startServers(new SockJSHandlerOptions().setOrigin("http://localhost:8080"));
101-
wsClient.connect("/test/websocket").onComplete(TestUtils.onSuccess(ws -> {
102-
ws.frameHandler(frame -> {
103-
if (frame.isClose()) {
104-
//
105-
} else {
106-
assertTrue(frame.isText());
107-
assertEquals(expected, frame.textData());
108-
ws.end();
109-
}
110-
});
111-
}));
105+
WebSocket ws = wsClient.connect("/test/websocket").await();
106+
ws.frameHandler(frame -> {
107+
if (frame.isClose()) {
108+
//
109+
} else {
110+
assertTrue(frame.isText());
111+
assertEquals(expected, frame.textData());
112+
ws.end();
113+
}
114+
});
115+
ws.writeTextMessage("ready");
112116
}
113117

114118
@Test

0 commit comments

Comments
 (0)