Search in sources :

Example 1 with DefaultChannelConfig

use of io.netty.channel.DefaultChannelConfig 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 DefaultChannelConfig

use of io.netty.channel.DefaultChannelConfig 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 3 with DefaultChannelConfig

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

the class BootstrapTest method testChannelOptionOrderPreserve.

@Test
public void testChannelOptionOrderPreserve() throws InterruptedException {
    final BlockingQueue<ChannelOption<?>> options = new LinkedBlockingQueue<ChannelOption<?>>();
    class ChannelConfigValidator extends DefaultChannelConfig {

        ChannelConfigValidator(Channel channel) {
            super(channel);
        }

        @Override
        public <T> boolean setOption(ChannelOption<T> option, T value) {
            options.add(option);
            return super.setOption(option, value);
        }
    }
    final CountDownLatch latch = new CountDownLatch(1);
    final Bootstrap bootstrap = new Bootstrap().handler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) {
            latch.countDown();
        }
    }).group(groupA).channelFactory(new ChannelFactory<Channel>() {

        @Override
        public Channel newChannel() {
            return new LocalChannel() {

                private ChannelConfigValidator config;

                @Override
                public synchronized ChannelConfig config() {
                    if (config == null) {
                        config = new ChannelConfigValidator(this);
                    }
                    return config;
                }
            };
        }
    }).option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 1).option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 2);
    bootstrap.register().syncUninterruptibly();
    latch.await();
    // Check the order is the same as what we defined before.
    assertSame(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, options.take());
    assertSame(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, options.take());
}
Also used : ChannelOption(io.netty.channel.ChannelOption) LocalChannel(io.netty.channel.local.LocalChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) ServerChannel(io.netty.channel.ServerChannel) Channel(io.netty.channel.Channel) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultChannelConfig(io.netty.channel.DefaultChannelConfig) ChannelConfig(io.netty.channel.ChannelConfig) DefaultChannelConfig(io.netty.channel.DefaultChannelConfig) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultChannelConfig (io.netty.channel.DefaultChannelConfig)3 ByteBuf (io.netty.buffer.ByteBuf)2 ChannelConfig (io.netty.channel.ChannelConfig)2 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelMetadata (io.netty.channel.ChannelMetadata)2 ChannelPromise (io.netty.channel.ChannelPromise)2 DefaultChannelPromise (io.netty.channel.DefaultChannelPromise)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Channel (io.netty.channel.Channel)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelOption (io.netty.channel.ChannelOption)1 ServerChannel (io.netty.channel.ServerChannel)1 LocalChannel (io.netty.channel.local.LocalChannel)1 LocalServerChannel (io.netty.channel.local.LocalServerChannel)1 FlowControlled (io.netty.handler.codec.http2.Http2RemoteFlowController.FlowControlled)1 GenericFutureListener (io.netty.util.concurrent.GenericFutureListener)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1