Search in sources :

Example 16 with Session

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");
    }
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) TesterProgrammaticEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint) Endpoint(javax.websocket.Endpoint) ServerEndpoint(javax.websocket.server.ServerEndpoint) TesterEndpoint(org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint) Session(javax.websocket.Session) Test(org.junit.Test)

Example 17 with Session

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);
}
Also used : Session(javax.websocket.Session) Test(org.testng.annotations.Test)

Example 18 with 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));
}
Also used : MemoryMXBean(java.lang.management.MemoryMXBean) CountDownLatch(java.util.concurrent.CountDownLatch) MemoryUsage(java.lang.management.MemoryUsage) URI(java.net.URI) Endpoint(javax.websocket.Endpoint) BasicEchoEndpoint(org.eclipse.jetty.websocket.jsr356.server.samples.echo.BasicEchoEndpoint) Session(javax.websocket.Session) Test(org.junit.Test)

Example 19 with Session

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));
    }
}
Also used : RemoteEndpoint(javax.websocket.RemoteEndpoint) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) Session(javax.websocket.Session) Test(org.junit.Test)

Example 20 with Session

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));
    }
}
Also used : RemoteEndpoint(javax.websocket.RemoteEndpoint) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) Session(javax.websocket.Session) Test(org.junit.Test)

Aggregations

Session (javax.websocket.Session)115 Test (org.junit.Test)92 URI (java.net.URI)73 WebSocketContainer (javax.websocket.WebSocketContainer)42 Endpoint (javax.websocket.Endpoint)39 CountDownLatch (java.util.concurrent.CountDownLatch)34 Context (org.apache.catalina.Context)31 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)31 Tomcat (org.apache.catalina.startup.Tomcat)31 EndpointConfig (javax.websocket.EndpointConfig)29 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)27 UndertowSession (io.undertow.websockets.jsr.UndertowSession)25 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)23 ServerEndpoint (javax.websocket.server.ServerEndpoint)20 ServerWebSocketContainer (io.undertow.websockets.jsr.ServerWebSocketContainer)16 IOException (java.io.IOException)16 AnnotatedClientEndpoint (io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint)15 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 FrameChecker (io.undertow.websockets.utils.FrameChecker)14