Search in sources :

Example 61 with ByteBufAllocator

use of io.netty.buffer.ByteBufAllocator in project neo4j by neo4j.

the class ChunkedOutputTest method shouldThrowErrorWithRemoteAddressWhenClosed.

@Test
void shouldThrowErrorWithRemoteAddressWhenClosed() throws Exception {
    Channel channel = mock(Channel.class);
    ByteBufAllocator allocator = mock(ByteBufAllocator.class);
    when(allocator.buffer(anyInt())).thenReturn(Unpooled.buffer());
    when(channel.alloc()).thenReturn(allocator);
    SocketAddress remoteAddress = mock(SocketAddress.class);
    String remoteAddressString = "client.server.com:7687";
    when(remoteAddress.toString()).thenReturn(remoteAddressString);
    when(channel.remoteAddress()).thenReturn(remoteAddress);
    ChunkedOutput output = new ChunkedOutput(channel, DEFAULT_TEST_BUFFER_SIZE, DEFAULT_TEST_BUFFER_SIZE, NO_THROTTLE);
    output.close();
    var e = assertThrows(PackOutputClosedException.class, () -> output.writeInt(42));
    assertThat(e.getMessage()).contains(remoteAddressString);
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) SocketAddress(java.net.SocketAddress) Test(org.junit.jupiter.api.Test)

Example 62 with ByteBufAllocator

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

the class ChunkedWriteHandlerTest method testListenerNotifiedWhenIsEnd.

// Test case which shows that there is not a bug like stated here:
// https://stackoverflow.com/a/10426305
@Test
public void testListenerNotifiedWhenIsEnd() {
    ByteBuf buffer = Unpooled.copiedBuffer("Test", CharsetUtil.ISO_8859_1);
    ChunkedInput<ByteBuf> input = new ChunkedInput<ByteBuf>() {

        private boolean done;

        private final ByteBuf buffer = Unpooled.copiedBuffer("Test", CharsetUtil.ISO_8859_1);

        @Override
        public boolean isEndOfInput() throws Exception {
            return done;
        }

        @Override
        public void close() throws Exception {
            buffer.release();
        }

        @Deprecated
        @Override
        public ByteBuf readChunk(ChannelHandlerContext ctx) throws Exception {
            return readChunk(ctx.alloc());
        }

        @Override
        public ByteBuf readChunk(ByteBufAllocator allocator) throws Exception {
            if (done) {
                return null;
            }
            done = true;
            return buffer.retainedDuplicate();
        }

        @Override
        public long length() {
            return -1;
        }

        @Override
        public long progress() {
            return 1;
        }
    };
    final AtomicBoolean listenerNotified = new AtomicBoolean(false);
    final ChannelFutureListener listener = new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            listenerNotified.set(true);
        }
    };
    EmbeddedChannel ch = new EmbeddedChannel(new ChunkedWriteHandler());
    ch.writeAndFlush(input).addListener(listener).syncUninterruptibly();
    assertTrue(ch.finish());
    // the listener should have been notified
    assertTrue(listenerNotified.get());
    ByteBuf buffer2 = ch.readOutbound();
    assertEquals(buffer, buffer2);
    assertNull(ch.readOutbound());
    buffer.release();
    buffer2.release();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Test(org.junit.jupiter.api.Test)

Example 63 with ByteBufAllocator

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

the class ChunkedWriteHandlerTest method testChunkedMessageInput.

