Example usage for io.netty.channel DefaultChannelPromise setSuccess

List of usage examples for io.netty.channel DefaultChannelPromise setSuccess

Introduction

In this page you can find the example usage for io.netty.channel DefaultChannelPromise setSuccess.

Prototype

@Override
    public ChannelPromise setSuccess() 

Source Link

Usage

From source file:com.github.spapageo.jannel.client.ClientSessionTest.java

License:Open Source License

@Test
public void testSendSmsAndWaitThrowsWhenOfferToWindowTimesOut() {
    DefaultChannelPromise promise = new DefaultChannelPromise(channel, eventExecutors.next());
    promise.setSuccess();

    when(channel.writeAndFlush(any())).thenReturn(promise);

    Sms sms = new Sms();
    sms.setId(UUID.randomUUID());
    sms.setBoxId("test box");
    clientSessionConfiguration.setRequestExpiryTimeout(1);

    try {//from ww  w.j  a v  a 2 s.  co m
        clientSession.sendSmsAndWait(sms, 1);
    } catch (InterruptedException e) {
        fail("Not the correct exception");
    } catch (ExecutionException e) {
        assertTrue(e.getCause() instanceof TimeoutException);
    }
}

From source file:com.github.spapageo.jannel.client.ClientSessionTest.java

License:Open Source License

@Test
public void testSendSmsAndWaitThrowsWhenOfferToWindowFails() throws Exception {
    DefaultChannelPromise promise = new DefaultChannelPromise(channel, eventExecutors.next());
    promise.setSuccess();

    when(channel.writeAndFlush(any())).thenReturn(promise);

    Sms sms = new Sms();
    sms.setId(UUID.randomUUID());
    sms.setBoxId("test box");

    //add the sms so the next offer fails
    clientSession.getWindow().offer(sms.getId(), sms, 5000);

    try {/*from   w  w  w .j  av a  2  s. c  o m*/
        clientSession.sendSmsAndWait(sms, 5000);
    } catch (ExecutionException e) {
        assertTrue(e.getCause() instanceof DuplicateKeyException);
    }
}

From source file:com.github.spapageo.jannel.client.JannelClientTest.java

License:Open Source License

@Test
public void testIdentifyAddsTheCorrectlyHandlersToThePipelineWithEnabledWriteTimeout() throws Exception {
    Channel channel = mock(Channel.class, Answers.RETURNS_SMART_NULLS.get());
    mockWriteHandler = mock(ChannelHandler.class);

    DefaultChannelPromise completedFuture = new DefaultChannelPromise(channel);
    completedFuture.setSuccess();

    ChannelPipeline channelPipeline = mock(ChannelPipeline.class);
    when(channelPipeline.addLast(anyString(), any(ChannelHandler.class))).thenReturn(channelPipeline);
    when(channelPipeline.addLast(any(EventExecutorGroup.class), anyString(), any(ChannelHandler.class)))
            .thenReturn(channelPipeline);
    when(channel.pipeline()).thenReturn(channelPipeline);
    when(channel.isActive()).thenReturn(true);
    when(channel.writeAndFlush(any())).thenReturn(completedFuture);
    when(channelHandlerProvider.getChangeHandler(eq(HandlerType.WRITE_TIMEOUT_HANDLER),
            any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class)))
                    .thenReturn(mockWriteHandler);
    when(channelHandlerProvider.getChangeHandler(eq(HandlerType.LENGTH_FRAME_ENCODER),
            any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class)))
                    .thenReturn(mockLengthWriteHandler);
    when(channelHandlerProvider.getChangeHandler(eq(HandlerType.LENGTH_FRAME_DECODER),
            any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class)))
                    .thenReturn(mockLengthReadHandler);
    when(channelHandlerProvider.getChangeHandler(eq(HandlerType.MESSAGE_ENCODER),
            any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class)))
                    .thenReturn(mockEncoderHandler);
    when(channelHandlerProvider.getChangeHandler(eq(HandlerType.MESSAGE_DECODER),
            any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class)))
                    .thenReturn(mockDecoderHandler);
    when(channelHandlerProvider.getChangeHandler(eq(HandlerType.MESSAGE_LOGGER),
            any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class)))
                    .thenReturn(mockLoggerHandler);
    when(channelHandlerProvider.getChangeHandler(eq(HandlerType.SESSION_WRAPPER),
            any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class)))
                    .thenReturn(mockSessionHandler);

    when(bootstrap.connect(anyString(), anyInt())).thenReturn(completedFuture);

    InOrder pipelineOrder = inOrder(channelPipeline);

    ClientSessionConfiguration configuration = new ClientSessionConfiguration();
    configuration.setWriteTimeout(1000);

    jannelClient.identify(configuration, null);

    pipelineOrder.verify(channelPipeline).addLast(HandlerType.WRITE_TIMEOUT_HANDLER.name(), mockWriteHandler);
    pipelineOrder.verify(channelPipeline).addLast(HandlerType.LENGTH_FRAME_DECODER.name(),
            mockLengthReadHandler);//from w  w w . j a v  a 2 s .  c  om
    pipelineOrder.verify(channelPipeline).addLast(HandlerType.LENGTH_FRAME_ENCODER.name(),
            mockLengthWriteHandler);
    pipelineOrder.verify(channelPipeline).addLast(HandlerType.MESSAGE_DECODER.name(), mockDecoderHandler);
    pipelineOrder.verify(channelPipeline).addLast(HandlerType.MESSAGE_ENCODER.name(), mockEncoderHandler);
    pipelineOrder.verify(channelPipeline).addLast(HandlerType.MESSAGE_LOGGER.name(), mockLoggerHandler);
    pipelineOrder.verify(channelPipeline).addLast(eventExecutors, HandlerType.SESSION_WRAPPER.name(),
            mockSessionHandler);
    pipelineOrder.verify(channelPipeline).remove(JannelClient.DummyChannelHandler.class);

    verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.WRITE_TIMEOUT_HANDLER), eq(configuration),
            any(ClientSession.class), eq(transcoder));
    verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.LENGTH_FRAME_DECODER), eq(configuration),
            any(ClientSession.class), eq(transcoder));
    verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.LENGTH_FRAME_ENCODER), eq(configuration),
            any(ClientSession.class), eq(transcoder));
    verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.MESSAGE_DECODER), eq(configuration),
            any(ClientSession.class), eq(transcoder));
    verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.MESSAGE_ENCODER), eq(configuration),
            any(ClientSession.class), eq(transcoder));
    verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.MESSAGE_LOGGER), eq(configuration),
            any(ClientSession.class), eq(transcoder));
    verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.SESSION_WRAPPER), eq(configuration),
            any(ClientSession.class), eq(transcoder));
}

