Search in sources :

Example 6 with DefaultChannelPromise

use of io.netty.channel.DefaultChannelPromise in project x-pipe by ctripcorp.

the class DefaultRedisSlaveTest method beforeDefaultRedisSlaveTest.

@Before
public void beforeDefaultRedisSlaveTest() {
    when(channel.closeFuture()).thenReturn(new DefaultChannelPromise(channel));
    when(channel.remoteAddress()).thenReturn(localhostInetAddress(randomPort()));
    RedisClient redisClient = new DefaultRedisClient(channel, redisKeeperServer);
    redisSlave = new DefaultRedisSlave(redisClient);
    redisSlave.setRdbDumpMaxWaitMilli(waitDumpMilli);
}
Also used : RedisClient(com.ctrip.xpipe.redis.keeper.RedisClient) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) Before(org.junit.Before)

Example 7 with DefaultChannelPromise

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

the class DefaultHttp2ConnectionDecoderTest method setup.

@BeforeEach
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    promise = new DefaultChannelPromise(channel);
    final AtomicInteger headersReceivedState = new AtomicInteger();
    when(channel.isActive()).thenReturn(true);
    when(stream.id()).thenReturn(STREAM_ID);
    when(stream.state()).thenReturn(OPEN);
    when(stream.open(anyBoolean())).thenReturn(stream);
    final Map<Object, Object> properties = new IdentityHashMap<Object, Object>();
    when(stream.getProperty(ArgumentMatchers.<Http2Connection.PropertyKey>any())).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) {
            return properties.get(invocationOnMock.getArgument(0));
        }
    });
    when(stream.setProperty(ArgumentMatchers.<Http2Connection.PropertyKey>any(), any())).then(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) {
            return properties.put(invocationOnMock.getArgument(0), invocationOnMock.getArgument(1));
        }
    });
    when(pushStream.id()).thenReturn(PUSH_STREAM_ID);
    doAnswer(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock in) throws Throwable {
            return (headersReceivedState.get() & STATE_RECV_HEADERS) != 0;
        }
    }).when(stream).isHeadersReceived();
    doAnswer(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock in) throws Throwable {
            return (headersReceivedState.get() & STATE_RECV_TRAILERS) != 0;
        }
    }).when(stream).isTrailersReceived();
    doAnswer(new Answer<Http2Stream>() {

        @Override
        public Http2Stream answer(InvocationOnMock in) throws Throwable {
            boolean isInformational = in.getArgument(0);
            if (isInformational) {
                return stream;
            }
            for (; ; ) {
                int current = headersReceivedState.get();
                int next = current;
                if ((current & STATE_RECV_HEADERS) != 0) {
                    if ((current & STATE_RECV_TRAILERS) != 0) {
                        throw new IllegalStateException("already sent headers!");
                    }
                    next |= STATE_RECV_TRAILERS;
                } else {
                    next |= STATE_RECV_HEADERS;
                }
                if (headersReceivedState.compareAndSet(current, next)) {
                    break;
                }
            }
            return stream;
        }
    }).when(stream).headersReceived(anyBoolean());
    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(STREAM_ID)).thenReturn(stream);
    when(connection.streamMayHaveExisted(STREAM_ID)).thenReturn(true);
    when(connection.local()).thenReturn(local);
    when(local.flowController()).thenReturn(localFlow);
    when(encoder.flowController()).thenReturn(remoteFlow);
    when(encoder.frameWriter()).thenReturn(writer);
    when(connection.remote()).thenReturn(remote);
    when(local.reservePushStream(eq(PUSH_STREAM_ID), eq(stream))).thenReturn(pushStream);
    when(remote.reservePushStream(eq(PUSH_STREAM_ID), eq(stream))).thenReturn(pushStream);
    when(ctx.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT);
    when(ctx.channel()).thenReturn(channel);
    when(ctx.newSucceededFuture()).thenReturn(future);
    when(ctx.newPromise()).thenReturn(promise);
    when(ctx.write(any())).thenReturn(future);
    decoder = new DefaultHttp2ConnectionDecoder(connection, encoder, reader);
    decoder.lifecycleManager(lifecycleManager);
    decoder.frameListener(listener);
    // Simulate receiving the initial settings from the remote endpoint.
    decode().onSettingsRead(ctx, new Http2Settings());
    verify(listener).onSettingsRead(eq(ctx), eq(new Http2Settings()));
    assertTrue(decoder.prefaceReceived());
    verify(encoder).writeSettingsAck(eq(ctx), eq(promise));
    // Simulate receiving the SETTINGS ACK for the initial settings.
    decode().onSettingsAckRead(ctx);
    // Disallow any further flushes now that settings ACK has been sent
    when(ctx.flush()).thenThrow(new AssertionFailedError("forbidden"));
}
Also used : IdentityHashMap(java.util.IdentityHashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Mockito.anyBoolean(org.mockito.Mockito.anyBoolean) AssertionFailedError(junit.framework.AssertionFailedError) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 8 with DefaultChannelPromise

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

the class Http2FrameRoundtripTest method setup.

@BeforeEach
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    when(ctx.alloc()).thenReturn(alloc);
    when(ctx.executor()).thenReturn(executor);
    when(ctx.channel()).thenReturn(channel);
    doAnswer(new Answer<ByteBuf>() {

        @Override
        public ByteBuf answer(InvocationOnMock in) throws Throwable {
            return Unpooled.buffer();
        }
    }).when(alloc).buffer();
    doAnswer(new Answer<ByteBuf>() {

        @Override
        public ByteBuf answer(InvocationOnMock in) throws Throwable {
            return Unpooled.buffer((Integer) in.getArguments()[0]);
        }
    }).when(alloc).buffer(anyInt());
    doAnswer(new Answer<ChannelPromise>() {

        @Override
        public ChannelPromise answer(InvocationOnMock invocation) throws Throwable {
            return new DefaultChannelPromise(channel, GlobalEventExecutor.INSTANCE);
        }
    }).when(ctx).newPromise();
    writer = new DefaultHttp2FrameWriter(new DefaultHttp2HeadersEncoder(NEVER_SENSITIVE, newTestEncoder()));
    reader = new DefaultHttp2FrameReader(new DefaultHttp2HeadersDecoder(false, newTestDecoder()));
}
Also used : ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) EmptyByteBuf(io.netty.buffer.EmptyByteBuf) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 9 with DefaultChannelPromise

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

