Search in sources :

Example 51 with ChannelPromise

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

the class DefaultHttp2ConnectionEncoderTest method setup.

@BeforeEach
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    ChannelMetadata metadata = new ChannelMetadata(false, 16);
    when(channel.isActive()).thenReturn(true);
    when(channel.pipeline()).thenReturn(pipeline);
    when(channel.metadata()).thenReturn(metadata);
    when(channel.unsafe()).thenReturn(unsafe);
    ChannelConfig config = new DefaultChannelConfig(channel);
    when(channel.config()).thenReturn(config);
    doAnswer(new Answer<ChannelFuture>() {

        @Override
        public ChannelFuture answer(InvocationOnMock in) {
            return newPromise().setFailure((Throwable) in.getArgument(0));
        }
    }).when(channel).newFailedFuture(any(Throwable.class));
    when(writer.configuration()).thenReturn(writerConfig);
    when(writerConfig.frameSizePolicy()).thenReturn(frameSizePolicy);
    when(frameSizePolicy.maxFrameSize()).thenReturn(64);
    doAnswer(new Answer<ChannelFuture>() {

        @Override
        public ChannelFuture answer(InvocationOnMock in) throws Throwable {
            return ((ChannelPromise) in.getArguments()[2]).setSuccess();
        }
    }).when(writer).writeSettings(eq(ctx), any(Http2Settings.class), any(ChannelPromise.class));
    doAnswer(new Answer<ChannelFuture>() {

        @Override
        public ChannelFuture answer(InvocationOnMock in) throws Throwable {
            ((ByteBuf) in.getArguments()[3]).release();
            return ((ChannelPromise) in.getArguments()[4]).setSuccess();
        }
    }).when(writer).writeGoAway(eq(ctx), anyInt(), anyInt(), any(ByteBuf.class), any(ChannelPromise.class));
    writtenData = new ArrayList<String>();
    writtenPadding = new ArrayList<Integer>();
    when(writer.writeData(eq(ctx), anyInt(), any(ByteBuf.class), anyInt(), anyBoolean(), any(ChannelPromise.class))).then(new Answer<ChannelFuture>() {

        @Override
        public ChannelFuture answer(InvocationOnMock in) throws Throwable {
            // Make sure we only receive stream closure on the last frame and that void promises
            // are used for all writes except the last one.
            ChannelPromise promise = (ChannelPromise) in.getArguments()[5];
            if (streamClosed) {
                fail("Stream already closed");
            } else {
                streamClosed = (Boolean) in.getArguments()[4];
            }
            writtenPadding.add((Integer) in.getArguments()[3]);
            ByteBuf data = (ByteBuf) in.getArguments()[2];
            writtenData.add(data.toString(UTF_8));
            // Release the buffer just as DefaultHttp2FrameWriter does
            data.release();
            // Let the promise succeed to trigger listeners.
            return promise.setSuccess();
        }
    });
    when(writer.writeHeaders(eq(ctx), anyInt(), any(Http2Headers.class), anyInt(), anyShort(), anyBoolean(), anyInt(), anyBoolean(), any(ChannelPromise.class))).then(new Answer<ChannelFuture>() {

        @Override
        public ChannelFuture answer(InvocationOnMock invocationOnMock) {
            ChannelPromise promise = invocationOnMock.getArgument(8);
            if (streamClosed) {
                fail("Stream already closed");
            } else {
                streamClosed = invocationOnMock.getArgument(5);
            }
            return promise.setSuccess();
        }
    });
    when(writer.writeHeaders(eq(ctx), anyInt(), any(Http2Headers.class), anyInt(), anyBoolean(), any(ChannelPromise.class))).then(new Answer<ChannelFuture>() {

        @Override
        public ChannelFuture answer(InvocationOnMock invocationOnMock) {
            ChannelPromise promise = invocationOnMock.getArgument(5);
            if (streamClosed) {
                fail("Stream already closed");
            } else {
                streamClosed = invocationOnMock.getArgument(4);
            }
            return promise.setSuccess();
        }
    });
    payloadCaptor = ArgumentCaptor.forClass(Http2RemoteFlowController.FlowControlled.class);
    doNothing().when(remoteFlow).addFlowControlled(any(Http2Stream.class), payloadCaptor.capture());
    when(ctx.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT);
    when(ctx.channel()).thenReturn(channel);
    doAnswer(new Answer<ChannelPromise>() {

        @Override
        public ChannelPromise answer(InvocationOnMock in) throws Throwable {
            return newPromise();
        }
    }).when(ctx).newPromise();
    doAnswer(new Answer<ChannelFuture>() {

        @Override
        public ChannelFuture answer(InvocationOnMock in) throws Throwable {
            return newSucceededFuture();
        }
    }).when(ctx).newSucceededFuture();
    when(ctx.flush()).thenThrow(new AssertionFailedError("forbidden"));
    when(channel.alloc()).thenReturn(PooledByteBufAllocator.DEFAULT);
    // Use a server-side connection so we can test server push.
    connection = new DefaultHttp2Connection(true);
    connection.remote().flowController(remoteFlow);
    encoder = new DefaultHttp2ConnectionEncoder(connection, writer);
    encoder.lifecycleManager(lifecycleManager);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) FlowControlled(io.netty.handler.codec.http2.Http2RemoteFlowController.FlowControlled) ChannelMetadata(io.netty.channel.ChannelMetadata) DefaultChannelConfig(io.netty.channel.DefaultChannelConfig) ChannelConfig(io.netty.channel.ChannelConfig) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultChannelConfig(io.netty.channel.DefaultChannelConfig) Mockito.anyBoolean(org.mockito.Mockito.anyBoolean) AssertionFailedError(junit.framework.AssertionFailedError) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 52 with ChannelPromise

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

