use of javax.websocket.Session in project tomcat by apache.
the class TestWsWebSocketContainer 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");
}
}
use of javax.websocket.Session in project che by eclipse.
the class WebSocketSessionRegistryTest method shouldAddCorrectSession.
@Test
public void shouldAddCorrectSession() {
registry.add("0", this.session);
final Optional<Session> sessionOptional = registry.get("0");
assertTrue(sessionOptional.isPresent());
final Session session = sessionOptional.get();
assertEquals(this.session, session);
}
use of javax.websocket.Session in project jetty.project by eclipse.
the class MemoryUsageTest method testMemoryUsage.
@SuppressWarnings("unused")
@Test
public void testMemoryUsage() throws Exception {
int sessionCount = 1000;
Session[] sessions = new Session[sessionCount];
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
System.gc();
MemoryUsage heapBefore = memoryMXBean.getHeapMemoryUsage();
MemoryUsage nonHeapBefore = memoryMXBean.getNonHeapMemoryUsage();
URI uri = URI.create("ws://localhost:" + connector.getLocalPort());
final CountDownLatch latch = new CountDownLatch(sessionCount);
for (int i = 0; i < sessionCount; ++i) {
sessions[i] = client.connectToServer(new EndpointAdapter() {
@Override
public void onMessage(String message) {
latch.countDown();
}
}, uri);
}
for (int i = 0; i < sessionCount; ++i) {
sessions[i].getBasicRemote().sendText("OK");
}
latch.await(5 * sessionCount, TimeUnit.MILLISECONDS);
System.gc();
MemoryUsage heapAfter = memoryMXBean.getHeapMemoryUsage();
MemoryUsage nonHeapAfter = memoryMXBean.getNonHeapMemoryUsage();
long heapUsed = heapAfter.getUsed() - heapBefore.getUsed();
long nonHeapUsed = nonHeapAfter.getUsed() - nonHeapBefore.getUsed();
System.out.println("heapUsed = " + heapUsed);
System.out.println("nonHeapUsed = " + nonHeapUsed);
// new CountDownLatch(1).await();
// Assume no more than 25 KiB per session pair (client and server).
long expected = 25 * 1024 * sessionCount;
Assert.assertThat("heap used", heapUsed, lessThan(expected));
}
use of javax.websocket.Session in project jetty.project by eclipse.
the class JsrBatchModeTest method testBatchModeOff.
@Test
public void testBatchModeOff() throws Exception {
ClientEndpointConfig config = ClientEndpointConfig.Builder.create().build();
URI uri = URI.create("ws://localhost:" + connector.getLocalPort());
final CountDownLatch latch = new CountDownLatch(1);
EndpointAdapter endpoint = new EndpointAdapter() {
@Override
public void onMessage(String message) {
latch.countDown();
}
};
try (Session session = client.connectToServer(endpoint, config, uri)) {
RemoteEndpoint.Async remote = session.getAsyncRemote();
remote.setBatchingAllowed(false);
Future<Void> future = remote.sendText("batch_mode_off");
// The write is immediate.
future.get(1, TimeUnit.SECONDS);
// Wait for the echo.
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
}
use of javax.websocket.Session in project jetty.project by eclipse.
the class JsrBatchModeTest method testBatchModeAuto.
@Test
public void testBatchModeAuto() throws Exception {
ClientEndpointConfig config = ClientEndpointConfig.Builder.create().build();
URI uri = URI.create("ws://localhost:" + connector.getLocalPort());
final CountDownLatch latch = new CountDownLatch(1);
EndpointAdapter endpoint = new EndpointAdapter() {
@Override
public void onMessage(String message) {
latch.countDown();
}
};
try (Session session = client.connectToServer(endpoint, config, uri)) {
RemoteEndpoint.Async remote = session.getAsyncRemote();
Future<Void> future = remote.sendText("batch_mode_auto");
// The write is immediate, as per the specification.
future.get(1, TimeUnit.SECONDS);
// Wait for the echo.
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
}
Aggregations