use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.
the class BasicEndpointTest method testEcho.
@Test
public void testEcho() throws Exception {
WSServer wsb = new WSServer(testdir, "app");
wsb.copyWebInf("basic-echo-endpoint-config-web.xml");
// the endpoint (extends javax.websocket.Endpoint)
wsb.copyClass(BasicEchoEndpoint.class);
// the configuration (adds the endpoint)
wsb.copyClass(BasicEchoEndpointConfigContextListener.class);
try {
wsb.start();
URI uri = wsb.getServerBaseURI();
WebAppContext webapp = wsb.createWebAppContext();
wsb.deployWebapp(webapp);
WebSocketClient client = new WebSocketClient(bufferPool);
try {
client.start();
JettyEchoSocket clientEcho = new JettyEchoSocket();
Future<Session> future = client.connect(clientEcho, uri.resolve("echo"));
// wait for connect
future.get(1, TimeUnit.SECONDS);
clientEcho.sendMessage("Hello World");
Queue<String> msgs = clientEcho.awaitMessages(1);
Assert.assertEquals("Expected message", "Hello World", msgs.poll());
} finally {
client.stop();
}
} finally {
wsb.stop();
}
}
use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.
the class SessionTest method testBasicEcho_FromClient.
@Test
// TODO fix frequent failure
@Ignore
public void testBasicEcho_FromClient() throws Exception {
WebSocketClient client = new WebSocketClient();
client.start();
try {
JettyTrackingSocket cliSock = new JettyTrackingSocket();
client.getPolicy().setIdleTimeout(10000);
URI wsUri = server.getWsUri();
ClientUpgradeRequest request = new ClientUpgradeRequest();
request.setSubProtocols("echo");
Future<Session> future = client.connect(cliSock, wsUri, request);
final IBlockheadServerConnection srvSock = server.accept();
srvSock.upgrade();
Session sess = future.get(30000, TimeUnit.MILLISECONDS);
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());
cliSock.assertWasOpened();
cliSock.assertNotClosed();
Collection<WebSocketSession> sessions = client.getBeans(WebSocketSession.class);
Assert.assertThat("client.connectionManager.sessions.size", sessions.size(), is(1));
RemoteEndpoint remote = cliSock.getSession().getRemote();
remote.sendStringByFuture("Hello World!");
if (remote.getBatchMode() == BatchMode.ON) {
remote.flush();
}
srvSock.echoMessage(1, 30000, TimeUnit.MILLISECONDS);
// wait for response from server
cliSock.waitForMessage(30000, TimeUnit.MILLISECONDS);
Set<WebSocketSession> open = client.getOpenSessions();
Assert.assertThat("(Before Close) Open Sessions.size", open.size(), is(1));
cliSock.assertMessage("Hello World!");
cliSock.close();
srvSock.close();
cliSock.waitForClose(30000, TimeUnit.MILLISECONDS);
open = client.getOpenSessions();
// TODO this sometimes fails!
Assert.assertThat("(After Close) Open Sessions.size", open.size(), is(0));
} finally {
client.stop();
}
}
use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.
the class SlowClientTest method testClientSlowToSend.
@Test
@Slow
public void testClientSlowToSend() throws Exception {
JettyTrackingSocket tsocket = new JettyTrackingSocket();
client.getPolicy().setIdleTimeout(60000);
URI wsUri = server.getWsUri();
Future<Session> future = client.connect(tsocket, wsUri);
IBlockheadServerConnection sconnection = server.accept();
sconnection.setSoTimeout(60000);
sconnection.upgrade();
// Confirm connected
future.get(30, TimeUnit.SECONDS);
tsocket.waitForConnected(30, TimeUnit.SECONDS);
int messageCount = 10;
// Setup server read thread
ServerReadThread reader = new ServerReadThread(sconnection, messageCount);
reader.start();
// Have client write slowly.
ClientWriteThread writer = new ClientWriteThread(tsocket.getSession());
writer.setMessageCount(messageCount);
writer.setMessage("Hello");
writer.setSlowness(10);
writer.start();
writer.join();
reader.waitForExpectedMessageCount(1, TimeUnit.MINUTES);
// Verify receive
Assert.assertThat("Frame Receive Count", reader.getFrameCount(), is(messageCount));
// Close
tsocket.getSession().close(StatusCode.NORMAL, "Done");
Assert.assertTrue("Client Socket Closed", tsocket.closeLatch.await(3, TimeUnit.MINUTES));
tsocket.assertCloseCode(StatusCode.NORMAL);
// stop reading
reader.cancel();
}
use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.
the class ClientConnectTest method testUpgradeRequest.
@Test
public void testUpgradeRequest() throws Exception {
JettyTrackingSocket wsocket = new JettyTrackingSocket();
URI wsUri = server.getWsUri();
Future<Session> future = client.connect(wsocket, wsUri);
IBlockheadServerConnection connection = server.accept();
connection.upgrade();
Session sess = future.get(30, TimeUnit.SECONDS);
wsocket.waitForConnected(1, TimeUnit.SECONDS);
assertThat("Connect.UpgradeRequest", wsocket.connectUpgradeRequest, notNullValue());
assertThat("Connect.UpgradeResponse", wsocket.connectUpgradeResponse, notNullValue());
sess.close();
}
use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.
the class ClientConnectTest method testBadHandshake.
@Test
public void testBadHandshake() throws Exception {
JettyTrackingSocket wsocket = new JettyTrackingSocket();
URI wsUri = server.getWsUri();
Future<Session> future = client.connect(wsocket, wsUri);
IBlockheadServerConnection connection = server.accept();
connection.readRequest();
// no upgrade, just fail with a 404 error
connection.respond("HTTP/1.1 404 NOT FOUND\r\n" + "Content-Length: 0\r\n" + "\r\n");
// The attempt to get upgrade response future should throw error
try {
future.get(30, TimeUnit.SECONDS);
Assert.fail("Expected ExecutionException -> UpgradeException");
} catch (ExecutionException e) {
// Expected Path
UpgradeException ue = assertExpectedError(e, wsocket, UpgradeException.class);
Assert.assertThat("UpgradeException.requestURI", ue.getRequestURI(), notNullValue());
Assert.assertThat("UpgradeException.requestURI", ue.getRequestURI().toASCIIString(), is(wsUri.toASCIIString()));
Assert.assertThat("UpgradeException.responseStatusCode", ue.getResponseStatusCode(), is(404));
}
}
Aggregations