Search in sources :

Example 51 with ByteBuf

use of io.netty.buffer.ByteBuf in project netty by netty.

the class ChunkedWriteHandlerTest method check.

private static void check(Object... inputs) {
    EmbeddedChannel ch = new EmbeddedChannel(new ChunkedWriteHandler());
    for (Object input : inputs) {
        ch.writeOutbound(input);
    }
    assertTrue(ch.finish());
    int i = 0;
    int read = 0;
    for (; ; ) {
        ByteBuf buffer = ch.readOutbound();
        if (buffer == null) {
            break;
        }
        while (buffer.isReadable()) {
            assertEquals(BYTES[i++], buffer.readByte());
            read++;
            if (i == BYTES.length) {
                i = 0;
            }
        }
        buffer.release();
    }
    assertEquals(BYTES.length * inputs.length, read);
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf)

Example 52 with ByteBuf

use of io.netty.buffer.ByteBuf in project netty by netty.

the class SSLEngineTest method writeAndVerifyReceived.

private static void writeAndVerifyReceived(ByteBuf message, Channel sendChannel, CountDownLatch receiverLatch, MessageReceiver receiver) throws Exception {
    List<ByteBuf> dataCapture = null;
    try {
        sendChannel.writeAndFlush(message);
        receiverLatch.await(5, TimeUnit.SECONDS);
        message.resetReaderIndex();
        ArgumentCaptor<ByteBuf> captor = ArgumentCaptor.forClass(ByteBuf.class);
        verify(receiver).messageReceived(captor.capture());
        dataCapture = captor.getAllValues();
        assertEquals(message, dataCapture.get(0));
    } finally {
        if (dataCapture != null) {
            for (ByteBuf data : dataCapture) {
                data.release();
            }
        }
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 53 with ByteBuf

use of io.netty.buffer.ByteBuf in project netty by netty.

the class SSLEngineTest method runTest.

protected void runTest(String expectedApplicationProtocol) throws Exception {
    final ByteBuf clientMessage = Unpooled.copiedBuffer("I am a client".getBytes());
    final ByteBuf serverMessage = Unpooled.copiedBuffer("I am a server".getBytes());
    try {
        writeAndVerifyReceived(clientMessage.retain(), clientChannel, serverLatch, serverReceiver);
        writeAndVerifyReceived(serverMessage.retain(), serverConnectedChannel, clientLatch, clientReceiver);
        if (expectedApplicationProtocol != null) {
            verifyApplicationLevelProtocol(clientChannel, expectedApplicationProtocol);
            verifyApplicationLevelProtocol(serverConnectedChannel, expectedApplicationProtocol);
        }
    } finally {
        clientMessage.release();
        serverMessage.release();
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 54 with ByteBuf

use of io.netty.buffer.ByteBuf in project netty by netty.

the class HpackUtilBenchmark method newTestEncoder.

static HpackEncoder newTestEncoder() {
    HpackEncoder hpackEncoder = new HpackEncoder();
    ByteBuf buf = Unpooled.buffer();
    try {
        hpackEncoder.setMaxHeaderTableSize(buf, MAX_HEADER_TABLE_SIZE);
        hpackEncoder.setMaxHeaderListSize(MAX_HEADER_LIST_SIZE);
    } catch (Http2Exception e) {
        throw new Error("max size not allowed?", e);
    } finally {
        buf.release();
    }
    return hpackEncoder;
}
Also used : ByteBuf(io.netty.buffer.ByteBuf)

Example 55 with ByteBuf

use of io.netty.buffer.ByteBuf in project netty by netty.

the class ByteBufAllocatorBenchmark method unpooledHeapAllocAndFree.

@Benchmark
public void unpooledHeapAllocAndFree() {
    int idx = rand.nextInt(unpooledHeapBuffers.length);
    ByteBuf oldBuf = unpooledHeapBuffers[idx];
    if (oldBuf != null) {
        oldBuf.release();
    }
    unpooledHeapBuffers[idx] = unpooledAllocator.heapBuffer(size);
}
Also used : ByteBuf(io.netty.buffer.ByteBuf) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)1557 Test (org.junit.Test)668 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)162 IOException (java.io.IOException)99 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)89 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)81 Test (org.testng.annotations.Test)68 InetSocketAddress (java.net.InetSocketAddress)60 Channel (io.netty.channel.Channel)57 ChannelFuture (io.netty.channel.ChannelFuture)56 ArrayList (java.util.ArrayList)55 Map (java.util.Map)45 ChannelPromise (io.netty.channel.ChannelPromise)41 AtomicReference (java.util.concurrent.atomic.AtomicReference)36 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)35 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)34 HashMap (java.util.HashMap)34 CountDownLatch (java.util.concurrent.CountDownLatch)34 RecyclableDuplicateByteBuf (io.netty.buffer.RecyclableDuplicateByteBuf)32 EventLoopGroup (io.netty.channel.EventLoopGroup)32