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);
}
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();
}
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());
}
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());
}
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());
}
Aggregations