Search in sources :

Example 1 with EndpointA

use of org.apache.tomcat.websocket.TestWsWebSocketContainer.EndpointA in project tomcat by apache.

the class TestWsWebSocketContainerSessionExpirySession method testSessionExpirySession.

@Test
public void testSessionExpirySession() 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();
    // Need access to implementation methods for configuring unit tests
    WsWebSocketContainer wsContainer = (WsWebSocketContainer) ContainerProvider.getWebSocketContainer();
    // 5 second timeout
    wsContainer.setDefaultMaxSessionIdleTimeout(5000);
    wsContainer.setProcessPeriod(1);
    EndpointA endpointA = new EndpointA();
    Session s1a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC);
    s1a.setMaxIdleTimeout(3000);
    Session s2a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC);
    s2a.setMaxIdleTimeout(6000);
    Session s3a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC);
    s3a.setMaxIdleTimeout(9000);
    // Check all three sessions are open
    Set<Session> setA = s3a.getOpenSessions();
    int expected = 3;
    while (expected > 0) {
        Assert.assertEquals(expected, getOpenCount(setA));
        int count = 0;
        while (getOpenCount(setA) == expected && count < 50) {
            count++;
            Thread.sleep(100);
        }
        expected--;
    }
    Assert.assertEquals(0, getOpenCount(setA));
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) EndpointA(org.apache.tomcat.websocket.TestWsWebSocketContainer.EndpointA) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) Session(jakarta.websocket.Session) Test(org.junit.Test)

Example 2 with EndpointA

use of org.apache.tomcat.websocket.TestWsWebSocketContainer.EndpointA in project tomcat by apache.

the class TestWsWebSocketContainerSessionExpiryContainer method testSessionExpiryContainer.

@Test
public void testSessionExpiryContainer() 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();
    // Need access to implementation methods for configuring unit tests
    WsWebSocketContainer wsContainer = (WsWebSocketContainer) ContainerProvider.getWebSocketContainer();
    // 5 second timeout
    wsContainer.setDefaultMaxSessionIdleTimeout(5000);
    wsContainer.setProcessPeriod(1);
    EndpointA endpointA = new EndpointA();
    connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC);
    connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC);
    Session s3a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC);
    // Check all three sessions are open
    Set<Session> setA = s3a.getOpenSessions();
    Assert.assertEquals(3, setA.size());
    int count = 0;
    boolean isOpen = true;
    while (isOpen && count < 8) {
        count++;
        Thread.sleep(1000);
        isOpen = false;
        for (Session session : setA) {
            if (session.isOpen()) {
                isOpen = true;
                break;
            }
        }
    }
    if (isOpen) {
        for (Session session : setA) {
            if (session.isOpen()) {
                System.err.println("Session with ID [" + session.getId() + "] is open");
            }
        }
        Assert.fail("There were open sessions");
    }
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) EndpointA(org.apache.tomcat.websocket.TestWsWebSocketContainer.EndpointA) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) Session(jakarta.websocket.Session) Test(org.junit.Test)

Aggregations

Session (jakarta.websocket.Session)2 Context (org.apache.catalina.Context)2 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)2 Tomcat (org.apache.catalina.startup.Tomcat)2 EndpointA (org.apache.tomcat.websocket.TestWsWebSocketContainer.EndpointA)2 Test (org.junit.Test)2