Search in sources :

Example 21 with BasicText

use of org.apache.tomcat.websocket.TesterMessageCountClient.BasicText 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

URI (java.net.URI)21 CountDownLatch (java.util.concurrent.CountDownLatch)21 BasicText (org.apache.tomcat.websocket.TesterMessageCountClient.BasicText)21 Context (org.apache.catalina.Context)18 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)18 Tomcat (org.apache.catalina.startup.Tomcat)18 Test (org.junit.Test)14 Session (jakarta.websocket.Session)11 WebSocketContainer (jakarta.websocket.WebSocketContainer)11 Session (javax.websocket.Session)10 WebSocketContainer (javax.websocket.WebSocketContainer)10 ClientEndpointConfig (jakarta.websocket.ClientEndpointConfig)9 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)8 EndpointConfig (javax.websocket.EndpointConfig)5 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)5 TesterProgrammaticEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint)5 File (java.io.File)4 SSLContext (javax.net.ssl.SSLContext)4 TesterEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint)4 EndpointConfig (jakarta.websocket.EndpointConfig)3