Search in sources :

Example 1 with LeakTrackingBufferPoolRule

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

the class TooFastClientTest method testUpgradeWithSmallFrames.

@Test
@Ignore("RELEASE")
public void testUpgradeWithSmallFrames() throws Exception {
    BlockheadClient client = new BlockheadClient(server.getServerUri());
    try {
        client.connect();
        // Create ByteBuffer representing the initial opening network packet from the client
        ByteBuffer initialPacket = ByteBuffer.allocate(4096);
        BufferUtil.clearToFill(initialPacket);
        // Add upgrade request to packet
        StringBuilder upgradeRequest = client.generateUpgradeRequest();
        ByteBuffer upgradeBuffer = BufferUtil.toBuffer(upgradeRequest.toString(), StandardCharsets.UTF_8);
        initialPacket.put(upgradeBuffer);
        // Add text frames
        Generator generator = new Generator(WebSocketPolicy.newClientPolicy(), new LeakTrackingBufferPoolRule("Generator"));
        String msg1 = "Echo 1";
        String msg2 = "This is also an echooooo!";
        TextFrame frame1 = new TextFrame().setPayload(msg1);
        TextFrame frame2 = new TextFrame().setPayload(msg2);
        // Need to set frame mask (as these are client frames)
        byte[] mask = new byte[] { 0x11, 0x22, 0x33, 0x44 };
        frame1.setMask(mask);
        frame2.setMask(mask);
        generator.generateWholeFrame(frame1, initialPacket);
        generator.generateWholeFrame(frame2, initialPacket);
        // Write packet to network
        BufferUtil.flipToFlush(initialPacket, 0);
        client.writeRaw(initialPacket);
        // Expect upgrade
        client.expectUpgradeResponse();
        // Read frames (hopefully text frames)
        EventQueue<WebSocketFrame> frames = client.readFrames(2, 1, TimeUnit.SECONDS);
        WebSocketFrame tf = frames.poll();
        Assert.assertThat("Text Frame/msg1", tf.getPayloadAsUTF8(), is(msg1));
        tf = frames.poll();
        Assert.assertThat("Text Frame/msg2", tf.getPayloadAsUTF8(), is(msg2));
    } finally {
        client.close();
    }
}
Also used : LeakTrackingBufferPoolRule(org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPoolRule) BlockheadClient(org.eclipse.jetty.websocket.common.test.BlockheadClient) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame) ByteBuffer(java.nio.ByteBuffer) Generator(org.eclipse.jetty.websocket.common.Generator) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

ByteBuffer (java.nio.ByteBuffer)1 Generator (org.eclipse.jetty.websocket.common.Generator)1 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)1 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)1 BlockheadClient (org.eclipse.jetty.websocket.common.test.BlockheadClient)1 LeakTrackingBufferPoolRule (org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPoolRule)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1