Search in sources :

Example 1 with ChannelMetadata

use of io.netty.channel.ChannelMetadata 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 2 with ChannelMetadata

use of io.netty.channel.ChannelMetadata in project riposte by Nike-Inc.

the class SmartHttpContentDecompressorTest method newContentDecoder_works_as_expected.

@DataProvider(value = { "GZIP", "X_GZIP", "DEFLATE", "X_DEFLATE", "CONTENT_ENCODING_THAT_DOES_NOT_REPRESENT_COMPRESSED_PAYLOAD", "ENDPOINT_DOES_NOT_WANT_DECOMPRESS", "NULL_ENDPOINT" })
@Test
public void newContentDecoder_works_as_expected(NewContentDecoderScenario scenario) throws Exception {
    // given
    SmartHttpContentDecompressor decompressor = new SmartHttpContentDecompressor();
    TestUtil.ChannelHandlerContextMocks mocks = TestUtil.mockChannelHandlerContext();
    Whitebox.setInternalState(decompressor, "ctx", mocks.mockContext);
    ChannelMetadata channelMetadata = new ChannelMetadata(false);
    ChannelConfig channelConfigMock = mock(ChannelConfig.class);
    doReturn(scenario.endpoint).when(mocks.mockHttpProcessingState).getEndpointForExecution();
    doReturn(channelMetadata).when(mocks.mockChannel).metadata();
    doReturn(channelConfigMock).when(mocks.mockChannel).config();
    // when
    EmbeddedChannel result = decompressor.newContentDecoder(scenario.contentEncoding);
    // then
    if (scenario.expectValidDecompressor) {
        assertThat(result).isNotNull();
    } else {
        assertThat(result).isNull();
    }
}
Also used : TestUtil(com.nike.riposte.server.testutils.TestUtil) ChannelMetadata(io.netty.channel.ChannelMetadata) ChannelConfig(io.netty.channel.ChannelConfig) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 3 with ChannelMetadata

use of io.netty.channel.ChannelMetadata in project netty by netty.

the class Http2ConnectionHandlerTest method setup.

@SuppressWarnings("unchecked")
@BeforeEach
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    promise = new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE);
    voidPromise = new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE);
    when(channel.metadata()).thenReturn(new ChannelMetadata(false));
    DefaultChannelConfig config = new DefaultChannelConfig(channel);
    when(channel.config()).thenReturn(config);
    Throwable fakeException = new RuntimeException("Fake exception");
    when(encoder.connection()).thenReturn(connection);
    when(decoder.connection()).thenReturn(connection);
    when(encoder.frameWriter()).thenReturn(frameWriter);
    when(encoder.flowController()).thenReturn(remoteFlow);
    when(decoder.flowController()).thenReturn(localFlow);
    doAnswer(new Answer<ChannelFuture>() {

        @Override
        public ChannelFuture answer(InvocationOnMock invocation) throws Throwable {
            ByteBuf buf = invocation.getArgument(3);
            goAwayDebugCap = buf.toString(UTF_8);
            buf.release();
            return future;
        }
    }).when(frameWriter).writeGoAway(any(ChannelHandlerContext.class), anyInt(), anyLong(), any(ByteBuf.class), any(ChannelPromise.class));
    doAnswer(new Answer<ChannelFuture>() {

        @Override
        public ChannelFuture answer(InvocationOnMock invocation) throws Throwable {
            Object o = invocation.getArguments()[0];
            if (o instanceof ChannelFutureListener) {
                ((ChannelFutureListener) o).operationComplete(future);
            }
            return future;
        }
    }).when(future).addListener(any(GenericFutureListener.class));
    when(future.cause()).thenReturn(fakeException);
    when(future.channel()).thenReturn(channel);
    when(channel.isActive()).thenReturn(true);
    when(channel.pipeline()).thenReturn(pipeline);
    when(connection.remote()).thenReturn(remote);
    when(remote.flowController()).thenReturn(remoteFlowController);
    when(connection.local()).thenReturn(local);
    when(local.flowController()).thenReturn(localFlowController);
    doAnswer(new Answer<Http2Stream>() {

        @Override
        public Http2Stream answer(InvocationOnMock in) throws Throwable {
            Http2StreamVisitor visitor = in.getArgument(0);
            if (!visitor.visit(stream)) {
                return stream;
            }
            return null;
        }
    }).when(connection).forEachActiveStream(any(Http2StreamVisitor.class));
    when(connection.stream(NON_EXISTANT_STREAM_ID)).thenReturn(null);
    when(connection.numActiveStreams()).thenReturn(1);
    when(connection.stream(STREAM_ID)).thenReturn(stream);
    when(connection.goAwaySent(anyInt(), anyLong(), any(ByteBuf.class))).thenReturn(true);
    when(stream.open(anyBoolean())).thenReturn(stream);
    when(encoder.writeSettings(eq(ctx), any(Http2Settings.class), eq(promise))).thenReturn(future);
    when(ctx.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT);
    when(ctx.channel()).thenReturn(channel);
    when(ctx.newSucceededFuture()).thenReturn(future);
    when(ctx.newPromise()).thenReturn(promise);
    when(ctx.voidPromise()).thenReturn(voidPromise);
    when(ctx.write(any())).thenReturn(future);
    when(ctx.executor()).thenReturn(executor);
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock in) throws Throwable {
            Object msg = in.getArgument(0);
            ReferenceCountUtil.release(msg);
            return null;
        }
    }).when(ctx).fireChannelRead(any());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ChannelMetadata(io.netty.channel.ChannelMetadata) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultChannelConfig(io.netty.channel.DefaultChannelConfig) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with ChannelMetadata

