Search in sources :

Example 6 with Session

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

the class TestWsWebSocketContainer method testGetOpenSessions.

@Test
public void testGetOpenSessions() 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();
    EndpointA endpointA = new EndpointA();
    Session s1a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC);
    Session s2a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC);
    Session s3a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC);
    EndpointB endpointB = new EndpointB();
    Session s1b = connectToEchoServer(wsContainer, endpointB, TesterEchoServer.Config.PATH_BASIC);
    Session s2b = connectToEchoServer(wsContainer, endpointB, TesterEchoServer.Config.PATH_BASIC);
    Set<Session> setA = s3a.getOpenSessions();
    Assert.assertEquals(3, setA.size());
    Assert.assertTrue(setA.remove(s1a));
    Assert.assertTrue(setA.remove(s2a));
    Assert.assertTrue(setA.remove(s3a));
    s1a.close();
    setA = s3a.getOpenSessions();
    Assert.assertEquals(2, setA.size());
    Assert.assertFalse(setA.remove(s1a));
    Assert.assertTrue(setA.remove(s2a));
    Assert.assertTrue(setA.remove(s3a));
    Set<Session> setB = s1b.getOpenSessions();
    Assert.assertEquals(2, setB.size());
    Assert.assertTrue(setB.remove(s1b));
    Assert.assertTrue(setB.remove(s2b));
    // Close sessions explicitly as Gump reports a session remains open at
    // the end of this test
    s2a.close();
    s3a.close();
    s1b.close();
    s2b.close();
}
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) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) Session(jakarta.websocket.Session) Test(org.junit.Test)

Example 7 with Session

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

the class TestWsWebSocketContainer method doTestPerMessageDeflateClient.

private void doTestPerMessageDeflateClient(String msg, int count) 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();
    Extension perMessageDeflate = new WsExtension(PerMessageDeflate.NAME);
    List<Extension> extensions = new ArrayList<>(1);
    extensions.add(perMessageDeflate);
    ClientEndpointConfig clientConfig = ClientEndpointConfig.Builder.create().extensions(extensions).build();
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientConfig, new URI("ws://" + getHostName() + ":" + getPort() + TesterEchoServer.Config.PATH_ASYNC));
    CountDownLatch latch = new CountDownLatch(count);
    BasicText handler = new BasicText(latch, msg);
    wsSession.addMessageHandler(handler);
    for (int i = 0; i < count; i++) {
        wsSession.getBasicRemote().sendText(msg);
    }
    boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
    Assert.assertTrue(latchResult);
    ((WsWebSocketContainer) wsContainer).destroy();
}
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) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) TesterProgrammaticEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint) Endpoint(jakarta.websocket.Endpoint) TesterEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint) ServerEndpoint(jakarta.websocket.server.ServerEndpoint) Extension(jakarta.websocket.Extension) BasicText(org.apache.tomcat.websocket.TesterMessageCountClient.BasicText) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) Session(jakarta.websocket.Session)

Example 8 with Session

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

the class TestWsWebSocketContainerTimeoutServer method doTestWriteTimeoutServer.

private void doTestWriteTimeoutServer(boolean setTimeoutOnContainer) throws Exception {
    /*
         * Note: There are all sorts of horrible uses of statics in this test
         *       because the API uses classes and the tests really need access
         *       to the instances which simply isn't possible.
         */
    timeoutOnContainer = setTimeoutOnContainer;
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(ConstantTxConfig.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    tomcat.start();
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder.create().build(), new URI("ws://" + getHostName() + ":" + getPort() + ConstantTxConfig.PATH));
    wsSession.addMessageHandler(new BlockingBinaryHandler());
    int loops = 0;
    while (loops < 15) {
        Thread.sleep(1000);
        if (!ConstantTxEndpoint.getRunning()) {
            break;
        }
        loops++;
    }
    // Close the client session, primarily to allow the
    // BackgroundProcessManager to shut down.
    wsSession.close();
    // Check the right exception was thrown
    Assert.assertNotNull(ConstantTxEndpoint.getException());
    Assert.assertEquals(ExecutionException.class, ConstantTxEndpoint.getException().getClass());
    Assert.assertNotNull(ConstantTxEndpoint.getException().getCause());
    Assert.assertEquals(SocketTimeoutException.class, ConstantTxEndpoint.getException().getCause().getClass());
    // Check correct time passed
    Assert.assertTrue(ConstantTxEndpoint.getTimeout() >= TIMEOUT_MS);
    // Check the timeout wasn't too long
    Assert.assertTrue(ConstantTxEndpoint.getTimeout() < TIMEOUT_MS * 2);
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(jakarta.websocket.WebSocketContainer) BlockingBinaryHandler(org.apache.tomcat.websocket.TestWsWebSocketContainer.BlockingBinaryHandler) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) URI(java.net.URI) TesterProgrammaticEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint) Endpoint(jakarta.websocket.Endpoint) Session(jakarta.websocket.Session)

Example 9 with Session

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

the class TestWebSocketFrameClient method testConnectToServerEndpoint.

@Test
public void testConnectToServerEndpoint() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(TesterFirehoseServer.ConfigInline.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    tomcat.start();
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    // BZ 62596
    ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().configurator(new Configurator() {

        @Override
        public void beforeRequest(Map<String, List<String>> headers) {
            headers.put("Dummy", Collections.singletonList(String.join("", Collections.nCopies(4000, "A"))));
            super.beforeRequest(headers);
        }
    }).build();
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientEndpointConfig, new URI("ws://localhost:" + getPort() + TesterFirehoseServer.PATH));
    CountDownLatch latch = new CountDownLatch(TesterFirehoseServer.MESSAGE_COUNT);
    BasicText handler = new BasicText(latch);
    wsSession.addMessageHandler(handler);
    wsSession.getBasicRemote().sendText("Hello");
    System.out.println("Sent Hello message, waiting for data");
    // Ignore the latch result as the message count test below will tell us
    // if the right number of messages arrived
    handler.getLatch().await(TesterFirehoseServer.WAIT_TIME_MILLIS, TimeUnit.MILLISECONDS);
    Queue<String> messages = handler.getMessages();
    Assert.assertEquals(TesterFirehoseServer.MESSAGE_COUNT, messages.size());
    for (String message : messages) {
        Assert.assertEquals(TesterFirehoseServer.MESSAGE, message);
    }
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(jakarta.websocket.WebSocketContainer) Configurator(jakarta.websocket.ClientEndpointConfig.Configurator) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) BasicText(org.apache.tomcat.websocket.TesterMessageCountClient.BasicText) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) Map(java.util.Map) Session(jakarta.websocket.Session) Test(org.junit.Test)

Example 10 with Session

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

the class TestEncodingDecoding method testMessagesEndPoints.

@Test
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 < WAIT_LOOPS) {
        if (server.received.size() > 0 && client.received.size() > 1) {
            break;
        }
        i++;
        Thread.sleep(WAIT_DELAY);
    }
    // Check messages were received
    Assert.assertEquals(1, server.received.size());
    Assert.assertEquals(2, 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(jakarta.websocket.WebSocketContainer) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ServerConfigListener(org.apache.tomcat.websocket.pojo.TesterUtil.ServerConfigListener) URI(java.net.URI) Endpoint(jakarta.websocket.Endpoint) ClientEndpoint(jakarta.websocket.ClientEndpoint) ServerEndpoint(jakarta.websocket.server.ServerEndpoint) Session(jakarta.websocket.Session) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

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