Search in sources :

Example 6 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project tomcat by apache.

the class TestEncodingDecoding method testMessagesEndPoints.

@Test
// TODO Investigate why this test fails
@Ignore
public void testMessagesEndPoints() throws Exception {
    // Set up utility classes
    MessagesServer server = new MessagesServer();
    SingletonConfigurator.setInstance(server);
    ServerConfigListener.setPojoClazz(MessagesServer.class);
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(ServerConfigListener.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    tomcat.start();
    StringClient client = new StringClient();
    URI uri = new URI("ws://localhost:" + getPort() + PATH_MESSAGES_EP);
    Session session = wsContainer.connectToServer(client, uri);
    session.getBasicRemote().sendText(MESSAGE_ONE);
    // Should not take very long
    int i = 0;
    while (i < 20) {
        if (server.received.size() > 0 && client.received.size() > 0) {
            break;
        }
        i++;
        Thread.sleep(100);
    }
    // Check messages were received
    Assert.assertEquals(1, server.received.size());
    Assert.assertEquals(1, client.received.size());
    // Check correct messages were received
    Assert.assertEquals(MESSAGE_ONE, server.received.peek());
    session.close();
    Assert.assertNull(server.t);
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ServerConfigListener(org.apache.tomcat.websocket.pojo.TesterUtil.ServerConfigListener) URI(java.net.URI) Endpoint(javax.websocket.Endpoint) ServerEndpoint(javax.websocket.server.ServerEndpoint) ClientEndpoint(javax.websocket.ClientEndpoint) Session(javax.websocket.Session) Ignore(org.junit.Ignore) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 7 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project tomcat by apache.

the class TestEncodingDecoding method testGenericsCoders.

@Test
public void testGenericsCoders() throws Exception {
    // Set up utility classes
    GenericsServer server = new GenericsServer();
    SingletonConfigurator.setInstance(server);
    ServerConfigListener.setPojoClazz(GenericsServer.class);
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(ServerConfigListener.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    tomcat.start();
    GenericsClient client = new GenericsClient();
    URI uri = new URI("ws://localhost:" + getPort() + PATH_GENERICS_EP);
    Session session = wsContainer.connectToServer(client, uri);
    ArrayList<String> list = new ArrayList<>(2);
    list.add("str1");
    list.add("str2");
    session.getBasicRemote().sendObject(list);
    // Should not take very long
    int i = 0;
    while (i < 20) {
        if (server.received.size() > 0 && client.received.size() > 0) {
            break;
        }
        i++;
        Thread.sleep(100);
    }
    // Check messages were received
    Assert.assertEquals(1, server.received.size());
    Assert.assertEquals(server.received.peek().toString(), "[str1, str2]");
    Assert.assertEquals(1, client.received.size());
    Assert.assertEquals(client.received.peek().toString(), "[str1, str2]");
    session.close();
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) ArrayList(java.util.ArrayList) URI(java.net.URI) Endpoint(javax.websocket.Endpoint) ServerEndpoint(javax.websocket.server.ServerEndpoint) ClientEndpoint(javax.websocket.ClientEndpoint) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ServerConfigListener(org.apache.tomcat.websocket.pojo.TesterUtil.ServerConfigListener) Session(javax.websocket.Session) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 8 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project tomcat by apache.

the class TestPojoEndpointBase method testOnOpenPojoMethod.

@Test
public void testOnOpenPojoMethod() throws Exception {
    // Set up utility classes
    OnOpenServerEndpoint server = new OnOpenServerEndpoint();
    SingletonConfigurator.setInstance(server);
    ServerConfigListener.setPojoClazz(OnOpenServerEndpoint.class);
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(ServerConfigListener.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    tomcat.start();
    Client client = new Client();
    URI uri = new URI("ws://localhost:" + getPort() + "/");
    Session session = wsContainer.connectToServer(client, uri);
    client.waitForClose(5);
    Assert.assertTrue(session.isOpen());
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ServerConfigListener(org.apache.tomcat.websocket.pojo.TesterUtil.ServerConfigListener) URI(java.net.URI) Session(javax.websocket.Session) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 9 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project tomcat 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.addServletMappingDecoded("/", "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 10 with WebSocketContainer

use of javax.websocket.WebSocketContainer in project tomcat by apache.

the class TestWsServerContainer method testBug58232.

@Test
public void testBug58232() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(Bug54807Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    tomcat.start();
    Assert.assertEquals(LifecycleState.STARTED, ctx.getState());
    SimpleClient client = new SimpleClient();
    URI uri = new URI("ws://localhost:" + getPort() + "/echoBasic");
    try (Session session = wsContainer.connectToServer(client, uri)) {
        CountDownLatch latch = new CountDownLatch(1);
        BasicText handler = new BasicText(latch);
        session.addMessageHandler(handler);
        session.getBasicRemote().sendText("echoBasic");
        boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
        Assert.assertTrue(latchResult);
        Queue<String> messages = handler.getMessages();
        Assert.assertEquals(1, messages.size());
        for (String message : messages) {
            Assert.assertEquals("echoBasic", message);
        }
    }
}
Also used : TesterServletContext(org.apache.tomcat.unittest.TesterServletContext) Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) BasicText(org.apache.tomcat.websocket.TesterMessageCountClient.BasicText) SimpleClient(org.apache.tomcat.websocket.pojo.TesterUtil.SimpleClient) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) Session(javax.websocket.Session) Test(org.junit.Test) WebSocketBaseTest(org.apache.tomcat.websocket.WebSocketBaseTest)

Aggregations

WebSocketContainer (javax.websocket.WebSocketContainer)67 URI (java.net.URI)46 Session (javax.websocket.Session)42 Test (org.junit.Test)39 Context (org.apache.catalina.Context)32 Tomcat (org.apache.catalina.startup.Tomcat)32 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)30 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)20 Endpoint (javax.websocket.Endpoint)14 ServerEndpoint (javax.websocket.server.ServerEndpoint)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 EndpointConfig (javax.websocket.EndpointConfig)11 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)10 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)9 BasicText (org.apache.tomcat.websocket.TesterMessageCountClient.BasicText)9 TesterProgrammaticEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint)9 ClientEndpoint (javax.websocket.ClientEndpoint)8 DeploymentException (javax.websocket.DeploymentException)8 TesterEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint)7 ServerConfigListener (org.apache.tomcat.websocket.pojo.TesterUtil.ServerConfigListener)7