Search in sources :

Example 71 with WebSocketContainer

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();
    }
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) URI(java.net.URI) ClientEndpoint(javax.websocket.ClientEndpoint) Session(javax.websocket.Session) Test(org.junit.Test) WebSocketBaseTest(org.apache.tomcat.websocket.WebSocketBaseTest)

Example 72 with WebSocketContainer

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);
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) Test(org.junit.Test)

Example 73 with WebSocketContainer

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()));
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) Test(org.junit.Test)

Example 74 with WebSocketContainer

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"));
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) Test(org.junit.Test)

Example 75 with WebSocketContainer

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);
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) ByteBuffer(java.nio.ByteBuffer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CloseReason(javax.websocket.CloseReason) Session(javax.websocket.Session) Test(org.junit.Test)

Aggregations

WebSocketContainer (javax.websocket.WebSocketContainer)85 URI (java.net.URI)52 Session (javax.websocket.Session)52 Test (org.junit.Test)51 Context (org.apache.catalina.Context)29 Tomcat (org.apache.catalina.startup.Tomcat)29 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)27 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)20 CountDownLatch (java.util.concurrent.CountDownLatch)16 DeploymentException (javax.websocket.DeploymentException)13 Endpoint (javax.websocket.Endpoint)13 EndpointConfig (javax.websocket.EndpointConfig)13 ServerEndpoint (javax.websocket.server.ServerEndpoint)10 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)10 BasicText (org.apache.tomcat.websocket.TesterMessageCountClient.BasicText)10 IOException (java.io.IOException)8 TesterProgrammaticEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint)8 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)7 TesterEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint)7 PrintWriter (java.io.PrintWriter)6