Search in sources :

Example 36 with BlockheadClient

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

the class WebSocketCloseTest method testFastClose.

/**
     * Test fast close (bug #403817)
     * 
     * @throws Exception
     *             on test failure
     */
@Test
public void testFastClose() throws Exception {
    try (IBlockheadClient client = new BlockheadClient(server.getServerUri())) {
        client.setProtocols("fastclose");
        client.setTimeout(5, TimeUnit.SECONDS);
        client.connect();
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        // Verify that client got close frame
        EventQueue<WebSocketFrame> frames = client.readFrames(1, 5, TimeUnit.SECONDS);
        WebSocketFrame frame = frames.poll();
        assertThat("frames[0].opcode", frame.getOpCode(), is(OpCode.CLOSE));
        CloseInfo close = new CloseInfo(frame);
        assertThat("Close Status Code", close.getStatusCode(), is(StatusCode.NORMAL));
        // Notify server of close handshake
        // respond with close
        client.write(close.asFrame());
        // ensure server socket got close event
        assertThat("Fast Close Latch", closeSocket.closeLatch.await(5, TimeUnit.SECONDS), is(true));
        assertThat("Fast Close.statusCode", closeSocket.closeStatusCode, is(StatusCode.NORMAL));
    }
}
Also used : IBlockheadClient(org.eclipse.jetty.websocket.common.test.IBlockheadClient) BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) IBlockheadClient(org.eclipse.jetty.websocket.common.test.IBlockheadClient) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) Test(org.junit.Test)

Example 37 with BlockheadClient

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

the class AnnotatedMaxMessageSizeTest method testEchoGood.

@Test
public void testEchoGood() throws IOException, Exception {
    BlockheadClient client = new BlockheadClient(serverUri);
    try {
        client.setProtocols("echo");
        client.connect();
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        // Generate text frame
        String msg = "this is an echo ... cho ... ho ... o";
        client.write(new TextFrame().setPayload(msg));
        // Read frame (hopefully text frame)
        EventQueue<WebSocketFrame> frames = client.readFrames(1, 30, TimeUnit.SECONDS);
        WebSocketFrame tf = frames.poll();
        Assert.assertThat("Text Frame.status code", tf.getPayloadAsUTF8(), is(msg));
    } finally {
        client.close();
    }
}
Also used : BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) Test(org.junit.Test)

Example 38 with BlockheadClient

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

the class AnnotatedMaxMessageSizeTest method testEchoTooBig.

@Test(timeout = 8000)
public void testEchoTooBig() throws IOException, Exception {
    BlockheadClient client = new BlockheadClient(serverUri);
    try (StacklessLogging logging = new StacklessLogging(Parser.class)) {
        client.setProtocols("echo");
        client.connect();
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        // Generate text frame
        int size = 120 * 1024;
        // buffer bigger than maxMessageSize
        byte[] buf = new byte[size];
        Arrays.fill(buf, (byte) 'x');
        client.write(new TextFrame().setPayload(ByteBuffer.wrap(buf)));
        // Read frame (hopefully close frame saying its too large)
        EventQueue<WebSocketFrame> frames = client.readFrames(1, 30, TimeUnit.SECONDS);
        WebSocketFrame tf = frames.poll();
        Assert.assertThat("Frame is close", tf.getOpCode(), is(OpCode.CLOSE));
        CloseInfo close = new CloseInfo(tf);
        Assert.assertThat("Close Code", close.getStatusCode(), is(StatusCode.MESSAGE_TOO_LARGE));
    } finally {
        client.close();
    }
}
Also used : BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) Test(org.junit.Test)

Example 39 with BlockheadClient

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

the class ManyConnectionsCleanupTest method testOpenSessionCleanup.

/**
     * Test session open session cleanup (bug #474936)
     * 
     * @throws Exception
     *             on test failure
     */
