Search in sources :

Example 16 with WebSocketContainer

use of jakarta.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(jakarta.websocket.WebSocketContainer) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) URI(java.net.URI) ClientEndpoint(jakarta.websocket.ClientEndpoint) Session(jakarta.websocket.Session) Test(org.junit.Test) WebSocketBaseTest(org.apache.tomcat.websocket.WebSocketBaseTest)

Example 17 with WebSocketContainer

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

the class TestSlowClient method testSendingFromAppThread.

@Test
public void testSendingFromAppThread() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Context ctx = tomcat.addContext("", null);
    // Server side endpoint that sends a stream of messages on a new thread
    // in response to any message received.
    ctx.addApplicationListener(TesterFirehoseServer.ConfigThread.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    tomcat.start();
    // WebSocket client
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder.create().build(), new URI("ws://localhost:" + getPort() + TesterFirehoseServer.PATH));
    // Configure a handler designed to create a backlog causing the server
    // side write to time out.
    wsSession.addMessageHandler(new VerySlowHandler());
    // Trigger the sending of the messages from the server
    wsSession.getBasicRemote().sendText("start");
    // Wait for server to close connection (it shouldn't)
    // 20s should be long enough even for the slowest CI system. May need to
    // extend this if not.
    int count = 0;
    while (wsSession.isOpen() && count < 200) {
        Thread.sleep(100);
        count++;
    }
    Assert.assertTrue(wsSession.isOpen());
    wsSession.close();
    // BZ 64848 (non-container thread variant)
    // Confirm there are no waiting processors
    AbstractProtocol<?> protocol = (AbstractProtocol<?>) tomcat.getConnector().getProtocolHandler();
    Assert.assertEquals(0, protocol.getWaitingProcessorCount());
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) AbstractProtocol(org.apache.coyote.AbstractProtocol) WebSocketContainer(jakarta.websocket.WebSocketContainer) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) URI(java.net.URI) TesterProgrammaticEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint) Session(jakarta.websocket.Session) Test(org.junit.Test) WebSocketBaseTest(org.apache.tomcat.websocket.WebSocketBaseTest)

Example 18 with WebSocketContainer

use of jakarta.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(jakarta.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(jakarta.websocket.Session) Test(org.junit.Test) WebSocketBaseTest(org.apache.tomcat.websocket.WebSocketBaseTest)

Example 19 with WebSocketContainer

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

the class TestWsWebSocketContainerGetOpenSessions method doTest.

private void doTest(Endpoint client1, Endpoint client2, String server1, String server2, int client1Count, int client2Count, int server1Count, int server2Count) throws Exception {
    Tracker.reset();
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    tomcat.start();
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    Session sClient1Server1 = createSession(wsContainer, client1, "client1", server1);
    Session sClient1Server2 = createSession(wsContainer, client1, "client1", server2);
    Session sClient2Server1 = createSession(wsContainer, client2, "client2", server1);
    Session sClient2Server2 = createSession(wsContainer, client2, "client2", server2);
    int delayCount = 0;
    // but some CI systems get be slow at times.
    while (Tracker.getUpdateCount() < 8 && delayCount < 400) {
        Thread.sleep(50);
        delayCount++;
    }
    Assert.assertTrue(Tracker.dump(), Tracker.checkRecord("client1", client1Count));
    Assert.assertTrue(Tracker.dump(), Tracker.checkRecord("client2", client2Count));
    // Note: need to strip leading '/' from path
    Assert.assertTrue(Tracker.dump(), Tracker.checkRecord(server1.substring(1), server1Count));
    Assert.assertTrue(Tracker.dump(), Tracker.checkRecord(server2.substring(1), server2Count));
    sClient1Server1.close();
    sClient1Server2.close();
    sClient2Server1.close();
    sClient2Server2.close();
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(jakarta.websocket.WebSocketContainer) ServerEndpointConfig(jakarta.websocket.server.ServerEndpointConfig) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) EndpointConfig(jakarta.websocket.EndpointConfig) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) Endpoint(jakarta.websocket.Endpoint) ServerEndpoint(jakarta.websocket.server.ServerEndpoint) Session(jakarta.websocket.Session)

Example 20 with WebSocketContainer

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

the class TestWsWebSocketContainer method testConnectToServerEndpointInvalidScheme.

@Test(expected = jakarta.websocket.DeploymentException.class)
public void testConnectToServerEndpointInvalidScheme() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
    tomcat.start();
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    wsContainer.connectToServer(TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder.create().build(), new URI("ftp://" + getHostName() + ":" + getPort() + TesterEchoServer.Config.PATH_ASYNC));
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(jakarta.websocket.WebSocketContainer) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) EndpointConfig(jakarta.websocket.EndpointConfig) URI(java.net.URI) Test(org.junit.Test)

Aggregations

WebSocketContainer (jakarta.websocket.WebSocketContainer)40 Context (org.apache.catalina.Context)38 Tomcat (org.apache.catalina.startup.Tomcat)38 Session (jakarta.websocket.Session)36 URI (java.net.URI)36 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)36 Test (org.junit.Test)30 ClientEndpointConfig (jakarta.websocket.ClientEndpointConfig)20 CountDownLatch (java.util.concurrent.CountDownLatch)15 Endpoint (jakarta.websocket.Endpoint)14 ServerEndpoint (jakarta.websocket.server.ServerEndpoint)13 TesterProgrammaticEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint)12 BasicText (org.apache.tomcat.websocket.TesterMessageCountClient.BasicText)11 EndpointConfig (jakarta.websocket.EndpointConfig)10 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)10 ClientEndpoint (jakarta.websocket.ClientEndpoint)8 ServerConfigListener (org.apache.tomcat.websocket.pojo.TesterUtil.ServerConfigListener)7 SSLContext (javax.net.ssl.SSLContext)6 TesterEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint)6 WebSocketBaseTest (org.apache.tomcat.websocket.WebSocketBaseTest)6