Search in sources :

Example 61 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project netty by netty.

the class MessageToMessageEncoderTest method testIntermediateWriteFailures.

@Test
public void testIntermediateWriteFailures() {
    ChannelHandler encoder = new MessageToMessageEncoder<Object>() {

        @Override
        protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) {
            out.add(new Object());
            out.add(msg);
        }
    };
    final Exception firstWriteException = new Exception();
    ChannelHandler writeThrower = new ChannelOutboundHandlerAdapter() {

        private boolean firstWritten;

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
            if (firstWritten) {
                ctx.write(msg, promise);
            } else {
                firstWritten = true;
                promise.setFailure(firstWriteException);
            }
        }
    };
    EmbeddedChannel channel = new EmbeddedChannel(writeThrower, encoder);
    Object msg = new Object();
    ChannelFuture write = channel.writeAndFlush(msg);
    assertSame(firstWriteException, write.cause());
    assertSame(msg, channel.readOutbound());
    assertFalse(channel.finish());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) List(java.util.List) ChannelPromise(io.netty.channel.ChannelPromise) ChannelHandler(io.netty.channel.ChannelHandler) Test(org.junit.jupiter.api.Test)

Example 62 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project netty by netty.

the class AbstractHttp2StreamChannel method write0.

protected ChannelFuture write0(ChannelHandlerContext ctx, Object msg) {
    ChannelPromise promise = ctx.newPromise();
    ctx.write(msg, promise);
    return promise;
}
Also used : ChannelPromise(io.netty.channel.ChannelPromise) VoidChannelPromise(io.netty.channel.VoidChannelPromise)

Example 63 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project netty by netty.

the class SslHandlerTest method testNonApplicationDataFailureFailsQueuedWrites.

@Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testNonApplicationDataFailureFailsQueuedWrites() throws NoSuchAlgorithmException, InterruptedException {
    final CountDownLatch writeLatch = new CountDownLatch(1);
    final Queue<ChannelPromise> writesToFail = new ConcurrentLinkedQueue<ChannelPromise>();
    SSLEngine engine = newClientModeSSLEngine();
    SslHandler handler = new SslHandler(engine) {

        @Override
        public void write(final ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            super.write(ctx, msg, promise);
            writeLatch.countDown();
        }
    };
    EmbeddedChannel ch = new EmbeddedChannel(new ChannelDuplexHandler() {

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
            if (msg instanceof ByteBuf) {
                if (((ByteBuf) msg).isReadable()) {
                    writesToFail.add(promise);
                } else {
                    promise.setSuccess();
                }
            }
            ReferenceCountUtil.release(msg);
        }
    }, handler);
    try {
        final CountDownLatch writeCauseLatch = new CountDownLatch(1);
        final AtomicReference<Throwable> failureRef = new AtomicReference<Throwable>();
        ch.write(Unpooled.wrappedBuffer(new byte[] { 1 })).addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) {
                failureRef.compareAndSet(null, future.cause());
                writeCauseLatch.countDown();
            }
        });
        writeLatch.await();
        // Simulate failing the SslHandler non-application writes after there are applications writes queued.
        ChannelPromise promiseToFail;
        while ((promiseToFail = writesToFail.poll()) != null) {
            promiseToFail.setFailure(new RuntimeException("fake exception"));
        }
        writeCauseLatch.await();
        Throwable writeCause = failureRef.get();
        assertNotNull(writeCause);
        assertThat(writeCause, is(CoreMatchers.<Throwable>instanceOf(SSLException.class)));
        Throwable cause = handler.handshakeFuture().cause();
        assertNotNull(cause);
        assertThat(cause, is(CoreMatchers.<Throwable>instanceOf(SSLException.class)));
    } finally {
        assertFalse(ch.finishAndReleaseAll());
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SSLEngine(javax.net.ssl.SSLEngine) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelPromise(io.netty.channel.ChannelPromise) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 64 with ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project netty by netty.

the class ChunkedWriteHandlerTest method testFailureWhenLastChunkFailed.

// See https://github.com/netty/netty/issues/8700.
@Test
public void testFailureWhenLastChunkFailed() throws IOException {
    ChannelOutboundHandlerAdapter failLast = new ChannelOutboundHandlerAdapter() {

        private int passedWrites;

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
            if (++this.passedWrites < 4) {
                ctx.write(msg, promise);
            } else {
                ReferenceCountUtil.release(msg);
                promise.tryFailure(new RuntimeException());
            }
        }
    };
    EmbeddedChannel ch = new EmbeddedChannel(failLast, new ChunkedWriteHandler());
    // 4 chunks
    ChannelFuture r = ch.writeAndFlush(new ChunkedFile(TMP, 1024 * 16));
    assertTrue(ch.finish());
    assertFalse(r.isSuccess());
    assertTrue(r.cause() instanceof RuntimeException);
    // 3 out of 4 chunks were already written
    int read = 0;
    for (; ; ) {
        ByteBuf buffer = ch.readOutbound();
        if (buffer == null) {
            break;
        }
        read += buffer.readableBytes();
        buffer.release();
    }
    assertEquals(1024 * 16 * 3, read);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) 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 ChannelPromise

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPromise in project netty by netty.

the class ChunkedWriteHandlerTest method checkSkipFailed.

private static void checkSkipFailed(Object input1, Object input2) {
    ChannelOutboundHandlerAdapter failFirst = new ChannelOutboundHandlerAdapter() {

        private boolean alreadyFailed;

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
            if (alreadyFailed) {
                ctx.write(msg, promise);
            } else {
                this.alreadyFailed = true;
                ReferenceCountUtil.release(msg);
                promise.tryFailure(new RuntimeException());
            }
        }
    };
    EmbeddedChannel ch = new EmbeddedChannel(failFirst, new ChunkedWriteHandler());
    ChannelFuture r1 = ch.write(input1);
    ChannelFuture r2 = ch.writeAndFlush(input2).awaitUninterruptibly();
    assertTrue(ch.finish());
    assertTrue(r1.cause() instanceof RuntimeException);
    assertTrue(r2.isSuccess());
    // note, that after we've "skipped" the first write,
    // we expect to see the second message, chunk by chunk
    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, read);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) 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)

Aggregations

ChannelPromise (io.netty.channel.ChannelPromise)223 Test (org.junit.jupiter.api.Test)88 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)63 ChannelFuture (io.netty.channel.ChannelFuture)62 DefaultChannelPromise (io.netty.channel.DefaultChannelPromise)58 ByteBuf (io.netty.buffer.ByteBuf)56 ChannelOutboundHandlerAdapter (io.netty.channel.ChannelOutboundHandlerAdapter)30 Test (org.junit.Test)25 Channel (io.netty.channel.Channel)23 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)22 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)22 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)21 ClosedChannelException (java.nio.channels.ClosedChannelException)20 ChannelFutureListener (io.netty.channel.ChannelFutureListener)19 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)18 InvocationOnMock (org.mockito.invocation.InvocationOnMock)18 AsciiString (io.netty.util.AsciiString)15 IOException (java.io.IOException)14 CountDownLatch (java.util.concurrent.CountDownLatch)13 Bootstrap (io.netty.bootstrap.Bootstrap)12