use of io.netty.channel.ChannelMetadata in project netty by netty.

the class Http2ControlFrameLimitEncoderTest method setup.

/**
 * Init fields and do mocking.
 */
@BeforeEach
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    numWrites = 0;
    Http2FrameWriter.Configuration configuration = mock(Http2FrameWriter.Configuration.class);
    Http2FrameSizePolicy frameSizePolicy = mock(Http2FrameSizePolicy.class);
    when(writer.configuration()).thenReturn(configuration);
    when(configuration.frameSizePolicy()).thenReturn(frameSizePolicy);
    when(frameSizePolicy.maxFrameSize()).thenReturn(DEFAULT_MAX_FRAME_SIZE);
    when(writer.writeRstStream(eq(ctx), anyInt(), anyLong(), any(ChannelPromise.class))).thenAnswer(new Answer<ChannelFuture>() {

        @Override
        public ChannelFuture answer(InvocationOnMock invocationOnMock) {
            return handlePromise(invocationOnMock, 3);
        }
    });
    when(writer.writeSettingsAck(any(ChannelHandlerContext.class), any(ChannelPromise.class))).thenAnswer(new Answer<ChannelFuture>() {

        @Override
        public ChannelFuture answer(InvocationOnMock invocationOnMock) {
            return handlePromise(invocationOnMock, 1);
        }
    });
    when(writer.writePing(any(ChannelHandlerContext.class), anyBoolean(), anyLong(), any(ChannelPromise.class))).thenAnswer(new Answer<ChannelFuture>() {

        @Override
        public ChannelFuture answer(InvocationOnMock invocationOnMock) {
            ChannelPromise promise = handlePromise(invocationOnMock, 3);
            if (invocationOnMock.getArgument(1) == Boolean.FALSE) {
                promise.trySuccess();
            }
            return promise;
        }
    });
    when(writer.writeGoAway(any(ChannelHandlerContext.class), anyInt(), anyLong(), any(ByteBuf.class), any(ChannelPromise.class))).thenAnswer(new Answer<ChannelFuture>() {

        @Override
        public ChannelFuture answer(InvocationOnMock invocationOnMock) {
            ReferenceCountUtil.release(invocationOnMock.getArgument(3));
            ChannelPromise promise = invocationOnMock.getArgument(4);
            goAwayPromises.offer(promise);
            return promise;
        }
    });
    Http2Connection connection = new DefaultHttp2Connection(false);
    connection.remote().flowController(new DefaultHttp2RemoteFlowController(connection));
    connection.local().flowController(new DefaultHttp2LocalFlowController(connection).frameWriter(writer));
    DefaultHttp2ConnectionEncoder defaultEncoder = new DefaultHttp2ConnectionEncoder(connection, writer);
    encoder = new Http2ControlFrameLimitEncoder(defaultEncoder, 2);
    DefaultHttp2ConnectionDecoder decoder = new DefaultHttp2ConnectionDecoder(connection, encoder, mock(Http2FrameReader.class));
    Http2ConnectionHandler handler = new Http2ConnectionHandlerBuilder().frameListener(mock(Http2FrameListener.class)).codec(decoder, encoder).build();
    // Set LifeCycleManager on encoder and decoder
    when(ctx.channel()).thenReturn(channel);
    when(ctx.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT);
    when(channel.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT);
    when(executor.inEventLoop()).thenReturn(true);
    doAnswer(new Answer<ChannelPromise>() {

        @Override
        public ChannelPromise answer(InvocationOnMock invocation) throws Throwable {
            return newPromise();
        }
    }).when(ctx).newPromise();
    when(ctx.executor()).thenReturn(executor);
    when(channel.isActive()).thenReturn(false);
    when(channel.config()).thenReturn(config);
    when(channel.isWritable()).thenReturn(true);
    when(channel.bytesBeforeUnwritable()).thenReturn(Long.MAX_VALUE);
    when(config.getWriteBufferHighWaterMark()).thenReturn(Integer.MAX_VALUE);
    when(config.getMessageSizeEstimator()).thenReturn(DefaultMessageSizeEstimator.DEFAULT);
    ChannelMetadata metadata = new ChannelMetadata(false, 16);
    when(channel.metadata()).thenReturn(metadata);
    when(channel.unsafe()).thenReturn(unsafe);
    handler.handlerAdded(ctx);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) ChannelMetadata(io.netty.channel.ChannelMetadata) InvocationOnMock(org.mockito.invocation.InvocationOnMock) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with ChannelMetadata

