Search in sources :

Example 31 with WebSocketContainer

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

the class TestPojoMethodMapping method test.

@Test
public void test() throws Exception {
    // Set up utility classes
    Server server = new Server();
    SingletonConfigurator.setInstance(server);
    ServerConfigListener.setPojoClazz(Server.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();
    SimpleClient client = new SimpleClient();
    URI uri = new URI("ws://localhost:" + getPort() + "/" + PARAM_ONE + "/" + PARAM_TWO + "/" + PARAM_THREE);
    Session session = wsContainer.connectToServer(client, uri);
    session.getBasicRemote().sendText("NO-OP");
    session.close();
    // Give server 20s to close. 5s should be plenty but the Gump VM is slow
    int count = 0;
    while (count < 200) {
        if (server.isClosed()) {
            break;
        }
        count++;
        Thread.sleep(100);
    }
    if (count == 50) {
        Assert.fail("Server did not process an onClose event within 5 " + "seconds of the client sending a close message");
    }
    // Check no errors
    List<String> errors = server.getErrors();
    for (String error : errors) {
        System.err.println(error);
    }
    Assert.assertEquals("Found errors", 0, errors.size());
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(jakarta.websocket.WebSocketContainer) SimpleClient(org.apache.tomcat.websocket.pojo.TesterUtil.SimpleClient) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ServerConfigListener(org.apache.tomcat.websocket.pojo.TesterUtil.ServerConfigListener) URI(java.net.URI) ServerEndpoint(jakarta.websocket.server.ServerEndpoint) Session(jakarta.websocket.Session) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 32 with WebSocketContainer

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

the class TestAsyncMessages method testAsyncTiming.

@Test
public void testAsyncTiming() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(TesterAsyncTiming.Config.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() + TesterAsyncTiming.Config.PATH));
    AsyncTimingClientHandler handler = new AsyncTimingClientHandler();
    wsSession.addMessageHandler(ByteBuffer.class, handler);
    wsSession.getBasicRemote().sendText("Hello");
    System.out.println("Sent Hello message, waiting for data");
    handler.waitForLatch();
    Assert.assertFalse(handler.hasFailed());
}
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) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) URI(java.net.URI) Session(jakarta.websocket.Session) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 33 with WebSocketContainer

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

the class TestPojoEndpointBase method testBug54716.

@Test
public void testBug54716() throws Exception {
    TestUtil.generateMask();
    // Set up utility classes
    Bug54716 server = new Bug54716();
    SingletonConfigurator.setInstance(server);
    ServerConfigListener.setPojoClazz(Bug54716.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() + "/");
    wsContainer.connectToServer(client, uri);
    // Server should close the connection after the exception on open.
    boolean closed = client.waitForClose(5);
    Assert.assertTrue("Server failed to close connection", closed);
}
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) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 34 with WebSocketContainer

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

the class TestClassLoader method testSimple.

/*
     * Checks class loader for the server endpoint during onOpen and onMessage
     */
@Test
public void testSimple() 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();
    Client client = new Client();
    Session wsSession = wsContainer.connectToServer(client, new URI("ws://localhost:" + getPort() + "/test"));
    Assert.assertTrue(wsSession.isOpen());
    // Wait up to 5s for a message
    int count = 0;
    while (count < 50 && client.getMsgCount() < 1) {
        Thread.sleep(100);
    }
    // Check it
    Assert.assertEquals(1, client.getMsgCount());
    Assert.assertFalse(client.hasFailed());
    wsSession.getBasicRemote().sendText("Testing");
    // Wait up to 5s for a message
    count = 0;
    while (count < 50 && client.getMsgCount() < 2) {
        Thread.sleep(100);
    }
    Assert.assertEquals(2, client.getMsgCount());
    Assert.assertFalse(client.hasFailed());
    wsSession.close();
}
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) ServerEndpoint(jakarta.websocket.server.ServerEndpoint) Session(jakarta.websocket.Session) Test(org.junit.Test) WebSocketBaseTest(org.apache.tomcat.websocket.WebSocketBaseTest)

Example 35 with WebSocketContainer

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

the class TestWsRemoteEndpointImplServer method testClientDropsConnection.

/*
     * https://bz.apache.org/bugzilla/show_bug.cgi?id=58624
     *
     * This test requires three breakpoints to be set. Two in this file (marked
     * A & B with comments) and one (C) at the start of
     * WsRemoteEndpointImplServer.doWrite().
     *
     * With the breakpoints in place, run this test.
     * Once breakpoints A & B are reached, progress the thread at breakpoint A
     * one line to close the connection.
     * Once breakpoint C is reached, allow the thread at breakpoint B to
     * continue.
     * Then allow the thread at breakpoint C to continue.
     *
     * In the failure mode, the thread at breakpoint B will not progress past
     * the call to sendObject(). If the issue is fixed, the thread at breakpoint
     * B will continue past sendObject() and terminate with a TimeoutException.
     */
@Test
public void testClientDropsConnection() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(Bug58624Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMappingDecoded("/", "default");
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    tomcat.start();
    SimpleClient client = new SimpleClient();
    URI uri = new URI("ws://localhost:" + getPort() + Bug58624Config.PATH);
    Session session = wsContainer.connectToServer(client, uri);
    // Break point A required on following line
    session.close();
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(jakarta.websocket.WebSocketContainer) SimpleClient(org.apache.tomcat.websocket.pojo.TesterUtil.SimpleClient) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) URI(java.net.URI) Session(jakarta.websocket.Session) WebSocketBaseTest(org.apache.tomcat.websocket.WebSocketBaseTest) 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