}
}
tomcat.start();
Session wsSession = wsContainer.connectToServer(
TesterProgrammaticEndpoint.class,
ClientEndpointConfig.Builder.create().build(),
new URI("ws://localhost:" + getPort() +
TesterEchoServer.Config.PATH_BASIC));
BasicHandler<?> handler;
CountDownLatch latch = new CountDownLatch(1);
TesterEndpoint tep =
(TesterEndpoint) wsSession.getUserProperties().get("endpoint");
tep.setLatch(latch);
if (isTextMessage) {
handler = new BasicText(latch);
} else {
handler = new BasicBinary(latch);
}
wsSession.addMessageHandler(handler);
try {
if (isTextMessage) {
wsSession.getBasicRemote().sendText(MESSAGE_TEXT_4K);
} else {
wsSession.getBasicRemote().sendBinary(
ByteBuffer.wrap(MESSAGE_BINARY_4K));
}
} catch (IOException ioe) {
// Some messages sends are expected to fail. Assertions further on
// in this method will check for the correct behaviour so ignore any
// exception here.
}
boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
Assert.assertTrue(latchResult);
Queue<?> messages = handler.getMessages();
if (pass) {
Assert.assertEquals(1, messages.size());
if (isTextMessage) {
Assert.assertEquals(MESSAGE_TEXT_4K, messages.peek());
} else {
Assert.assertEquals(ByteBuffer.wrap(MESSAGE_BINARY_4K),
messages.peek());
}
} else {
// When the message exceeds the buffer size, the WebSocket is
// closed. The endpoint ensures that the latch is cleared when the
// WebSocket closes. However, the session isn't marked as closed
// until after the onClose() method completes so there is a small
// window where this test could fail. Therefore, wait briefly to
// give the session a chance to complete the close process.
for (int i = 0; i < 500; i++) {
if (!wsSession.isOpen()) {
break;
}
Thread.sleep(10);
}
Assert.assertFalse(wsSession.isOpen());
}
}