use of io.netty.channel.ChannelMetadata in project netty by netty.

the class StreamBufferingEncoderTest method setup.

/**
 * Init fields and do mocking.
 */
@BeforeEach
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    Http2FrameWriter.Configuration configuration = mock(Http2FrameWriter.Configuration.class);
    Http2FrameSizePolicy frameSizePolicy = mock(Http2FrameSizePolicy.class);
    when(writer.configuration()).thenReturn(configuration);
    when(configuration.frameSizePolicy()).thenReturn(frameSizePolicy);
    when(frameSizePolicy.maxFrameSize()).thenReturn(DEFAULT_MAX_FRAME_SIZE);
    when(writer.writeData(any(ChannelHandlerContext.class), anyInt(), any(ByteBuf.class), anyInt(), anyBoolean(), any(ChannelPromise.class))).thenAnswer(successAnswer());
    when(writer.writeRstStream(eq(ctx), anyInt(), anyLong(), any(ChannelPromise.class))).thenAnswer(successAnswer());
    when(writer.writeGoAway(any(ChannelHandlerContext.class), anyInt(), anyLong(), any(ByteBuf.class), any(ChannelPromise.class))).thenAnswer(successAnswer());
    when(writer.writeHeaders(any(ChannelHandlerContext.class), anyInt(), any(Http2Headers.class), anyInt(), anyBoolean(), any(ChannelPromise.class))).thenAnswer(noopAnswer());
    when(writer.writeHeaders(any(ChannelHandlerContext.class), anyInt(), any(Http2Headers.class), anyInt(), anyShort(), anyBoolean(), anyInt(), anyBoolean(), any(ChannelPromise.class))).thenAnswer(noopAnswer());
    connection = new DefaultHttp2Connection(false);
    connection.remote().flowController(new DefaultHttp2RemoteFlowController(connection));
    connection.local().flowController(new DefaultHttp2LocalFlowController(connection).frameWriter(writer));
    DefaultHttp2ConnectionEncoder defaultEncoder = new DefaultHttp2ConnectionEncoder(connection, writer);
    encoder = new StreamBufferingEncoder(defaultEncoder);
    DefaultHttp2ConnectionDecoder decoder = new DefaultHttp2ConnectionDecoder(connection, encoder, mock(Http2FrameReader.class));
    Http2ConnectionHandler handler = new Http2ConnectionHandlerBuilder().frameListener(mock(Http2FrameListener.class)).codec(decoder, encoder).build();
    // Set LifeCycleManager on encoder and decoder
    when(ctx.channel()).thenReturn(channel);
    when(ctx.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT);
    when(channel.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT);
    when(executor.inEventLoop()).thenReturn(true);
    doAnswer(new Answer<ChannelPromise>() {

        @Override
        public ChannelPromise answer(InvocationOnMock invocation) throws Throwable {
            return newPromise();
        }
    }).when(ctx).newPromise();
    when(ctx.executor()).thenReturn(executor);
    when(channel.isActive()).thenReturn(false);
    when(channel.config()).thenReturn(config);
    when(channel.isWritable()).thenReturn(true);
    when(channel.bytesBeforeUnwritable()).thenReturn(Long.MAX_VALUE);
    when(config.getWriteBufferHighWaterMark()).thenReturn(Integer.MAX_VALUE);
    when(config.getMessageSizeEstimator()).thenReturn(DefaultMessageSizeEstimator.DEFAULT);
    ChannelMetadata metadata = new ChannelMetadata(false, 16);
    when(channel.metadata()).thenReturn(metadata);
    when(channel.unsafe()).thenReturn(unsafe);
    handler.handlerAdded(ctx);
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) ChannelMetadata(io.netty.channel.ChannelMetadata) InvocationOnMock(org.mockito.invocation.InvocationOnMock) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

ChannelMetadata (io.netty.channel.ChannelMetadata)5 ByteBuf (io.netty.buffer.ByteBuf)4 ChannelPromise (io.netty.channel.ChannelPromise)4 DefaultChannelPromise (io.netty.channel.DefaultChannelPromise)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 ChannelFuture (io.netty.channel.ChannelFuture)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 ChannelConfig (io.netty.channel.ChannelConfig)2 DefaultChannelConfig (io.netty.channel.DefaultChannelConfig)2 TestUtil (com.nike.riposte.server.testutils.TestUtil)1 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1 FlowControlled (io.netty.handler.codec.http2.Http2RemoteFlowController.FlowControlled)1 GenericFutureListener (io.netty.util.concurrent.GenericFutureListener)1 AssertionFailedError (junit.framework.AssertionFailedError)1 Test (org.junit.Test)1 Mockito.anyBoolean (org.mockito.Mockito.anyBoolean)1