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