From source file:com.github.spapageo.jannel.client.JannelClientTest.java

License:Open Source License

@Test
public void testIdentifyAddsTheCorrectlyHandlersToThePipelineWithDisabledWriteTimeout() throws Exception {
    Channel channel = mock(Channel.class, Answers.RETURNS_SMART_NULLS.get());

    DefaultChannelPromise completedFuture = new DefaultChannelPromise(channel);
    completedFuture.setSuccess();

    ChannelPipeline channelPipeline = mock(ChannelPipeline.class);
    when(channelPipeline.addLast(anyString(), any(ChannelHandler.class))).thenReturn(channelPipeline);
    when(channelPipeline.addLast(any(EventExecutorGroup.class), anyString(), any(ChannelHandler.class)))
            .thenReturn(channelPipeline);
    when(channel.pipeline()).thenReturn(channelPipeline);
    when(channel.isActive()).thenReturn(true);
    when(channel.writeAndFlush(any())).thenReturn(completedFuture);
    when(channelHandlerProvider.getChangeHandler(eq(HandlerType.WRITE_TIMEOUT_HANDLER),
            any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class)))
                    .thenReturn(mockWriteHandler);
    when(channelHandlerProvider.getChangeHandler(eq(HandlerType.LENGTH_FRAME_ENCODER),
            any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class)))
                    .thenReturn(mockLengthWriteHandler);
    when(channelHandlerProvider.getChangeHandler(eq(HandlerType.LENGTH_FRAME_DECODER),
            any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class)))
                    .thenReturn(mockLengthReadHandler);
    when(channelHandlerProvider.getChangeHandler(eq(HandlerType.MESSAGE_ENCODER),
            any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class)))
                    .thenReturn(mockEncoderHandler);
    when(channelHandlerProvider.getChangeHandler(eq(HandlerType.MESSAGE_DECODER),
            any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class)))
                    .thenReturn(mockDecoderHandler);
    when(channelHandlerProvider.getChangeHandler(eq(HandlerType.MESSAGE_LOGGER),
            any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class)))
                    .thenReturn(mockLoggerHandler);
    when(channelHandlerProvider.getChangeHandler(eq(HandlerType.SESSION_WRAPPER),
            any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class)))
                    .thenReturn(mockSessionHandler);

    when(bootstrap.connect(anyString(), anyInt())).thenReturn(completedFuture);

    InOrder pipelineOrder = inOrder(channelPipeline);

    ClientSessionConfiguration configuration = new ClientSessionConfiguration();

    jannelClient.identify(configuration, null);

    pipelineOrder.verify(channelPipeline).addLast(HandlerType.LENGTH_FRAME_DECODER.name(),
            mockLengthReadHandler);/*w  ww  .j  av a 2  s . com*/
    pipelineOrder.verify(channelPipeline).addLast(HandlerType.LENGTH_FRAME_ENCODER.name(),
            mockLengthWriteHandler);
    pipelineOrder.verify(channelPipeline).addLast(HandlerType.MESSAGE_DECODER.name(), mockDecoderHandler);
    pipelineOrder.verify(channelPipeline).addLast(HandlerType.MESSAGE_ENCODER.name(), mockEncoderHandler);
    pipelineOrder.verify(channelPipeline).addLast(HandlerType.MESSAGE_LOGGER.name(), mockLoggerHandler);
    pipelineOrder.verify(channelPipeline).addLast(eventExecutors, HandlerType.SESSION_WRAPPER.name(),
            mockSessionHandler);
    pipelineOrder.verify(channelPipeline).remove(JannelClient.DummyChannelHandler.class);

    verify(channelHandlerProvider, times(0)).getChangeHandler(eq(HandlerType.WRITE_TIMEOUT_HANDLER),
            eq(configuration), any(ClientSession.class), eq(transcoder));
    verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.LENGTH_FRAME_DECODER), eq(configuration),
            any(ClientSession.class), eq(transcoder));
    verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.LENGTH_FRAME_ENCODER), eq(configuration),
            any(ClientSession.class), eq(transcoder));
    verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.MESSAGE_DECODER), eq(configuration),
            any(ClientSession.class), eq(transcoder));
    verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.MESSAGE_ENCODER), eq(configuration),
            any(ClientSession.class), eq(transcoder));
    verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.MESSAGE_LOGGER), eq(configuration),
            any(ClientSession.class), eq(transcoder));
    verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.SESSION_WRAPPER), eq(configuration),
            any(ClientSession.class), eq(transcoder));
}

