Search in sources :

Example 11 with DefaultChannelPromise

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

the class NettyClientStreamTest method removeUserAgentFromApplicationHeaders.

@Test
public void removeUserAgentFromApplicationHeaders() {
    Metadata metadata = new Metadata();
    metadata.put(GrpcUtil.USER_AGENT_KEY, "bad agent");
    listener = mock(ClientStreamListener.class);
    Mockito.reset(writeQueue);
    ChannelPromise completedPromise = new DefaultChannelPromise(channel).setSuccess();
    when(writeQueue.enqueue(any(QueuedCommand.class), anyBoolean())).thenReturn(completedPromise);
    stream = new NettyClientStream(new TransportStateImpl(handler, DEFAULT_MAX_MESSAGE_SIZE), methodDescriptor, new Metadata(), channel, AsciiString.of("localhost"), AsciiString.of("http"), AsciiString.of("good agent"), StatsTraceContext.NOOP, transportTracer, CallOptions.DEFAULT, false);
    stream.start(listener);
    ArgumentCaptor<CreateStreamCommand> cmdCap = ArgumentCaptor.forClass(CreateStreamCommand.class);
    verify(writeQueue).enqueue(cmdCap.capture(), eq(false));
    assertThat(ImmutableListMultimap.copyOf(cmdCap.getValue().headers())).containsEntry(Utils.USER_AGENT, AsciiString.of("good agent"));
}
Also used : ClientStreamListener(io.grpc.internal.ClientStreamListener) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) Metadata(io.grpc.Metadata) ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) QueuedCommand(io.grpc.netty.WriteQueue.QueuedCommand) Test(org.junit.Test)

Example 12 with DefaultChannelPromise

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

the class AbstractPCEPSessionTest method setUp.

@Before
public final void setUp() {
    MockitoAnnotations.initMocks(this);
    final ChannelFuture future = new DefaultChannelPromise(this.channel);
    doAnswer(invocation -> {
        final Object[] args = invocation.getArguments();
        AbstractPCEPSessionTest.this.msgsSend.add((Notification) args[0]);
        return future;
    }).when(this.channel).writeAndFlush(any(Notification.class));
    doReturn(this.channelFuture).when(this.channel).closeFuture();
    doReturn(this.channelFuture).when(this.channelFuture).addListener(any(GenericFutureListener.class));
    doReturn("TestingChannel").when(this.channel).toString();
    doReturn(this.pipeline).when(this.channel).pipeline();
    doReturn(this.address).when(this.channel).localAddress();
    doReturn(this.address).when(this.channel).remoteAddress();
    doReturn(this.eventLoop).when(this.channel).eventLoop();
    doReturn(true).when(this.future).cancel(false);
    doReturn(this.future).when(this.eventLoop).schedule(any(Runnable.class), any(long.class), any(TimeUnit.class));
    doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class));
    doReturn(this.pipeline).when(this.pipeline).addFirst(any(ChannelHandler.class));
    doReturn(true).when(this.channel).isActive();
    doReturn(mock(ChannelFuture.class)).when(this.channel).close();
    doReturn(new InetSocketAddress(this.ipAddress, this.port)).when(this.channel).remoteAddress();
    doReturn(new InetSocketAddress(this.ipAddress, this.port)).when(this.channel).localAddress();
    this.openMsg = new OpenBuilder().setOpenMessage(new OpenMessageBuilder().setOpen(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder().setDeadTimer(DEADTIMER).setKeepalive(KEEP_ALIVE).setSessionId((short) 0).build()).build()).build();
    this.kaMsg = new KeepaliveBuilder().setKeepaliveMessage(new KeepaliveMessageBuilder().build()).build();
    this.startTlsMsg = new StarttlsBuilder().setStartTlsMessage(new StartTlsMessageBuilder().build()).build();
    this.closeMsg = new CloseBuilder().setCCloseMessage(new CCloseMessageBuilder().setCClose(new CCloseBuilder().setReason((short) 6).build()).build()).build();
    this.listener = new SimpleSessionListener();
}
Also used : CloseBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.CloseBuilder) CCloseBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.object.CCloseBuilder) MockitoAnnotations(org.mockito.MockitoAnnotations) InetSocketAddress(java.net.InetSocketAddress) ChannelHandler(io.netty.channel.ChannelHandler) CCloseBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.object.CCloseBuilder) Notification(org.opendaylight.yangtools.yang.binding.Notification) OpenMessageBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessageBuilder) TimeUnit(java.util.concurrent.TimeUnit) KeepaliveMessageBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.keepalive.message.KeepaliveMessageBuilder) KeepaliveBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.KeepaliveBuilder) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) ChannelFuture(io.netty.channel.ChannelFuture) OpenBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.OpenBuilder) StartTlsMessageBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.start.tls.message.StartTlsMessageBuilder) StarttlsBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.StarttlsBuilder) Mockito.doReturn(org.mockito.Mockito.doReturn) CCloseMessageBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.close.message.CCloseMessageBuilder) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) Before(org.junit.Before)