@Test
public void testOpenSessionCleanup() throws Exception {
    int iterationCount = 100;
    StdErrLog.getLogger(FastFailSocket.class).setLevel(StdErrLog.LEVEL_OFF);
    StdErrLog sessLog = StdErrLog.getLogger(WebSocketSession.class);
    int oldLevel = sessLog.getLevel();
    sessLog.setLevel(StdErrLog.LEVEL_OFF);
    for (int requests = 0; requests < iterationCount; requests++) {
        fastFail();
        fastClose();
        dropConnection();
    }
    sessLog.setLevel(oldLevel);
    try (IBlockheadClient client = new BlockheadClient(server.getServerUri())) {
        client.setProtocols("container");
        client.setTimeout(1, TimeUnit.SECONDS);
        client.connect();
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        client.write(new TextFrame().setPayload("calls"));
        client.write(new TextFrame().setPayload("openSessions"));
        EventQueue<WebSocketFrame> frames = client.readFrames(3, 6, TimeUnit.SECONDS);
        WebSocketFrame frame;
        String resp;
        frame = frames.poll();
        assertThat("frames[0].opcode", frame.getOpCode(), is(OpCode.TEXT));
        resp = frame.getPayloadAsUTF8();
        assertThat("Should only have 1 open session", resp, containsString("calls=" + ((iterationCount * 2) + 1)));
        frame = frames.poll();
        assertThat("frames[1].opcode", frame.getOpCode(), is(OpCode.TEXT));
        resp = frame.getPayloadAsUTF8();
        assertThat("Should only have 1 open session", resp, containsString("openSessions.size=1\n"));
        frame = frames.poll();
        assertThat("frames[2].opcode", frame.getOpCode(), is(OpCode.CLOSE));
        CloseInfo close = new CloseInfo(frame);
        assertThat("Close Status Code", close.getStatusCode(), is(StatusCode.NORMAL));
        // respond with close
        client.write(close.asFrame());
        // ensure server socket got close event
        assertThat("Open Sessions Latch", closeSocket.closeLatch.await(1, TimeUnit.SECONDS), is(true));
        assertThat("Open Sessions.statusCode", closeSocket.closeStatusCode, is(StatusCode.NORMAL));
        assertThat("Open Sessions.errors", closeSocket.errors.size(), is(0));
    }
}
Also used : IBlockheadClient(org.eclipse.jetty.websocket.common.test.IBlockheadClient) BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) StdErrLog(org.eclipse.jetty.util.log.StdErrLog) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) IBlockheadClient(org.eclipse.jetty.websocket.common.test.IBlockheadClient) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) Test(org.junit.Test)

Example 40 with BlockheadClient

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

the class ManyConnectionsCleanupTest method dropConnection.

private void dropConnection() throws Exception {
    try (IBlockheadClient client = new BlockheadClient(server.getServerUri())) {
        client.setProtocols("container");
        client.setTimeout(1, TimeUnit.SECONDS);
        try (StacklessLogging scope = new StacklessLogging(WebSocketSession.class)) {
            client.connect();
            client.sendStandardRequest();
            client.expectUpgradeResponse();
            client.disconnect();
        }
    }
}
Also used : IBlockheadClient(org.eclipse.jetty.websocket.common.test.IBlockheadClient) BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) IBlockheadClient(org.eclipse.jetty.websocket.common.test.IBlockheadClient)

Aggregations

BlockheadClient (org.eclipse.jetty.websocket.common.test.BlockheadClient)47 Test (org.junit.Test)40 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)37 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)34 IBlockheadClient (org.eclipse.jetty.websocket.common.test.IBlockheadClient)25 URI (java.net.URI)14 CloseInfo (org.eclipse.jetty.websocket.common.CloseInfo)14 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)12 Matchers.containsString (org.hamcrest.Matchers.containsString)7 HttpResponse (org.eclipse.jetty.websocket.common.test.HttpResponse)6 ByteBuffer (java.nio.ByteBuffer)4 Utf8StringBuilder (org.eclipse.jetty.util.Utf8StringBuilder)2 Frame (org.eclipse.jetty.websocket.api.extensions.Frame)2 Generator (org.eclipse.jetty.websocket.common.Generator)2 BinaryFrame (org.eclipse.jetty.websocket.common.frames.BinaryFrame)2 ContinuationFrame (org.eclipse.jetty.websocket.common.frames.ContinuationFrame)2 Ignore (org.junit.Ignore)2 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 HttpCookie (java.net.HttpCookie)1