Search in sources :

Example 86 with ChannelPromise

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

the class Http2MultiplexTest method failedOutboundStreamCreationThrowsAndClosesChannel.

// Test failing the promise of the first headers frame of an outbound stream. In practice this error case would most
// likely happen due to the max concurrent streams limit being hit or the channel running out of stream identifiers.
// 
@Test
public void failedOutboundStreamCreationThrowsAndClosesChannel() throws Exception {
    LastInboundHandler handler = new LastInboundHandler();
    Http2StreamChannel childChannel = newOutboundStream(handler);
    assertTrue(childChannel.isActive());
    Http2Headers headers = new DefaultHttp2Headers();
    when(frameWriter.writeHeaders(eqCodecCtx(), anyInt(), eq(headers), anyInt(), anyBoolean(), any(ChannelPromise.class))).thenAnswer(new Answer<ChannelFuture>() {

        @Override
        public ChannelFuture answer(InvocationOnMock invocationOnMock) {
            return ((ChannelPromise) invocationOnMock.getArgument(5)).setFailure(new Http2NoMoreStreamIdsException());
        }
    });
    final ChannelFuture future = childChannel.writeAndFlush(new DefaultHttp2HeadersFrame(headers));
    parentChannel.flush();
    assertFalse(childChannel.isActive());
    assertFalse(childChannel.isOpen());
    handler.checkException();
    assertThrows(Http2NoMoreStreamIdsException.class, new Executable() {

        @Override
        public void execute() {
            future.syncUninterruptibly();
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ChannelPromise(io.netty.channel.ChannelPromise) Http2TestUtil.anyChannelPromise(io.netty.handler.codec.http2.Http2TestUtil.anyChannelPromise) Executable(org.junit.jupiter.api.function.Executable) Test(org.junit.jupiter.api.Test)

Example 87 with ChannelPromise

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

the class Http2MultiplexTest method callUnsafeCloseMultipleTimes.

@Test
public void callUnsafeCloseMultipleTimes() {
    LastInboundHandler inboundHandler = new LastInboundHandler();
    Http2StreamChannel childChannel = newInboundStream(3, false, inboundHandler);
    childChannel.unsafe().close(childChannel.voidPromise());
    ChannelPromise promise = childChannel.newPromise();
    childChannel.unsafe().close(promise);
    promise.syncUninterruptibly();
    childChannel.closeFuture().syncUninterruptibly();
}
Also used : ChannelPromise(io.netty.channel.ChannelPromise) Http2TestUtil.anyChannelPromise(io.netty.handler.codec.http2.Http2TestUtil.anyChannelPromise) Test(org.junit.jupiter.api.Test)

Example 88 with ChannelPromise

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

the class StreamBufferingEncoderTest method rstStreamClosesBufferedStream.

@Test
public void rstStreamClosesBufferedStream() {
    encoder.writeSettingsAck(ctx, newPromise());
    setMaxConcurrentStreams(0);
    encoderWriteHeaders(3, newPromise());
    assertEquals(1, encoder.numBufferedStreams());
    ChannelPromise rstStreamPromise = newPromise();
    encoder.writeRstStream(ctx, 3, CANCEL.code(), rstStreamPromise);
    assertTrue(rstStreamPromise.isSuccess());
    assertEquals(0, encoder.numBufferedStreams());
}
Also used : ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) Test(org.junit.jupiter.api.Test)

Example 89 with ChannelPromise

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

the class StreamBufferingEncoderTest method closedBufferedStreamReleasesByteBuf.

@Test
public void closedBufferedStreamReleasesByteBuf() {
    encoder.writeSettingsAck(ctx, newPromise());
    setMaxConcurrentStreams(0);
    ByteBuf data = mock(ByteBuf.class);
    ChannelFuture f1 = encoderWriteHeaders(3, newPromise());
    assertEquals(1, encoder.numBufferedStreams());
    ChannelFuture f2 = encoder.writeData(ctx, 3, data, 0, false, newPromise());
    ChannelPromise rstPromise = mock(ChannelPromise.class);
    encoder.writeRstStream(ctx, 3, CANCEL.code(), rstPromise);
    assertEquals(0, encoder.numBufferedStreams());
    verify(rstPromise).setSuccess();
    assertTrue(f1.isSuccess());
    assertTrue(f2.isSuccess());
    verify(data).release();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 90 with ChannelPromise

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

the class Http2ConnectionHandler method onConnectionError.

/**
 * Handler for a connection error. Sends a GO_AWAY frame to the remote endpoint. Once all
 * streams are closed, the connection is shut down.
 *
 * @param ctx the channel context
 * @param outbound {@code true} if the error was caused by an outbound operation.
 * @param cause the exception that was caught
 * @param http2Ex the {@link Http2Exception} that is embedded in the causality chain. This may
 *            be {@code null} if it's an unknown exception.
 */
protected void onConnectionError(ChannelHandlerContext ctx, boolean outbound, Throwable cause, Http2Exception http2Ex) {
    if (http2Ex == null) {
        http2Ex = new Http2Exception(INTERNAL_ERROR, cause.getMessage(), cause);
    }
    ChannelPromise promise = ctx.newPromise();
    ChannelFuture future = goAway(ctx, http2Ex, ctx.newPromise());
    if (http2Ex.shutdownHint() == Http2Exception.ShutdownHint.GRACEFUL_SHUTDOWN) {
        doGracefulShutdown(ctx, future, promise);
    } else {
        future.addListener(newClosingChannelFutureListener(ctx, promise));
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Http2CodecUtil.getEmbeddedHttp2Exception(io.netty.handler.codec.http2.Http2CodecUtil.getEmbeddedHttp2Exception) ChannelPromise(io.netty.channel.ChannelPromise)

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