Search in sources :

Example 26 with Session

use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.

the class WebSocketScopeSessionTest method testMultiSession_Overlapping.

@Test
public void testMultiSession_Overlapping() throws Exception {
    final CountDownLatch midLatch = new CountDownLatch(2);
    final CountDownLatch end1Latch = new CountDownLatch(1);
    Callable<Session> call1 = new Callable<Session>() {

        @Override
        public Session call() throws Exception {
            Session ret = null;
            ScopedInstance<WebSocketScopeContext> wsScope1Bean = newInstance(WebSocketScopeContext.class);
            WebSocketScopeContext wsScope1 = wsScope1Bean.instance;
            wsScope1.create();
            try {
                // Scope 1
                wsScope1.begin();
                BogusSession sess = new BogusSession("1");
                wsScope1.setSession(sess);
                midLatch.countDown();
                midLatch.await(1, TimeUnit.SECONDS);
                ScopedInstance<BogusSocket> sock1Bean = newInstance(BogusSocket.class);
                BogusSocket sock1 = sock1Bean.instance;
                assertThat("Socket 1 Session", sock1.getSession(), sameInstance((Session) sess));
                ret = sock1.getSession();
                sock1Bean.destroy();
            } finally {
                wsScope1.end();
            }
            wsScope1.destroy();
            wsScope1Bean.destroy();
            end1Latch.countDown();
            return ret;
        }
    };
    final CountDownLatch end2Latch = new CountDownLatch(1);
    Callable<Session> call2 = new Callable<Session>() {

        @Override
        public Session call() throws Exception {
            Session ret = null;
            ScopedInstance<WebSocketScopeContext> wsScope2Bean = newInstance(WebSocketScopeContext.class);
            WebSocketScopeContext wsScope2 = wsScope2Bean.instance;
            wsScope2.create();
            try {
                // Scope 2
                wsScope2.begin();
                BogusSession sess = new BogusSession("2");
                wsScope2.setSession(sess);
                ScopedInstance<BogusSocket> sock2Bean = newInstance(BogusSocket.class);
                midLatch.countDown();
                midLatch.await(1, TimeUnit.SECONDS);
                BogusSocket sock2 = sock2Bean.instance;
                ret = sock2.getSession();
                assertThat("Socket 2 Session", sock2.getSession(), sameInstance((Session) sess));
                sock2Bean.destroy();
            } finally {
                wsScope2.end();
            }
            wsScope2.destroy();
            wsScope2Bean.destroy();
            end2Latch.countDown();
            return ret;
        }
    };
    ExecutorService svc = Executors.newFixedThreadPool(4);
    Future<Session> fut1 = svc.submit(call1);
    Future<Session> fut2 = svc.submit(call2);
    Session sess1 = fut1.get(1, TimeUnit.SECONDS);
    Session sess2 = fut2.get(1, TimeUnit.SECONDS);
    assertThat("Sessions are different", sess1, not(sameInstance(sess2)));
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) WebSocketScopeContext(org.eclipse.jetty.cdi.websocket.WebSocketScopeContext) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 27 with Session

use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.

the class WebSocketClientTest method testMessageBiggerThanBufferSize.

@Test
public void testMessageBiggerThanBufferSize() throws Exception {
    int bufferSize = 512;
    JettyTrackingSocket wsocket = new JettyTrackingSocket();
    URI wsUri = server.getWsUri();
    Future<Session> future = client.connect(wsocket, wsUri);
    IBlockheadServerConnection ssocket = server.accept();
    ssocket.upgrade();
    future.get(30, TimeUnit.SECONDS);
    Assert.assertTrue(wsocket.openLatch.await(1, TimeUnit.SECONDS));
    // 1.5 times buffer size
    int length = bufferSize + (bufferSize / 2);
    // FIN + TEXT
    ssocket.write(0x80 | 0x01);
    // No MASK and 2 bytes length
    ssocket.write(0x7E);
    // first length byte
    ssocket.write(length >> 8);
    // second length byte
    ssocket.write(length & 0xFF);
    for (int i = 0; i < length; ++i) {
        ssocket.write('x');
    }
    ssocket.flush();
    Assert.assertTrue(wsocket.dataLatch.await(1000, TimeUnit.SECONDS));
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) URI(java.net.URI) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) Session(org.eclipse.jetty.websocket.api.Session) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Test(org.junit.Test)

Example 28 with Session

use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.

the class WebSocketClientTest method testBasicEcho_FromServer.

