Search in sources :

Example 51 with CloseInfo

use of org.eclipse.jetty.websocket.common.CloseInfo in project jetty.project by eclipse.

the class WebSocketServletRFCTest method testInternalError.

/**
     * Test the requirement of responding with server terminated close code 1011 when there is an unhandled (internal server error) being produced by the
     * WebSocket POJO.
     * @throws Exception on test failure
     */
@Test
public void testInternalError() throws Exception {
    try (BlockheadClient client = new BlockheadClient(server.getServerUri());
        StacklessLogging stackless = new StacklessLogging(EventDriver.class)) {
        client.connect();
        client.sendStandardRequest();
        client.expectUpgradeResponse();
        try (StacklessLogging context = new StacklessLogging(EventDriver.class)) {
            // Generate text frame
            client.write(new TextFrame().setPayload("CRASH"));
            // Read frame (hopefully close frame)
            EventQueue<WebSocketFrame> frames = client.readFrames(1, 30, TimeUnit.SECONDS);
            Frame cf = frames.poll();
            CloseInfo close = new CloseInfo(cf);
            Assert.assertThat("Close Frame.status code", close.getStatusCode(), is(StatusCode.SERVER_ERROR));
        }
    }
}
Also used : BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) Frame(org.eclipse.jetty.websocket.api.extensions.Frame) ContinuationFrame(org.eclipse.jetty.websocket.common.frames.ContinuationFrame) 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 52 with CloseInfo

use of org.eclipse.jetty.websocket.common.CloseInfo in project jetty.project by eclipse.

the class ManyConnectionsCleanupTest method fastClose.

private void fastClose() throws Exception {
    try (IBlockheadClient client = new BlockheadClient(server.getServerUri())) {
        client.setProtocols("fastclose");
        client.setTimeout(1, TimeUnit.SECONDS);
        try (StacklessLogging scope = new StacklessLogging(WebSocketSession.class)) {
            client.connect();
            client.sendStandardRequest();
            client.expectUpgradeResponse();
            client.readFrames(1, 1, TimeUnit.SECONDS);
            CloseInfo close = new CloseInfo(StatusCode.NORMAL, "Normal");
            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(1, 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) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) IBlockheadClient(org.eclipse.jetty.websocket.common.test.IBlockheadClient) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo)

Example 53 with CloseInfo

use of org.eclipse.jetty.websocket.common.CloseInfo in project jetty.project by eclipse.

the class ManyConnectionsCleanupTest method fastFail.

private void fastFail() throws Exception {
    try (IBlockheadClient client = new BlockheadClient(server.getServerUri())) {
        client.setProtocols("fastfail");
        client.setTimeout(1, TimeUnit.SECONDS);
        try (StacklessLogging scope = new StacklessLogging(WebSocketSession.class)) {
            client.connect();
            client.sendStandardRequest();
            client.expectUpgradeResponse();
            // client.readFrames(1,2,TimeUnit.SECONDS);
            CloseInfo close = new CloseInfo(StatusCode.NORMAL, "Normal");
            // respond with close
            client.write(close.asFrame());
            // ensure server socket got close event
            assertThat("Fast Fail Latch", closeSocket.closeLatch.await(1, TimeUnit.SECONDS), is(true));
            assertThat("Fast Fail.statusCode", closeSocket.closeStatusCode, is(StatusCode.SERVER_ERROR));
            assertThat("Fast Fail.errors", closeSocket.errors.size(), is(1));
        }
    }
}
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) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo)

Example 54 with CloseInfo

use of org.eclipse.jetty.websocket.common.CloseInfo in project jetty.project by eclipse.

the class BlockheadClient method onConnectionStateChange.

@Override
public void onConnectionStateChange(ConnectionState state) {
    LOG.debug("CLIENT onConnectionStateChange() - {}", state);
    switch(state) {
        case CLOSED:
            // this.disconnect();
            break;
        case CLOSING:
            CloseInfo close = ioState.getCloseInfo();
            WebSocketFrame frame = close.asFrame();
            LOG.debug("Issuing: {}", frame);
            try {
                write(frame);
            } catch (IOException e) {
                LOG.debug(e);
            }
            break;
        default:
            /* do nothing */
            break;
    }
}
Also used : WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) IOException(java.io.IOException) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo)

Example 55 with CloseInfo

use of org.eclipse.jetty.websocket.common.CloseInfo in project jetty.project by eclipse.

the class BlockheadServerConnection method incomingFrame.

@Override
public void incomingFrame(Frame frame) {
    LOG.debug("incoming({})", frame);
    int count = parseCount.incrementAndGet();
    if ((count % 10) == 0) {
        LOG.info("Server parsed {} frames", count);
    }
    incomingFrames.incomingFrame(WebSocketFrame.copy(frame));
    if (frame.getOpCode() == OpCode.CLOSE) {
        CloseInfo close = new CloseInfo(frame);
        LOG.debug("Close frame: {}", close);
    }
    Type type = frame.getType();
    if (echoing.get() && (type.isData() || type.isContinuation())) {
        try {
            write(WebSocketFrame.copy(frame).setMasked(false));
        } catch (IOException e) {
            LOG.warn(e);
        }
    }
}
Also used : Type(org.eclipse.jetty.websocket.api.extensions.Frame.Type) IOException(java.io.IOException) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo)

Aggregations

CloseInfo (org.eclipse.jetty.websocket.common.CloseInfo)151 Test (org.junit.Test)129 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)118 ArrayList (java.util.ArrayList)104 Fuzzer (org.eclipse.jetty.websocket.common.test.Fuzzer)104 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)68 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)55 ByteBuffer (java.nio.ByteBuffer)48 PingFrame (org.eclipse.jetty.websocket.common.frames.PingFrame)30 ContinuationFrame (org.eclipse.jetty.websocket.common.frames.ContinuationFrame)27 PongFrame (org.eclipse.jetty.websocket.common.frames.PongFrame)19 BinaryFrame (org.eclipse.jetty.websocket.common.frames.BinaryFrame)17 BlockheadClient (org.eclipse.jetty.websocket.common.test.BlockheadClient)14 IBlockheadClient (org.eclipse.jetty.websocket.common.test.IBlockheadClient)10 CloseableLocalWebSocketSession (org.eclipse.jetty.websocket.common.io.CloseableLocalWebSocketSession)8 LocalWebSocketSession (org.eclipse.jetty.websocket.common.io.LocalWebSocketSession)8 Stress (org.eclipse.jetty.toolchain.test.annotation.Stress)6 Slow (org.eclipse.jetty.toolchain.test.annotation.Slow)5 CloseFrame (org.eclipse.jetty.websocket.common.frames.CloseFrame)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4