@Test
public void testChunkedMessageInput() {
    ChunkedInput<Object> input = new ChunkedInput<Object>() {

        private boolean done;

        @Override
        public boolean isEndOfInput() throws Exception {
            return done;
        }

        @Override
        public void close() throws Exception {
        // NOOP
        }

        @Deprecated
        @Override
        public Object readChunk(ChannelHandlerContext ctx) throws Exception {
            return readChunk(ctx.alloc());
        }

        @Override
        public Object readChunk(ByteBufAllocator ctx) throws Exception {
            if (done) {
                return false;
            }
            done = true;
            return 0;
        }

        @Override
        public long length() {
            return -1;
        }

        @Override
        public long progress() {
            return 1;
        }
    };
    EmbeddedChannel ch = new EmbeddedChannel(new ChunkedWriteHandler());
    ch.writeAndFlush(input).syncUninterruptibly();
    assertTrue(ch.finish());
    assertEquals(0, (Integer) ch.readOutbound());
    assertNull(ch.readOutbound());
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Test(org.junit.jupiter.api.Test)

Example 64 with ByteBufAllocator

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

the class ChunkedWriteHandlerTest method testStopConsumingChunksWhenFailed.

// See https://github.com/netty/netty/issues/8700.
@Test
public void testStopConsumingChunksWhenFailed() {
    final ByteBuf buffer = Unpooled.copiedBuffer("Test", CharsetUtil.ISO_8859_1);
    final AtomicInteger chunks = new AtomicInteger(0);
    ChunkedInput<ByteBuf> nonClosableInput = new ChunkedInput<ByteBuf>() {

        @Override
        public boolean isEndOfInput() throws Exception {
            return chunks.get() >= 5;
        }

        @Override
        public void close() throws Exception {
        // no-op
        }

        @Deprecated
        @Override
        public ByteBuf readChunk(ChannelHandlerContext ctx) throws Exception {
            return readChunk(ctx.alloc());
        }

        @Override
        public ByteBuf readChunk(ByteBufAllocator allocator) throws Exception {
            chunks.incrementAndGet();
            return buffer.retainedDuplicate();
        }

        @Override
        public long length() {
            return -1;
        }

        @Override
        public long progress() {
            return 1;
        }
    };
    ChannelOutboundHandlerAdapter noOpWrites = new ChannelOutboundHandlerAdapter() {

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
            ReferenceCountUtil.release(msg);
            promise.tryFailure(new RuntimeException());
        }
    };
    EmbeddedChannel ch = new EmbeddedChannel(noOpWrites, new ChunkedWriteHandler());
    ch.writeAndFlush(nonClosableInput).awaitUninterruptibly();
    // Should be `false` as we do not expect any messages to be written
    assertFalse(ch.finish());
    buffer.release();
    // We should expect only single chunked being read from the input.
    // It's possible to get a race condition here between resolving a promise and
    // allocating a new chunk, but should be fine when working with embedded channels.
    assertEquals(1, chunks.get());
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 65 with ByteBufAllocator

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

the class ChunkedWriteHandlerTest method testDiscardPendingWritesOnInactive.

@Test
public void testDiscardPendingWritesOnInactive() throws IOException {
    final AtomicBoolean closeWasCalled = new AtomicBoolean(false);
    ChunkedInput<ByteBuf> notifiableInput = new ChunkedInput<ByteBuf>() {

        private boolean done;

        private final ByteBuf buffer = Unpooled.copiedBuffer("Test", CharsetUtil.ISO_8859_1);

        @Override
        public boolean isEndOfInput() throws Exception {
            return done;
        }

        @Override
        public void close() throws Exception {
            buffer.release();
            closeWasCalled.set(true);
        }

        @Deprecated
        @Override
        public ByteBuf readChunk(ChannelHandlerContext ctx) throws Exception {
            return readChunk(ctx.alloc());
        }

        @Override
        public ByteBuf readChunk(ByteBufAllocator allocator) throws Exception {
            if (done) {
                return null;
            }
            done = true;
            return buffer.retainedDuplicate();
        }

        @Override
        public long length() {
            return -1;
        }

        @Override
        public long progress() {
            return 1;
        }
    };
    EmbeddedChannel ch = new EmbeddedChannel(new ChunkedWriteHandler());
    // Write 3 messages and close channel before flushing
    ChannelFuture r1 = ch.write(new ChunkedFile(TMP));
    ChannelFuture r2 = ch.write(new ChunkedNioFile(TMP));
    ch.write(notifiableInput);
    // Should be `false` as we do not expect any messages to be written
    assertFalse(ch.finish());
    assertFalse(r1.isSuccess());
    assertFalse(r2.isSuccess());
    assertTrue(closeWasCalled.get());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Aggregations

ByteBufAllocator (io.netty.buffer.ByteBufAllocator)79 ByteBuf (io.netty.buffer.ByteBuf)48 ArrayList (java.util.ArrayList)17 Test (org.junit.Test)17 Test (org.junit.jupiter.api.Test)11 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)10 Channel (io.netty.channel.Channel)9 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)8 IOException (java.io.IOException)8 UnpooledByteBufAllocator (io.netty.buffer.UnpooledByteBufAllocator)7 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)7 ChannelFuture (io.netty.channel.ChannelFuture)6 Mono (reactor.core.publisher.Mono)6 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)4 InetSocketAddress (java.net.InetSocketAddress)4 StandardCharsets (java.nio.charset.StandardCharsets)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 NettyDataBufferFactory (org.springframework.core.io.buffer.NettyDataBufferFactory)4 RecvByteBufAllocator (io.netty.channel.RecvByteBufAllocator)3 ByteBuffer (java.nio.ByteBuffer)3