Search in sources :

Example 61 with ChannelPromise

use of 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 62 with ChannelPromise

use of 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)

Example 63 with ChannelPromise

use of 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 64 with ChannelPromise

use of 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 65 with ChannelPromise

use of 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)

Aggregations

ChannelPromise (io.netty.channel.ChannelPromise)218 Test (org.junit.jupiter.api.Test)87 ChannelFuture (io.netty.channel.ChannelFuture)62 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)59 DefaultChannelPromise (io.netty.channel.DefaultChannelPromise)57 ByteBuf (io.netty.buffer.ByteBuf)54 ChannelOutboundHandlerAdapter (io.netty.channel.ChannelOutboundHandlerAdapter)27 Test (org.junit.Test)24 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)22 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)22 Channel (io.netty.channel.Channel)21 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)13 Bootstrap (io.netty.bootstrap.Bootstrap)12 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)12