Example 13 with DefaultChannelPromise

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

the class WebSessionResourcesTest method testDoubleClose.

/**
 * Validates double call to {@link WebSessionResources#close()} doesn't throw any exception.
 * @throws Exception
 */
@Test
public void testDoubleClose() throws Exception {
    try {
        ChannelPromise closeFuture = new DefaultChannelPromise(null, mock(EventExecutor.class));
        webSessionResources = new WebSessionResources(mock(BufferAllocator.class), mock(SocketAddress.class), mock(UserSession.class), closeFuture);
        webSessionResources.close();
        verify(webSessionResources.getAllocator()).close();
        verify(webSessionResources.getSession()).close();
        assertTrue(webSessionResources.getCloseFuture() == null);
        webSessionResources.close();
    } catch (Exception e) {
        fail();
    }
}
Also used : EventExecutor(io.netty.util.concurrent.EventExecutor) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) Test(org.junit.Test)

Example 14 with DefaultChannelPromise

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

the class WebSessionResourcesTest method testChannelPromiseWithNullExecutor.

/**
 * Validates {@link WebSessionResources#close()} throws NPE when closefuture passed to WebSessionResources doesn't
 * have a valid channel and EventExecutor associated with it.
 * @throws Exception
 */
@Test
public void testChannelPromiseWithNullExecutor() throws Exception {
    try {
        ChannelPromise closeFuture = new DefaultChannelPromise(null);
        webSessionResources = new WebSessionResources(mock(BufferAllocator.class), mock(SocketAddress.class), mock(UserSession.class), closeFuture);
        webSessionResources.close();
        fail();
    } catch (Exception e) {
        assertTrue(e instanceof NullPointerException);
        verify(webSessionResources.getAllocator()).close();
        verify(webSessionResources.getSession()).close();
    }
}
Also used : DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) Test(org.junit.Test)

Example 15 with DefaultChannelPromise

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

the class DefaultRedisMasterReplicationTest method testCancelScheduleWhenConnected.

@Test
public void testCancelScheduleWhenConnected() throws IOException {
    AtomicInteger replConfCount = new AtomicInteger();
    defaultRedisMasterReplication = new DefaultRedisMasterReplication(redisMaster, redisKeeperServer, nioEventLoopGroup, scheduled, replTimeoutMilli) {

        @Override
        protected Command<Object> createReplConf() {
            replConfCount.incrementAndGet();
            return super.createReplConf();
        }
    };
    defaultRedisMasterReplication.onContinue(RunidGenerator.DEFAULT.generateRunid(), RunidGenerator.DEFAULT.generateRunid());
    Channel channel = mock(Channel.class);
    when(channel.closeFuture()).thenReturn(new DefaultChannelPromise(channel));
    defaultRedisMasterReplication.masterConnected(channel);
    int countBefore = replConfCount.get();
    sleep(DefaultRedisMasterReplication.REPLCONF_INTERVAL_MILLI * 2);
    int countAfter = replConfCount.get();
    Assert.assertEquals(countBefore, countAfter);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Command(com.ctrip.xpipe.api.command.Command) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) Channel(io.netty.channel.Channel) DefaultEndPoint(com.ctrip.xpipe.endpoint.DefaultEndPoint) AbstractRedisKeeperTest(com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest) 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