Search in sources :

Example 26 with WebSocketContainer

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

the class TesterWsClientAutobahn method main.

public static void main(String[] args) throws Exception {
    WebSocketContainer wsc = ContainerProvider.getWebSocketContainer();
    int testCaseCount = getTestCaseCount(wsc);
    System.out.println("There are " + testCaseCount + " test cases");
    for (int testCase = 1; testCase <= testCaseCount; testCase++) {
        if (testCase % 50 == 0) {
            System.out.println(testCase);
        } else {
            System.out.print('.');
        }
        try {
            executeTestCase(wsc, testCase);
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            t.printStackTrace();
        }
    }
    System.out.println("Testing complete");
    updateReports(wsc);
}
Also used : WebSocketContainer(jakarta.websocket.WebSocketContainer) Endpoint(jakarta.websocket.Endpoint) ClientEndpoint(jakarta.websocket.ClientEndpoint)

Example 27 with WebSocketContainer

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

the class TestEncodingDecoding method testBatchedEndPoints.

@Test
public void testBatchedEndPoints() throws Exception {
    // Set up utility classes
    BatchedServer server = new BatchedServer();
    SingletonConfigurator.setInstance(server);
    ServerConfigListener.setPojoClazz(BatchedServer.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_BATCHED_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)

Example 28 with WebSocketContainer

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

the class TestEncodingDecoding method testAnnotatedEndPoints.

@Test
public void testAnnotatedEndPoints() 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();
    Client client = new Client();
    URI uri = new URI("ws://localhost:" + getPort() + PATH_ANNOTATED_EP);
    Session session = wsContainer.connectToServer(client, uri);
    MsgString msg1 = new MsgString();
    msg1.setData(MESSAGE_ONE);
    session.getBasicRemote().sendObject(msg1);
    // Should not take very long
    int i = 0;
    while (i < WAIT_LOOPS) {
        if (server.received.size() > 0 && client.received.size() > 0) {
            break;
        }
        i++;
        Thread.sleep(WAIT_DELAY);
    }
    // Check messages were received
    Assert.assertEquals(1, server.received.size());
    Assert.assertEquals(1, client.received.size());
    // Check correct messages were received
    Assert.assertEquals(MESSAGE_ONE, ((MsgString) server.received.peek()).getData());
    Assert.assertEquals(MESSAGE_ONE, ((MsgString) client.received.peek()).getData());
    session.close();
    // Should not take very long but some failures have been seen
    i = testEvent(MsgStringEncoder.class.getName() + ":init", 0);
    i = testEvent(MsgStringDecoder.class.getName() + ":init", i);
    i = testEvent(MsgByteEncoder.class.getName() + ":init", i);
    i = testEvent(MsgByteDecoder.class.getName() + ":init", i);
    i = testEvent(MsgStringEncoder.class.getName() + ":destroy", i);
    i = testEvent(MsgStringDecoder.class.getName() + ":destroy", i);
    i = testEvent(MsgByteEncoder.class.getName() + ":destroy", i);
    i = testEvent(MsgByteDecoder.class.getName() + ":destroy", i);
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(jakarta.websocket.WebSocketContainer) URI(java.net.URI) Endpoint(jakarta.websocket.Endpoint) ClientEndpoint(jakarta.websocket.ClientEndpoint) ServerEndpoint(jakarta.websocket.server.ServerEndpoint) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ServerConfigListener(org.apache.tomcat.websocket.pojo.TesterUtil.ServerConfigListener) Session(jakarta.websocket.Session) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 29 with WebSocketContainer

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

the class TestEncodingDecoding method testUnsupportedObject.

@Test
public void testUnsupportedObject() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(ProgrammaticServerEndpointConfig.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() + PATH_PROGRAMMATIC_EP);
    Session session = wsContainer.connectToServer(client, uri);
    // This should fail
    Object msg1 = new Object();
    try {
        session.getBasicRemote().sendObject(msg1);
        Assert.fail("No exception thrown ");
    } catch (EncodeException e) {
    // Expected
    } catch (Throwable t) {
        Assert.fail("Wrong exception type");
    } finally {
        session.close();
    }
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(jakarta.websocket.WebSocketContainer) EncodeException(jakarta.websocket.EncodeException) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) URI(java.net.URI) Session(jakarta.websocket.Session) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 30 with WebSocketContainer

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

the class TestEncodingDecoding method testProgrammaticEndPoints.

@Test
public void testProgrammaticEndPoints() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(ProgrammaticServerEndpointConfig.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() + PATH_PROGRAMMATIC_EP);
    Session session = wsContainer.connectToServer(client, uri);
    MsgString msg1 = new MsgString();
    msg1.setData(MESSAGE_ONE);
    session.getBasicRemote().sendObject(msg1);
    // Should not take very long
    int i = 0;
    while (i < WAIT_LOOPS) {
        if (MsgStringMessageHandler.received.size() > 0 && client.received.size() > 0) {
            break;
        }
        i++;
        Thread.sleep(WAIT_DELAY);
    }
    // Check messages were received
    Assert.assertEquals(1, MsgStringMessageHandler.received.size());
    Assert.assertEquals(1, client.received.size());
    // Check correct messages were received
    Assert.assertEquals(MESSAGE_ONE, ((MsgString) MsgStringMessageHandler.received.peek()).getData());
    Assert.assertEquals(MESSAGE_ONE, new String(((MsgByte) client.received.peek()).getData()));
    session.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) 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

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