Search in sources :

Example 66 with ChannelPromise

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

the class ChunkedWriteHandlerTest method checkFirstFailed.

private static void checkFirstFailed(Object input) {
    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());
    ChannelFuture r = ch.writeAndFlush(input);
    // Should be `false` as we do not expect any messages to be written
    assertFalse(ch.finish());
    assertTrue(r.cause() instanceof RuntimeException);
}
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)

Example 67 with ChannelPromise

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

the class ChunkedWriteHandlerTest method testEndOfInputWhenChannelIsClosedwhenWrite.

@Test
public void testEndOfInputWhenChannelIsClosedwhenWrite() {
    ChunkedInput<ByteBuf> input = new ChunkedInput<ByteBuf>() {

        @Override
        public boolean isEndOfInput() {
            return true;
        }

        @Override
        public void close() {
        }

        @Deprecated
        @Override
        public ByteBuf readChunk(ChannelHandlerContext ctx) {
            return null;
        }

        @Override
        public ByteBuf readChunk(ByteBufAllocator allocator) {
            return null;
        }

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

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

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            ReferenceCountUtil.release(msg);
            // Calling close so we will drop all queued messages in the ChunkedWriteHandler.
            ctx.close();
            promise.setSuccess();
        }
    }, new ChunkedWriteHandler());
    ch.writeAndFlush(input).syncUninterruptibly();
    assertFalse(ch.finishAndReleaseAll());
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 68 with ChannelPromise

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

the class SpdySessionHandler method issueStreamError.

/*
     * SPDY Stream Error Handling:
     *
     * Upon a stream error, the endpoint must send a RST_STREAM frame which contains
     * the Stream-ID for the stream where the error occurred and the error getStatus which
     * caused the error.
     *
     * After sending the RST_STREAM, the stream is closed to the sending endpoint.
     *
     * Note: this is only called by the worker thread
     */
private void issueStreamError(ChannelHandlerContext ctx, int streamId, SpdyStreamStatus status) {
    boolean fireChannelRead = !spdySession.isRemoteSideClosed(streamId);
    ChannelPromise promise = ctx.newPromise();
    removeStream(streamId, promise);
    SpdyRstStreamFrame spdyRstStreamFrame = new DefaultSpdyRstStreamFrame(streamId, status);
    ctx.writeAndFlush(spdyRstStreamFrame, promise);
    if (fireChannelRead) {
        ctx.fireChannelRead(spdyRstStreamFrame);
    }
}
Also used : ChannelPromise(io.netty.channel.ChannelPromise)

Example 69 with ChannelPromise

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

the class JdkZlibEncoder method close.

@Override
public ChannelFuture close(final ChannelPromise promise) {
    ChannelHandlerContext ctx = ctx();
    EventExecutor executor = ctx.executor();
    if (executor.inEventLoop()) {
        return finishEncode(ctx, promise);
    } else {
        final ChannelPromise p = ctx.newPromise();
        executor.execute(new Runnable() {

            @Override
            public void run() {
                ChannelFuture f = finishEncode(ctx(), p);
                PromiseNotifier.cascade(f, promise);
            }
        });
        return p;
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) EventExecutor(io.netty.util.concurrent.EventExecutor) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise)

Example 70 with ChannelPromise

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

the class Http2FrameRoundtripTest method setup.

@BeforeEach
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    when(ctx.alloc()).thenReturn(alloc);
    when(ctx.executor()).thenReturn(executor);
    when(ctx.channel()).thenReturn(channel);
    doAnswer(new Answer<ByteBuf>() {

        @Override
        public ByteBuf answer(InvocationOnMock in) throws Throwable {
            return Unpooled.buffer();
        }
    }).when(alloc).buffer();
    doAnswer(new Answer<ByteBuf>() {

        @Override
        public ByteBuf answer(InvocationOnMock in) throws Throwable {
            return Unpooled.buffer((Integer) in.getArguments()[0]);
        }
    }).when(alloc).buffer(anyInt());
    doAnswer(new Answer<ChannelPromise>() {

        @Override
        public ChannelPromise answer(InvocationOnMock invocation) throws Throwable {
            return new DefaultChannelPromise(channel, GlobalEventExecutor.INSTANCE);
        }
    }).when(ctx).newPromise();
    writer = new DefaultHttp2FrameWriter(new DefaultHttp2HeadersEncoder(NEVER_SENSITIVE, newTestEncoder()));
    reader = new DefaultHttp2FrameReader(new DefaultHttp2HeadersDecoder(false, newTestDecoder()));
}
Also used : ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) EmptyByteBuf(io.netty.buffer.EmptyByteBuf) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) BeforeEach(org.junit.jupiter.api.BeforeEach)

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