From source file:com.github.spapageo.jannel.client.JannelClientTest.java

License:Open Source License

@Test
public void testIdentifyConnectsToCorrectRemoteServerWithConnectionTimeout() throws Exception {

    Channel channel = mock(Channel.class, Answers.RETURNS_SMART_NULLS.get());
    mockWriteHandler = mock(ChannelHandler.class);

    DefaultChannelPromise completedFuture = new DefaultChannelPromise(channel);
    completedFuture.setSuccess();

    ChannelPipeline channelPipeline = mock(ChannelPipeline.class);
    when(channelPipeline.addLast(anyString(), any(ChannelHandler.class))).thenReturn(channelPipeline);
    when(channelPipeline.addLast(any(EventExecutorGroup.class), anyString(), any(ChannelHandler.class)))
            .thenReturn(channelPipeline);
    when(channel.pipeline()).thenReturn(channelPipeline);
    when(channel.isActive()).thenReturn(true);
    when(channel.writeAndFlush(any())).thenReturn(completedFuture);

    when(bootstrap.connect(anyString(), anyInt())).thenReturn(completedFuture);

    ClientSessionConfiguration configuration = new ClientSessionConfiguration();
    configuration.setHost("testHost");
    configuration.setPort(1111);//from w  w  w. j ava  2s  .co m
    configuration.setConnectTimeout(10000);

    jannelClient.identify(configuration, null);

    verify(bootstrap).connect(configuration.getHost(), configuration.getPort());
    verify(bootstrap).option(ChannelOption.valueOf("connectTimeoutMillis"), configuration.getConnectTimeout());
}

From source file:com.github.spapageo.jannel.client.JannelClientTest.java

License:Open Source License

@Test
public void testIdentifySendsCorrectIdentifyCommand() throws Exception {
    Channel channel = mock(Channel.class, Answers.RETURNS_SMART_NULLS.get());
    mockWriteHandler = mock(ChannelHandler.class);

    DefaultChannelPromise completedFuture = new DefaultChannelPromise(channel);
    completedFuture.setSuccess();

    ChannelPipeline channelPipeline = mock(ChannelPipeline.class);
    when(channelPipeline.addLast(anyString(), any(ChannelHandler.class))).thenReturn(channelPipeline);
    when(channelPipeline.addLast(any(EventExecutorGroup.class), anyString(), any(ChannelHandler.class)))
            .thenReturn(channelPipeline);
    when(channel.pipeline()).thenReturn(channelPipeline);
    when(channel.isActive()).thenReturn(true);
    when(channel.writeAndFlush(any())).thenReturn(completedFuture);

    when(bootstrap.connect(anyString(), anyInt())).thenReturn(completedFuture);

    ClientSessionConfiguration configuration = new ClientSessionConfiguration();
    configuration.setClientId("testId");

    jannelClient.identify(configuration, null);

    ArgumentCaptor<Admin> captor = ArgumentCaptor.forClass(Admin.class);

    verify(channel).writeAndFlush(captor.capture());

    Admin command = captor.getValue();//  ww  w . j a v a 2s  . co m

    assertEquals("Wrong command type", AdminCommand.IDENTIFY, command.getAdminCommand());
    assertEquals("Wrong client id", configuration.getClientId(), command.getBoxId());
}

