Search in sources :

Example 36 with WebSocketContainer

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

the class TestWsWebSocketContainerTimeoutClient method doTestWriteTimeoutClient.

private void doTestWriteTimeoutClient(boolean setTimeoutOnContainer) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(BlockingConfig.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    // Set the async timeout
    if (setTimeoutOnContainer) {
        wsContainer.setAsyncSendTimeout(TIMEOUT_MS);
    }
    tomcat.start();
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder.create().build(), new URI("ws://" + getHostName() + ":" + getPort() + BlockingConfig.PATH));
    if (!setTimeoutOnContainer) {
        wsSession.getAsyncRemote().setSendTimeout(TIMEOUT_MS);
    }
    long lastSend = 0;
    // Should send quickly until the network buffers fill up and then block
    // until the timeout kicks in
    Exception exception = null;
    try {
        while (true) {
            lastSend = System.currentTimeMillis();
            Future<Void> f = wsSession.getAsyncRemote().sendBinary(ByteBuffer.wrap(MESSAGE_BINARY_4K));
            f.get();
        }
    } catch (Exception e) {
        exception = e;
    }
    long timeout = System.currentTimeMillis() - lastSend;
    // Clear the server side block and prevent further blocks to allow the
    // server to shutdown cleanly
    BlockingPojo.clearBlock();
    // Close the client session, primarily to allow the
    // BackgroundProcessManager to shut down.
    wsSession.close();
    String msg = "Time out was [" + timeout + "] ms";
    // Check correct time passed
    Assert.assertTrue(msg, timeout >= TIMEOUT_MS - MARGIN);
    // Check the timeout wasn't too long
    Assert.assertTrue(msg, timeout < TIMEOUT_MS * 2);
    Assert.assertNotNull(exception);
}
Also used : Context(org.apache.catalina.Context) BlockingConfig(org.apache.tomcat.websocket.TestWsWebSocketContainer.BlockingConfig) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(jakarta.websocket.WebSocketContainer) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) URI(java.net.URI) Session(jakarta.websocket.Session)

Example 37 with WebSocketContainer

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

the class TestWsPingPongMessages method testPingPongMessages.

@Test
public void testPingPongMessages() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    tomcat.start();
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder.create().build(), new URI("ws://localhost:" + getPort() + TesterEchoServer.Config.PATH_ASYNC));
    CountDownLatch latch = new CountDownLatch(1);
    TesterEndpoint tep = (TesterEndpoint) wsSession.getUserProperties().get("endpoint");
    tep.setLatch(latch);
    PongMessageHandler handler = new PongMessageHandler(latch);
    wsSession.addMessageHandler(handler);
    wsSession.getBasicRemote().sendPing(applicationData);
    boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
    Assert.assertTrue(latchResult);
    Assert.assertArrayEquals(applicationData.array(), (handler.getMessages().peek()).getApplicationData().array());
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(jakarta.websocket.WebSocketContainer) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) TesterEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint) Session(jakarta.websocket.Session) Test(org.junit.Test)

Example 38 with WebSocketContainer

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

the class TestWsSubprotocols method testWsSubprotocols.

@Test
public void testWsSubprotocols() throws Exception {
    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();
    tomcat.start();
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder.create().preferredSubprotocols(Arrays.asList("sp3")).build(), new URI("ws://localhost:" + getPort() + SubProtocolsEndpoint.PATH_BASIC));
    Assert.assertTrue(wsSession.isOpen());
    if (wsSession.getNegotiatedSubprotocol() != null) {
        Assert.assertTrue(wsSession.getNegotiatedSubprotocol().isEmpty());
    }
    wsSession.close();
    SubProtocolsEndpoint.recycle();
    wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder.create().preferredSubprotocols(Arrays.asList("sp2")).build(), new URI("ws://localhost:" + getPort() + SubProtocolsEndpoint.PATH_BASIC));
    Assert.assertTrue(wsSession.isOpen());
    Assert.assertEquals("sp2", wsSession.getNegotiatedSubprotocol());
    // Client thread might move faster than server. Wait for upto 5s for the
    // subProtocols to be set
    int count = 0;
    while (count < 50 && SubProtocolsEndpoint.subprotocols == null) {
        count++;
        Thread.sleep(100);
    }
    Assert.assertNotNull(SubProtocolsEndpoint.subprotocols);
    Assert.assertArrayEquals(new String[] { "sp1", "sp2" }, SubProtocolsEndpoint.subprotocols.toArray(new String[2]));
    wsSession.close();
    SubProtocolsEndpoint.recycle();
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(jakarta.websocket.WebSocketContainer) TesterProgrammaticEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint) ServerEndpointConfig(jakarta.websocket.server.ServerEndpointConfig) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) TesterEndpointConfig(org.apache.tomcat.websocket.server.TesterEndpointConfig) EndpointConfig(jakarta.websocket.EndpointConfig) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) URI(java.net.URI) TesterProgrammaticEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint) ServerEndpoint(jakarta.websocket.server.ServerEndpoint) Session(jakarta.websocket.Session) Test(org.junit.Test)

Example 39 with WebSocketContainer

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

the class TestWebSocketFrameClient method echoTester.

public void echoTester(String path, ClientEndpointConfig clientEndpointConfig) throws Exception {
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    if (clientEndpointConfig == null) {
        clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
    }
    // Increase default timeout from 5s to 10s to try and reduce errors on
    // CI systems.
    clientEndpointConfig.getUserProperties().put(Constants.IO_TIMEOUT_MS_PROPERTY, "10000");
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientEndpointConfig, new URI("ws://localhost:" + getPort() + path));
    CountDownLatch latch = new CountDownLatch(1);
    BasicText handler = new BasicText(latch);
    wsSession.addMessageHandler(handler);
    wsSession.getBasicRemote().sendText("Hello");
    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("Hello", message);
    }
    wsSession.close();
}
Also used : BasicText(org.apache.tomcat.websocket.TesterMessageCountClient.BasicText) WebSocketContainer(jakarta.websocket.WebSocketContainer) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) Session(jakarta.websocket.Session)

Example 40 with WebSocketContainer

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

the class TestShutdown method testShutdownBufferedMessages.

@Test
public void testShutdownBufferedMessages() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(EchoBufferedConfig.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    tomcat.start();
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientEndpointConfig, new URI("ws://localhost:" + getPort() + "/test"));
    CountDownLatch latch = new CountDownLatch(1);
    BasicText handler = new BasicText(latch);
    wsSession.addMessageHandler(handler);
    wsSession.getBasicRemote().sendText("Hello");
    int count = 0;
    while (count < 10 && EchoBufferedEndpoint.messageCount.get() == 0) {
        Thread.sleep(200);
        count++;
    }
    Assert.assertNotEquals("Message not received by server", EchoBufferedEndpoint.messageCount.get(), 0);
    tomcat.stop();
    Assert.assertTrue("Latch expired waiting for message", latch.await(10, TimeUnit.SECONDS));
}
Also used : Context(org.apache.catalina.Context) BasicText(org.apache.tomcat.websocket.TesterMessageCountClient.BasicText) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(jakarta.websocket.WebSocketContainer) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) TesterProgrammaticEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint) ServerEndpoint(jakarta.websocket.server.ServerEndpoint) Session(jakarta.websocket.Session) Test(org.junit.Test) WebSocketBaseTest(org.apache.tomcat.websocket.WebSocketBaseTest)

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