Search in sources :

Example 21 with Session

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

the class TestWsWebSocketContainerGetOpenSessions method doTest.

private void doTest(Endpoint client1, Endpoint client2, String server1, String server2, int client1Count, int client2Count, int server1Count, int server2Count) throws Exception {
    Tracker.reset();
    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();
    Session sClient1Server1 = createSession(wsContainer, client1, "client1", server1);
    Session sClient1Server2 = createSession(wsContainer, client1, "client1", server2);
    Session sClient2Server1 = createSession(wsContainer, client2, "client2", server1);
    Session sClient2Server2 = createSession(wsContainer, client2, "client2", server2);
    int delayCount = 0;
    // but some CI systems get be slow at times.
    while (Tracker.getUpdateCount() < 8 && delayCount < 400) {
        Thread.sleep(50);
        delayCount++;
    }
    Assert.assertTrue(Tracker.dump(), Tracker.checkRecord("client1", client1Count));
    Assert.assertTrue(Tracker.dump(), Tracker.checkRecord("client2", client2Count));
    // Note: need to strip leading '/' from path
    Assert.assertTrue(Tracker.dump(), Tracker.checkRecord(server1.substring(1), server1Count));
    Assert.assertTrue(Tracker.dump(), Tracker.checkRecord(server2.substring(1), server2Count));
    sClient1Server1.close();
    sClient1Server2.close();
    sClient2Server1.close();
    sClient2Server2.close();
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(jakarta.websocket.WebSocketContainer) ServerEndpointConfig(jakarta.websocket.server.ServerEndpointConfig) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) EndpointConfig(jakarta.websocket.EndpointConfig) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) Endpoint(jakarta.websocket.Endpoint) ServerEndpoint(jakarta.websocket.server.ServerEndpoint) Session(jakarta.websocket.Session)

Example 22 with Session

use of jakarta.websocket.Session in project spring-framework by spring-projects.

the class StandardWebSocketSessionTests method getPrincipalNone.

@Test
@SuppressWarnings("resource")
public void getPrincipalNone() {
    Session nativeSession = Mockito.mock(Session.class);
    given(nativeSession.getUserPrincipal()).willReturn(null);
    StandardWebSocketSession session = new StandardWebSocketSession(this.headers, this.attributes, null, null);
    session.initializeNativeSession(nativeSession);
    reset(nativeSession);
    assertThat(session.getPrincipal()).isNull();
    verifyNoMoreInteractions(nativeSession);
}
Also used : Session(jakarta.websocket.Session) Test(org.junit.jupiter.api.Test)

Example 23 with Session

use of jakarta.websocket.Session in project spring-framework by spring-projects.

the class StandardWebSocketSessionTests method getAcceptedProtocol.

@Test
@SuppressWarnings("resource")
public void getAcceptedProtocol() {
    String protocol = "foo";
    Session nativeSession = Mockito.mock(Session.class);
    given(nativeSession.getNegotiatedSubprotocol()).willReturn(protocol);
    StandardWebSocketSession session = new StandardWebSocketSession(this.headers, this.attributes, null, null);
    session.initializeNativeSession(nativeSession);
    reset(nativeSession);
    assertThat(session.getAcceptedProtocol()).isEqualTo(protocol);
    verifyNoMoreInteractions(nativeSession);
}
Also used : Session(jakarta.websocket.Session) Test(org.junit.jupiter.api.Test)

Example 24 with Session

use of jakarta.websocket.Session in project spring-framework by spring-projects.

the class StandardWebSocketSessionTests method getPrincipalWithNativeSession.

@Test
@SuppressWarnings("resource")
public void getPrincipalWithNativeSession() {
    TestPrincipal user = new TestPrincipal("joe");
    Session nativeSession = Mockito.mock(Session.class);
    given(nativeSession.getUserPrincipal()).willReturn(user);
    StandardWebSocketSession session = new StandardWebSocketSession(this.headers, this.attributes, null, null);
    session.initializeNativeSession(nativeSession);
    assertThat(session.getPrincipal()).isSameAs(user);
}
Also used : TestPrincipal(org.springframework.core.testfixture.security.TestPrincipal) Session(jakarta.websocket.Session) Test(org.junit.jupiter.api.Test)

Example 25 with Session

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

the class TestWsWebSocketContainer method testSessionExpiryOnUserPropertyReadIdleTimeout.

@Test
public void testSessionExpiryOnUserPropertyReadIdleTimeout() 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();
    wsContainer.setDefaultMaxSessionIdleTimeout(90000);
    wsContainer.setProcessPeriod(1);
    EndpointA endpointA = new EndpointA();
    Session s1a = connectToEchoServer(wsContainer, endpointA, TesterEchoServer.Config.PATH_BASIC);
    s1a.setMaxIdleTimeout(90000);
    s1a.getUserProperties().put(Constants.READ_IDLE_TIMEOUT_MS, Long.valueOf(5000));
    // maxIdleTimeout is 90s but the readIdleTimeout is 5s. The session
    // should get closed after 5 seconds as nothing is read on it.
    // First confirm the session has been opened.
    Assert.assertEquals(1, s1a.getOpenSessions().size());
    // Now wait for it to close. Allow up to 30s as some CI systems are slow
    // but that is still well under the 90s configured for the session.
    int count = 0;
    while (count < 300 && s1a.isOpen()) {
        count++;
        Thread.sleep(100);
    }
    Assert.assertFalse(s1a.isOpen());
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ClientEndpointConfig(jakarta.websocket.ClientEndpointConfig) EndpointConfig(jakarta.websocket.EndpointConfig) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) TesterProgrammaticEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint) Endpoint(jakarta.websocket.Endpoint) TesterEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint) ServerEndpoint(jakarta.websocket.server.ServerEndpoint) Session(jakarta.websocket.Session) 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