From source file:com.github.spapageo.jannel.client.JannelClientTest.java

License:Open Source License

@Test(expected = PrematureChannelClosureException.class)
public void testIdentifyCloseChannelOnFailure() throws Exception {
    Channel channel = mock(Channel.class, Answers.RETURNS_SMART_NULLS.get());
    mockWriteHandler = mock(ChannelHandler.class);

    DefaultChannelPromise completedFuture = new DefaultChannelPromise(channel);
    completedFuture.setSuccess();

    DefaultChannelPromise failedFuture = new DefaultChannelPromise(channel);
    failedFuture.setFailure(new PrematureChannelClosureException("test"));

    ChannelPipeline channelPipeline = mock(ChannelPipeline.class);
    when(channelPipeline.addLast(anyString(), any(ChannelHandler.class))).thenReturn(channelPipeline);
    when(channelPipeline.addLast(any(EventExecutorGroup.class), anyString(), any(ChannelHandler.class)))
            .thenReturn(channelPipeline);
    when(channel.pipeline()).thenReturn(channelPipeline);
    when(channel.isActive()).thenReturn(true);
    when(channel.writeAndFlush(any())).thenReturn(failedFuture);
    when(channel.close()).thenReturn(completedFuture);

    when(bootstrap.connect(anyString(), anyInt())).thenReturn(completedFuture);

    ClientSessionConfiguration configuration = new ClientSessionConfiguration();

    jannelClient.identify(configuration, null);
}

From source file:io.apigee.trireme.container.netty.NettyHttpResponse.java

License:Open Source License

@Override
public HttpFuture sendChunk(ByteBuffer buf, boolean lastChunk) {
    ChannelFuture future = null;//  w  w w.j  av a  2 s.  c  om
    if (buf != null) {
        if (log.isDebugEnabled()) {
            log.debug("sendChunk: Sending HTTP chunk {}", buf);
        }
        DefaultHttpContent chunk = new DefaultHttpContent(NettyServer.copyBuffer(buf));
        future = channel.write(chunk);
    }

    if (lastChunk) {
        future = sendLastChunk();
    }
    channel.flush();
    if (lastChunk && !keepAlive) {
        shutDown();
    }

    if (future == null) {
        DefaultChannelPromise doneFuture = new DefaultChannelPromise(channel);
        doneFuture.setSuccess();
        future = doneFuture;
    }
    return new NettyHttpFuture(future);
}

From source file:org.opendaylight.netconf.nettyutil.AbstractNetconfSession.java

License:Open Source License

@Override
public ChannelFuture sendMessage(final NetconfMessage netconfMessage) {
    // From: https://github.com/netty/netty/issues/3887
    // Netty can provide "ordering" in the following situations:
    // 1. You are doing all writes from the EventLoop thread; OR
    // 2. You are doing no writes from the EventLoop thread (i.e. all writes are being done in other thread(s)).
    ////from   ww w.ja  va2 s.  com
    // Restconf writes to a netconf mountpoint execute multiple messages
    // and one of these was executed from a restconf thread thus breaking ordering so
    // we need to execute all messages from an EventLoop thread.
    final DefaultChannelPromise proxyFuture = new DefaultChannelPromise(channel);
    channel.eventLoop().execute(new Runnable() {
        @Override
        public void run() {
            final ChannelFuture future = channel.writeAndFlush(netconfMessage);
            future.addListener(new FutureListener<Void>() {
                @Override
                public void operationComplete(Future<Void> future) throws Exception {
                    if (future.isSuccess()) {
                        proxyFuture.setSuccess();
                    } else {
                        proxyFuture.setFailure(future.cause());
                    }
                }
            });
            if (delayedEncoder != null) {
                replaceMessageEncoder(delayedEncoder);
                delayedEncoder = null;
            }
        }
    });

    return proxyFuture;
}