use of javax.websocket.WebSocketContainer in project tomcat70 by apache.
the class TestCloseBug58624 method testOnErrorNotCalledWhenClosingConnection.
@Test
public void testOnErrorNotCalledWhenClosingConnection() throws Throwable {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ctx.addApplicationListener(Bug58624ServerConfig.class.getName());
Tomcat.addServlet(ctx, "default", new DefaultServlet());
ctx.addServletMapping("/", "default");
WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
tomcat.start();
Bug58624ClientEndpoint client = new Bug58624ClientEndpoint();
URI uri = new URI("ws://localhost:" + getPort() + Bug58624ServerConfig.PATH);
Session session = wsContainer.connectToServer(client, uri);
// Wait for session to open on the server
int count = 0;
while (count < 50 && Bug58624ServerEndpoint.getOpenSessionCount() == 0) {
count++;
Thread.sleep(100);
}
Assert.assertNotEquals(0, Bug58624ServerEndpoint.getOpenSessionCount());
// Now close the session
session.close();
// Wait for session to close on the server
count = 0;
while (count < 50 && Bug58624ServerEndpoint.getOpenSessionCount() > 0) {
count++;
Thread.sleep(100);
}
Assert.assertEquals(0, Bug58624ServerEndpoint.getOpenSessionCount());
// Ensure no errors were reported on the server
Assert.assertEquals(0, Bug58624ServerEndpoint.getErrorCount());
if (client.getError() != null) {
throw client.getError();
}
}
use of javax.websocket.WebSocketContainer in project knox by apache.
the class BadSocket method testDroppedConnection.
/**
* The connection is dropped, so we should see a tineout exception when we try
* to poll the message from queue.
*
* @throws IOException
* @throws Exception
*/
@Test(expected = java.util.concurrent.TimeoutException.class)
public void testDroppedConnection() throws IOException, Exception {
final String message = "Echo";
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
WebsocketClient client = new WebsocketClient();
javax.websocket.Session session = container.connectToServer(client, proxyUri);
session.getBasicRemote().sendText(message);
client.messageQueue.awaitMessages(1, 1000, TimeUnit.MILLISECONDS);
}
use of javax.websocket.WebSocketContainer in project knox by apache.
the class BigEchoSocketHandler method testMessageTooBig.
/**
* Test for a message that bigger than configured value
* @throws IOException
* @throws Exception
*/
@Test(timeout = 8000)
public void testMessageTooBig() throws IOException, Exception {
final String bigMessage = "Echooooooooooooo";
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
WebsocketClient client = new WebsocketClient();
javax.websocket.Session session = container.connectToServer(client, proxyUri);
session.getBasicRemote().sendText(bigMessage);
client.awaitClose(CloseReason.CloseCodes.TOO_BIG.getCode(), 1000, TimeUnit.MILLISECONDS);
Assert.assertThat(client.close.getCloseCode().getCode(), CoreMatchers.is(CloseReason.CloseCodes.TOO_BIG.getCode()));
}
use of javax.websocket.WebSocketContainer in project knox by apache.
the class BigEchoSocketHandler method testMessageOk.
/**
* Test for a message within limit.
* @throws IOException
* @throws Exception
*/
@Test(timeout = 8000)
public void testMessageOk() throws IOException, Exception {
final String message = "Echo";
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
WebsocketClient client = new WebsocketClient();
javax.websocket.Session session = container.connectToServer(client, proxyUri);
session.getBasicRemote().sendText(message);
client.messageQueue.awaitMessages(1, 1000, TimeUnit.MILLISECONDS);
Assert.assertThat(client.messageQueue.get(0), CoreMatchers.is("Echo"));
}
use of javax.websocket.WebSocketContainer in project knox by apache.
the class ProxyInboundClientTest method testClientInstance.
// @Test(timeout = 3000)
@Test
public void testClientInstance() throws IOException, DeploymentException {
final String textMessage = "Echo";
final ByteBuffer binarymessage = ByteBuffer.wrap(textMessage.getBytes());
final AtomicBoolean isTestComplete = new AtomicBoolean(false);
final WebSocketContainer container = ContainerProvider.getWebSocketContainer();
final ProxyInboundClient client = new ProxyInboundClient(new MessageEventCallback() {
/**
* A generic callback, can be left un-implemented
*
* @param message
*/
@Override
public void doCallback(String message) {
}
/**
* Callback when connection is established.
*
* @param session
*/
@Override
public void onConnectionOpen(Object session) {
}
/**
* Callback when connection is closed.
*
* @param reason
*/
@Override
public void onConnectionClose(CloseReason reason) {
isTestComplete.set(true);
}
/**
* Callback when there is an error in connection.
*
* @param cause
*/
@Override
public void onError(Throwable cause) {
isTestComplete.set(true);
}
/**
* Callback when a text message is received.
*
* @param message
* @param session
*/
@Override
public void onMessageText(String message, Object session) {
recievedMessage = message;
isTestComplete.set(true);
}
/**
* Callback when a binary message is received.
*
* @param message
* @param last
* @param session
*/
@Override
public void onMessageBinary(byte[] message, boolean last, Object session) {
}
});
Assert.assertThat(client, instanceOf(javax.websocket.Endpoint.class));
Session session = container.connectToServer(client, serverUri);
session.getBasicRemote().sendText(textMessage);
while (!isTestComplete.get()) {
/* just wait for the test to finish */
}
Assert.assertEquals("The received text message is not the same as the sent", textMessage, recievedMessage);
}
Aggregations