Search in sources :

Example 16 with DefaultChannelPromise

use of io.netty.channel.DefaultChannelPromise 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 17 with DefaultChannelPromise

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

the class Http2ConnectionHandlerTest method writeMultipleRstFramesForSameStream.

@Test
public void writeMultipleRstFramesForSameStream() throws Exception {
    handler = newHandler();
    when(stream.id()).thenReturn(STREAM_ID);
    final AtomicBoolean resetSent = new AtomicBoolean();
    when(stream.resetSent()).then(new Answer<Http2Stream>() {

        @Override
        public Http2Stream answer(InvocationOnMock invocationOnMock) {
            resetSent.set(true);
            return stream;
        }
    });
    when(stream.isResetSent()).then(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocationOnMock) {
            return resetSent.get();
        }
    });
    when(frameWriter.writeRstStream(eq(ctx), eq(STREAM_ID), anyLong(), any(ChannelPromise.class))).then(new Answer<ChannelFuture>() {

        @Override
        public ChannelFuture answer(InvocationOnMock invocationOnMock) throws Throwable {
            ChannelPromise promise = invocationOnMock.getArgument(3);
            return promise.setSuccess();
        }
    });
    ChannelPromise promise = new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE);
    final ChannelPromise promise2 = new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE);
    promise.addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) {
            handler.resetStream(ctx, STREAM_ID, STREAM_CLOSED.code(), promise2);
        }
    });
    handler.resetStream(ctx, STREAM_ID, CANCEL.code(), promise);
    verify(frameWriter).writeRstStream(eq(ctx), eq(STREAM_ID), anyLong(), any(ChannelPromise.class));
    assertTrue(promise.isSuccess());
    assertTrue(promise2.isSuccess());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) ChannelFutureListener(io.netty.channel.ChannelFutureListener) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) Mockito.anyBoolean(org.mockito.Mockito.anyBoolean) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.jupiter.api.Test)

Example 18 with DefaultChannelPromise

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

the class Http2ConnectionHandlerTest method canSendGoAwayFramesWithDecreasingLastStreamIds.

@Test
public void canSendGoAwayFramesWithDecreasingLastStreamIds() throws Exception {
    handler = newHandler();
    ByteBuf data = dummyData();
    long errorCode = Http2Error.INTERNAL_ERROR.code();
    handler.goAway(ctx, STREAM_ID + 2, errorCode, data.retain(), promise);
    verify(frameWriter).writeGoAway(eq(ctx), eq(STREAM_ID + 2), eq(errorCode), eq(data), eq(promise));
    verify(connection).goAwaySent(eq(STREAM_ID + 2), eq(errorCode), eq(data));
    promise = new DefaultChannelPromise(channel);
    handler.goAway(ctx, STREAM_ID, errorCode, data, promise);
    verify(frameWriter).writeGoAway(eq(ctx), eq(STREAM_ID), eq(errorCode), eq(data), eq(promise));
    verify(connection).goAwaySent(eq(STREAM_ID), eq(errorCode), eq(data));
    assertEquals(0, data.refCnt());
}
Also used : DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 19 with DefaultChannelPromise

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

the class DefaultHttp2FrameWriterTest method setUp.

@BeforeEach
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    http2HeadersEncoder = new DefaultHttp2HeadersEncoder(Http2HeadersEncoder.NEVER_SENSITIVE, new HpackEncoder(false, 16, 0));
    frameWriter = new DefaultHttp2FrameWriter(new DefaultHttp2HeadersEncoder(Http2HeadersEncoder.NEVER_SENSITIVE, new HpackEncoder(false, 16, 0)));
    outbound = Unpooled.buffer();
    expectedOutbound = Unpooled.EMPTY_BUFFER;
    promise = new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE);
    Answer<Object> answer = new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock var1) throws Throwable {
            Object msg = var1.getArgument(0);
            if (msg instanceof ByteBuf) {
                outbound.writeBytes((ByteBuf) msg);
            }
            ReferenceCountUtil.release(msg);
            return future;
        }
    };
    when(ctx.write(any())).then(answer);
    when(ctx.write(any(), any(ChannelPromise.class))).then(answer);
    when(ctx.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT);
    when(ctx.channel()).thenReturn(channel);
    when(ctx.executor()).thenReturn(ImmediateEventExecutor.INSTANCE);
}
Also used : Answer(org.mockito.stubbing.Answer) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 20 with DefaultChannelPromise

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

the class AbstractBootstrap method initAndRegister.

final ChannelFuture initAndRegister() {
    Channel channel = null;
    try {
        channel = channelFactory.newChannel();
        init(channel);
    } catch (Throwable t) {
        if (channel != null) {
            // channel can be null if newChannel crashed (eg SocketException("too many open files"))
            channel.unsafe().closeForcibly();
            // as the Channel is not registered yet we need to force the usage of the GlobalEventExecutor
            return new DefaultChannelPromise(channel, GlobalEventExecutor.INSTANCE).setFailure(t);
        }
        // as the Channel is not registered yet we need to force the usage of the GlobalEventExecutor
        return new DefaultChannelPromise(new FailedChannel(), GlobalEventExecutor.INSTANCE).setFailure(t);
    }
    ChannelFuture regFuture = config().group().register(channel);
    if (regFuture.cause() != null) {
        if (channel.isRegistered()) {
            channel.close();
        } else {
            channel.unsafe().closeForcibly();
        }
    }
    return regFuture;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) Channel(io.netty.channel.Channel)

Aggregations

DefaultChannelPromise (io.netty.channel.DefaultChannelPromise)22 ChannelPromise (io.netty.channel.ChannelPromise)13 Test (org.junit.Test)8 ChannelFuture (io.netty.channel.ChannelFuture)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 QueuedCommand (io.grpc.netty.WriteQueue.QueuedCommand)4 ByteBuf (io.netty.buffer.ByteBuf)4 ChannelFutureListener (io.netty.channel.ChannelFutureListener)4 Before (org.junit.Before)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 Channel (io.netty.channel.Channel)3 GenericFutureListener (io.netty.util.concurrent.GenericFutureListener)3 ChannelHandler (io.netty.channel.ChannelHandler)2 EventExecutor (io.netty.util.concurrent.EventExecutor)2 Future (io.netty.util.concurrent.Future)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InetSocketAddress (java.net.InetSocketAddress)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AssertionFailedError (junit.framework.AssertionFailedError)2