Search in sources :

Example 6 with IBlockheadServerConnection

use of org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection in project jetty.project by eclipse.

the class ClientCloseTest method testNetworkCongestion.

@Ignore("Need sbordet's help here")
@Test
public void testNetworkCongestion() throws Exception {
    // Set client timeout
    final int timeout = 1000;
    client.setMaxIdleTimeout(timeout);
    // Client connects
    CloseTrackingSocket clientSocket = new CloseTrackingSocket();
    Future<Session> clientConnectFuture = client.connect(clientSocket, server.getWsUri());
    // Server accepts connect
    IBlockheadServerConnection serverConn = server.accept();
    serverConn.upgrade();
    // client confirms connection via echo
    confirmConnection(clientSocket, clientConnectFuture, serverConn);
    // client sends BIG frames (until it cannot write anymore)
    // server must not read (for test purpose, in order to congest connection)
    // when write is congested, client enqueue close frame
    // client initiate write, but write never completes
    EndPoint endp = clientSocket.getEndPoint();
    assertThat("EndPoint is testable", endp, instanceOf(TestEndPoint.class));
    TestEndPoint testendp = (TestEndPoint) endp;
    char[] msg = new char[10240];
    int writeCount = 0;
    long writeSize = 0;
    int i = 0;
    while (!testendp.congestedFlush.get()) {
        int z = i - ((i / 26) * 26);
        char c = (char) ('a' + z);
        Arrays.fill(msg, c);
        clientSocket.getRemote().sendStringByFuture(String.valueOf(msg));
        writeCount++;
        writeSize += msg.length;
    }
    LOG.info("Wrote {} frames totalling {} bytes of payload before congestion kicked in", writeCount, writeSize);
    // Verify timeout error
    assertThat("OnError Latch", clientSocket.errorLatch.await(2, TimeUnit.SECONDS), is(true));
    assertThat("OnError", clientSocket.error.get(), instanceOf(SocketTimeoutException.class));
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) EndPoint(org.eclipse.jetty.io.EndPoint) SocketChannelEndPoint(org.eclipse.jetty.io.SocketChannelEndPoint) EndPoint(org.eclipse.jetty.io.EndPoint) SocketChannelEndPoint(org.eclipse.jetty.io.SocketChannelEndPoint) Session(org.eclipse.jetty.websocket.api.Session) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 7 with IBlockheadServerConnection

use of org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection in project jetty.project by eclipse.

the class ClientCloseTest method testProtocolException.

@Test
public void testProtocolException() throws Exception {
    // Set client timeout
    final int timeout = 1000;
    client.setMaxIdleTimeout(timeout);
    // Client connects
    CloseTrackingSocket clientSocket = new CloseTrackingSocket();
    Future<Session> clientConnectFuture = client.connect(clientSocket, server.getWsUri());
    // Server accepts connect
    IBlockheadServerConnection serverConn = server.accept();
    serverConn.upgrade();
    // client confirms connection via echo
    confirmConnection(clientSocket, clientConnectFuture, serverConn);
    // client should not have received close message (yet)
    clientSocket.assertNoCloseEvent();
    // server sends bad close frame (too big of a reason message)
    byte[] msg = new byte[400];
    Arrays.fill(msg, (byte) 'x');
    ByteBuffer bad = ByteBuffer.allocate(500);
    RawFrameBuilder.putOpFin(bad, OpCode.CLOSE, true);
    RawFrameBuilder.putLength(bad, msg.length + 2, false);
    bad.putShort((short) StatusCode.NORMAL);
    bad.put(msg);
    BufferUtil.flipToFlush(bad, 0);
    try (StacklessLogging quiet = new StacklessLogging(Parser.class)) {
        serverConn.write(bad);
        // client should have noticed the error
        assertThat("OnError Latch", clientSocket.errorLatch.await(2, TimeUnit.SECONDS), is(true));
        assertThat("OnError", clientSocket.error.get(), instanceOf(ProtocolException.class));
        assertThat("OnError", clientSocket.error.get().getMessage(), containsString("Invalid control frame"));
        // client parse invalid frame, notifies server of close (protocol error)
        confirmServerReceivedCloseFrame(serverConn, StatusCode.PROTOCOL, allOf(containsString("Invalid control frame"), containsString("length")));
    }
    // server disconnects
    serverConn.disconnect();
    // client triggers close event on client ws-endpoint
    clientSocket.assertReceivedCloseEvent(timeout, is(StatusCode.PROTOCOL), allOf(containsString("Invalid control frame"), containsString("length")));
}
Also used : ProtocolException(org.eclipse.jetty.websocket.api.ProtocolException) IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) ByteBuffer(java.nio.ByteBuffer) EndPoint(org.eclipse.jetty.io.EndPoint) SocketChannelEndPoint(org.eclipse.jetty.io.SocketChannelEndPoint) Session(org.eclipse.jetty.websocket.api.Session) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Test(org.junit.Test)

Example 8 with IBlockheadServerConnection

use of org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection 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();
    }
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) URI(java.net.URI) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Session(org.eclipse.jetty.websocket.api.Session) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 9 with IBlockheadServerConnection

use of org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection 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();
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 10 with IBlockheadServerConnection

use of org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection 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();
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Aggregations

IBlockheadServerConnection (org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection)33 Test (org.junit.Test)33 Session (org.eclipse.jetty.websocket.api.Session)32 URI (java.net.URI)23 WebSocketSession (org.eclipse.jetty.websocket.common.WebSocketSession)15 ExecutionException (java.util.concurrent.ExecutionException)7 EndPoint (org.eclipse.jetty.io.EndPoint)7 SocketChannelEndPoint (org.eclipse.jetty.io.SocketChannelEndPoint)7 UpgradeException (org.eclipse.jetty.websocket.api.UpgradeException)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 Slow (org.eclipse.jetty.toolchain.test.annotation.Slow)3 RemoteEndpoint (org.eclipse.jetty.websocket.api.RemoteEndpoint)3 HttpCookie (java.net.HttpCookie)2 SocketTimeoutException (java.net.SocketTimeoutException)2 ByteBuffer (java.nio.ByteBuffer)2 ZeroMasker (org.eclipse.jetty.websocket.client.masks.ZeroMasker)2 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)2 Ignore (org.junit.Ignore)2 ConnectException (java.net.ConnectException)1 CookieManager (java.net.CookieManager)1