@Test
public void testBasicEcho_FromServer() throws Exception {
    JettyTrackingSocket wsocket = new JettyTrackingSocket();
    Future<Session> future = client.connect(wsocket, server.getWsUri());
    // Server
    final IBlockheadServerConnection srvSock = server.accept();
    srvSock.upgrade();
    // Validate connect
    Session sess = future.get(30, TimeUnit.SECONDS);
    Assert.assertThat("Session", sess, notNullValue());
    Assert.assertThat("Session.open", sess.isOpen(), is(true));
    Assert.assertThat("Session.upgradeRequest", sess.getUpgradeRequest(), notNullValue());
    Assert.assertThat("Session.upgradeResponse", sess.getUpgradeResponse(), notNullValue());
    // Have server send initial message
    srvSock.write(new TextFrame().setPayload("Hello World"));
    // Verify connect
    future.get(30, TimeUnit.SECONDS);
    wsocket.assertWasOpened();
    wsocket.awaitMessage(1, TimeUnit.SECONDS, 2);
    wsocket.assertMessage("Hello World");
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) Session(org.eclipse.jetty.websocket.api.Session) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Test(org.junit.Test)

Example 29 with Session

use of org.eclipse.jetty.websocket.api.Session in project neo4j by neo4j.

the class WebSocketConnection method connect.

@Override
public TransportConnection connect(HostnamePort address) throws Exception {
    URI target = uriGenerator.apply(address);
    client = clientSupplier.get();
    client.start();
    Session session;
    try {
        session = client.connect(this, target).get(10, SECONDS);
    } catch (Exception e) {
        throw new IOException("Failed to connect to the server within 10 seconds", e);
    }
    server = session.getRemote();
    return this;
}
Also used : IOException(java.io.IOException) URI(java.net.URI) IOException(java.io.IOException) Session(org.eclipse.jetty.websocket.api.Session)

Example 30 with Session

use of org.eclipse.jetty.websocket.api.Session in project spring-framework by spring-projects.

the class JettyWebSocketSessionTests method getAcceptedProtocol.

@Test
@SuppressWarnings("resource")
public void getAcceptedProtocol() {
    String protocol = "foo";
    UpgradeRequest request = Mockito.mock(UpgradeRequest.class);
    given(request.getUserPrincipal()).willReturn(null);
    UpgradeResponse response = Mockito.mock(UpgradeResponse.class);
    given(response.getAcceptedSubProtocol()).willReturn(protocol);
    Session nativeSession = Mockito.mock(Session.class);
    given(nativeSession.getUpgradeRequest()).willReturn(request);
    given(nativeSession.getUpgradeResponse()).willReturn(response);
    JettyWebSocketSession session = new JettyWebSocketSession(attributes);
    session.initializeNativeSession(nativeSession);
    reset(nativeSession);
    assertSame(protocol, session.getAcceptedProtocol());
    verifyNoMoreInteractions(nativeSession);
}
Also used : UpgradeResponse(org.eclipse.jetty.websocket.api.UpgradeResponse) UpgradeRequest(org.eclipse.jetty.websocket.api.UpgradeRequest) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Aggregations

Session (org.eclipse.jetty.websocket.api.Session)74 Test (org.junit.Test)57 URI (java.net.URI)46 IBlockheadServerConnection (org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection)32 WebSocketClient (org.eclipse.jetty.websocket.client.WebSocketClient)23 WebSocketSession (org.eclipse.jetty.websocket.common.WebSocketSession)21 ExecutionException (java.util.concurrent.ExecutionException)12 ClientUpgradeRequest (org.eclipse.jetty.websocket.client.ClientUpgradeRequest)9 HashMap (java.util.HashMap)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 Envelope (com.kixeye.chassis.transport.dto.Envelope)7 MessageSerDe (com.kixeye.chassis.transport.serde.MessageSerDe)7 ProtobufMessageSerDe (com.kixeye.chassis.transport.serde.converter.ProtobufMessageSerDe)7 QueuingWebSocketListener (com.kixeye.chassis.transport.websocket.QueuingWebSocketListener)7 WebSocketMessageRegistry (com.kixeye.chassis.transport.websocket.WebSocketMessageRegistry)7 EndPoint (org.eclipse.jetty.io.EndPoint)7 SocketChannelEndPoint (org.eclipse.jetty.io.SocketChannelEndPoint)7 RemoteEndpoint (org.eclipse.jetty.websocket.api.RemoteEndpoint)7 UpgradeException (org.eclipse.jetty.websocket.api.UpgradeException)7 MapPropertySource (org.springframework.core.env.MapPropertySource)7