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();
}
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);
}
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);
}
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);
}
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());
}
Aggregations