the class Http2TestUtil method newVoidPromise.

static ChannelPromise newVoidPromise(final Channel channel) {
    return new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE) {

        @Override
        public ChannelPromise addListener(GenericFutureListener<? extends Future<? super Void>> listener) {
            throw new AssertionFailedError();
        }

        @Override
        public ChannelPromise addListeners(GenericFutureListener<? extends Future<? super Void>>... listeners) {
            throw new AssertionFailedError();
        }

        @Override
        public boolean isVoid() {
            return true;
        }

        @Override
        public boolean tryFailure(Throwable cause) {
            channel().pipeline().fireExceptionCaught(cause);
            return true;
        }

        @Override
        public ChannelPromise setFailure(Throwable cause) {
            tryFailure(cause);
            return this;
        }

        @Override
        public ChannelPromise unvoid() {
            ChannelPromise promise = new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE);
            promise.addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (!future.isSuccess()) {
                        channel().pipeline().fireExceptionCaught(future.cause());
                    }
                }
            });
            return promise;
        }
    };
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) ChannelFuture(io.netty.channel.ChannelFuture) Future(io.netty.util.concurrent.Future) ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) AssertionFailedError(junit.framework.AssertionFailedError) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 10 with DefaultChannelPromise

use of io.netty.channel.DefaultChannelPromise in project grpc-java by grpc.

the class NettyStreamTestBase method shouldNotBeReadyForDataAfterWritingLargeMessage.

@Test
public void shouldNotBeReadyForDataAfterWritingLargeMessage() throws IOException {
    sendHeadersIfServer();
    // Make sure the writes don't complete so we "back up"
    ChannelPromise uncompletedPromise = new DefaultChannelPromise(channel);
    when(writeQueue.enqueue(any(QueuedCommand.class), anyBoolean())).thenReturn(uncompletedPromise);
    assertTrue(stream.isReady());
    byte[] msg = largeMessage();
    stream.writeMessage(new ByteArrayInputStream(msg));
    stream.flush();
    assertFalse(stream.isReady());
    verify(listener(), never()).onReady();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) QueuedCommand(io.grpc.netty.WriteQueue.QueuedCommand) Test(org.junit.Test)

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