the class DefaultHttp2ConnectionEncoderTest method headersWriteShouldOpenStreamForPush.

@Test
public void headersWriteShouldOpenStreamForPush() throws Exception {
    writeAllFlowControlledFrames();
    Http2Stream parent = createStream(STREAM_ID, false);
    reservePushStream(PUSH_STREAM_ID, parent);
    ChannelPromise promise = newPromise();
    encoder.writeHeaders(ctx, PUSH_STREAM_ID, EmptyHttp2Headers.INSTANCE, 0, false, promise);
    assertEquals(HALF_CLOSED_REMOTE, stream(PUSH_STREAM_ID).state());
    verify(writer).writeHeaders(eq(ctx), eq(PUSH_STREAM_ID), eq(EmptyHttp2Headers.INSTANCE), eq(0), eq(false), eq(promise));
}
Also used : ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) Test(org.junit.jupiter.api.Test)

Example 53 with ChannelPromise

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

the class DefaultHttp2ConnectionEncoderTest method canWriteHeaderFrameAfterGoAwaySent.

@Test
public void canWriteHeaderFrameAfterGoAwaySent() throws Exception {
    writeAllFlowControlledFrames();
    createStream(STREAM_ID, false);
    goAwaySent(0);
    ChannelPromise promise = newPromise();
    encoder.writeHeaders(ctx, STREAM_ID, EmptyHttp2Headers.INSTANCE, 0, false, promise);
    verify(writer).writeHeaders(eq(ctx), eq(STREAM_ID), eq(EmptyHttp2Headers.INSTANCE), eq(0), eq(false), eq(promise));
}
Also used : ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) Test(org.junit.jupiter.api.Test)

Example 54 with ChannelPromise

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

the class DefaultHttp2ConnectionEncoderTest method headersWriteShouldHalfCloseAfterOnErrorForPreCreatedStream.

@Test
public void headersWriteShouldHalfCloseAfterOnErrorForPreCreatedStream() throws Exception {
    final ChannelPromise promise = newPromise();
    final Throwable ex = new RuntimeException();
    // Fake an encoding error, like HPACK's HeaderListSizeException
    when(writer.writeHeaders(eq(ctx), eq(STREAM_ID), eq(EmptyHttp2Headers.INSTANCE), eq(0), eq(true), eq(promise))).thenAnswer(new Answer<ChannelFuture>() {

        @Override
        public ChannelFuture answer(InvocationOnMock invocation) {
            promise.setFailure(ex);
            return promise;
        }
    });
    writeAllFlowControlledFrames();
    Http2Stream stream = createStream(STREAM_ID, false);
    encoder.writeHeaders(ctx, STREAM_ID, EmptyHttp2Headers.INSTANCE, 0, true, promise);
    assertTrue(promise.isDone());
    assertFalse(promise.isSuccess());
    assertFalse(stream.isHeadersSent());
    InOrder inOrder = inOrder(lifecycleManager);
    inOrder.verify(lifecycleManager).onError(eq(ctx), eq(true), eq(ex));
    inOrder.verify(lifecycleManager).closeStreamLocal(eq(stream(STREAM_ID)), eq(promise));
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) InOrder(org.mockito.InOrder) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) Test(org.junit.jupiter.api.Test)

Example 55 with ChannelPromise

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

the class DefaultHttp2ConnectionEncoderTest method headersWriteShouldHalfCloseAfterOnErrorForImplicitlyCreatedStream.

@Test
public void headersWriteShouldHalfCloseAfterOnErrorForImplicitlyCreatedStream() throws Exception {
    final ChannelPromise promise = newPromise();
    final Throwable ex = new RuntimeException();
    // Fake an encoding error, like HPACK's HeaderListSizeException
    when(writer.writeHeaders(eq(ctx), eq(STREAM_ID), eq(EmptyHttp2Headers.INSTANCE), eq(0), eq(true), eq(promise))).thenAnswer(new Answer<ChannelFuture>() {

        @Override
        public ChannelFuture answer(InvocationOnMock invocation) {
            promise.setFailure(ex);
            return promise;
        }
    });
    writeAllFlowControlledFrames();
    encoder.writeHeaders(ctx, STREAM_ID, EmptyHttp2Headers.INSTANCE, 0, true, promise);
    assertTrue(promise.isDone());
    assertFalse(promise.isSuccess());
    assertFalse(stream(STREAM_ID).isHeadersSent());
    InOrder inOrder = inOrder(lifecycleManager);
    inOrder.verify(lifecycleManager).onError(eq(ctx), eq(true), eq(ex));
    inOrder.verify(lifecycleManager).closeStreamLocal(eq(stream(STREAM_ID)), eq(promise));
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) InOrder(org.mockito.InOrder) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) Test(org.junit.jupiter.api.Test)

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