Example usage for io.netty.channel DefaultChannelPromise DefaultChannelPromise

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

Introduction

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

Prototype

public DefaultChannelPromise(Channel channel, EventExecutor executor) 

Source Link

Document

Creates a new instance.

Usage

From source file:io.lettuce.core.protocol.DefaultEndpointTest.java

License:Apache License

@Test
public void shouldCancelCommandsOnEncoderException() {

    when(channel.isActive()).thenReturn(true);
    sut.notifyChannelActive(channel);/*from  w  w w .  j  a va 2  s  . c  om*/

    DefaultChannelPromise promise = new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE);

    when(channel.writeAndFlush(any())).thenAnswer(invocation -> {
        if (invocation.getArguments()[0] instanceof RedisCommand) {
            queue.add((RedisCommand) invocation.getArguments()[0]);
        }

        if (invocation.getArguments()[0] instanceof Collection) {
            queue.addAll((Collection) invocation.getArguments()[0]);
        }
        return promise;
    });

    promise.setFailure(new EncoderException("foo"));

    sut.write(command);

    assertThat(command.exception).isInstanceOf(EncoderException.class);
}

From source file:io.reactivex.netty.contexts.NoOpChannelHandlerContext.java

License:Apache License

public NoOpChannelHandlerContext() {
    channel = new LocalChannel() {
        @Override/*from  w  w w.j a  va2s.  co m*/
        public <T> Attribute<T> attr(AttributeKey<T> key) {
            return attributeMap.attr(key);
        }
    };
    failedPromise = new DefaultChannelPromise(channel, executor());
    failedPromise.setFailure(new OperationNotSupportedException());
}

From source file:io.reactivex.netty.NoOpChannelHandlerContext.java

License:Apache License

public NoOpChannelHandlerContext(Channel channel) {
    this.channel = channel;
    completedPromise = new DefaultChannelPromise(this.channel, executor());
    completedPromise.setSuccess();//from   w  w w.j a  v  a 2  s .  c  om
}

From source file:io.reactivex.netty.NoOpChannelHandlerContext.java

License:Apache License

@Override
public ChannelPromise newPromise() {
    return new DefaultChannelPromise(channel, executor());
}

From source file:io.soliton.protobuf.json.HttpJsonRpcClientTest.java

License:Apache License

@Test
public void testEncodeMethodCallFailure() throws InvalidProtocolBufferException, InterruptedException {
    Channel channel = Mockito.mock(Channel.class);

    Mockito.when(channel.remoteAddress())
            .thenReturn(new InetSocketAddress(InetAddress.getLoopbackAddress(), 10000));

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

    DefaultChannelPromise failure = new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE);
    failure.setFailure(new Exception("OMGWTF"));
    Mockito.when(channel.writeAndFlush(captor.capture())).thenReturn(failure);

    JsonRpcClientHandler handler = new JsonRpcClientHandler();
    HttpJsonRpcClient client = new HttpJsonRpcClient(channel, handler, "/rpc", new NullClientLogger());

    ClientMethod<TimeResponse> method = Mockito.mock(ClientMethod.class);
    Mockito.when(method.serviceName()).thenReturn("TimeService");
    Mockito.when(method.name()).thenReturn("GetTime");
    Mockito.when(method.outputParser()).thenReturn(TimeResponse.PARSER);

    final CountDownLatch latch = new CountDownLatch(1);
    FutureCallback<TimeResponse> callback = new FutureCallback<TimeResponse>() {
        @Override/*from  w  w w .  j a v  a  2s . c  o  m*/
        public void onSuccess(@Nullable TimeResponse result) {
        }

        @Override
        public void onFailure(Throwable t) {
            Assert.assertEquals("OMGWTF", t.getMessage());
            latch.countDown();
        }
    };

    ListenableFuture<TimeResponse> future = client.encodeMethodCall(method,
            TimeRequest.newBuilder().setTimezone("UTC").build());

    Futures.addCallback(future, callback);
    latch.await(5, TimeUnit.SECONDS);

    Assert.assertEquals(0, handler.inFlightRequests().size());
}

From source file:io.soliton.protobuf.socket.RpcClientTest.java

License:Apache License

@Test
public void testEncodeMethodCallFailure() throws InvalidProtocolBufferException, InterruptedException {
    Channel channel = Mockito.mock(Channel.class);
    ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class);

    DefaultChannelPromise failure = new DefaultChannelPromise(channel, ImmediateEventExecutor.INSTANCE);
    failure.setFailure(new Exception("OMGWTF"));
    Mockito.when(channel.writeAndFlush(captor.capture())).thenReturn(failure);

    RpcClientHandler handler = new RpcClientHandler();
    RpcClient client = new RpcClient(channel, handler, new NullClientLogger());

    ClientMethod<TimeResponse> method = Mockito.mock(ClientMethod.class);
    Mockito.when(method.serviceName()).thenReturn("TimeService");
    Mockito.when(method.name()).thenReturn("GetTime");
    Mockito.when(method.outputParser()).thenReturn(TimeResponse.PARSER);

    final CountDownLatch latch = new CountDownLatch(1);
    FutureCallback<TimeResponse> callback = new FutureCallback<TimeResponse>() {
        @Override/*from  w w  w . j a  v a  2s. c o  m*/
        public void onSuccess(@Nullable TimeResponse result) {
        }

        @Override
        public void onFailure(Throwable t) {
            Assert.assertEquals("OMGWTF", t.getMessage());
            latch.countDown();
        }
    };

    ListenableFuture<TimeResponse> future = client.encodeMethodCall(method,
            TimeRequest.newBuilder().setTimezone("UTC").build());

    Futures.addCallback(future, callback);
    latch.await(5, TimeUnit.SECONDS);

    Assert.assertEquals(0, handler.inFlightRequests().size());
}

From source file:org.apache.drill.exec.server.rest.WebSessionResourcesTest.java

License:Apache License

/**
 * Validates successful {@link WebSessionResources#close()} with valid CloseFuture and other parameters.
 * @throws Exception//  ww w. j  ava2  s .  c  o  m
 */
@Test
public void testChannelPromiseWithValidExecutor() throws Exception {
    try {
        EventExecutor mockExecutor = mock(EventExecutor.class);
        ChannelPromise closeFuture = new DefaultChannelPromise(null, mockExecutor);
        webSessionResources = new WebSessionResources(mock(BufferAllocator.class), mock(SocketAddress.class),
                mock(UserSession.class), closeFuture);
        webSessionResources.close();
        verify(webSessionResources.getAllocator()).close();
        verify(webSessionResources.getSession()).close();
        verify(mockExecutor).inEventLoop();
        verify(mockExecutor).execute(any(Runnable.class));
        assertTrue(webSessionResources.getCloseFuture() == null);
        assertTrue(!listenerComplete);
    } catch (Exception e) {
        fail();
    }
}

From source file:org.apache.drill.exec.server.rest.WebSessionResourcesTest.java

License:Apache License

/**
 * Validates double call to {@link WebSessionResources#close()} doesn't throw any exception.
 * @throws Exception//from   w w w. j  a  v  a2  s  .  c  o  m
 */
@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();
    }
}

From source file:org.apache.drill.exec.server.rest.WebSessionResourcesTest.java

License:Apache License

/**
 * Validates successful {@link WebSessionResources#close()} with valid CloseFuture and {@link TestClosedListener}
 * getting invoked which is added to the close future.
 * @throws Exception//from w  w w.  ja v a  2  s  .c  o  m
 */
@Test
public void testCloseWithListener() throws Exception {
    try {
        // Assign latch, executor and closeListener for this test case
        GenericFutureListener<Future<Void>> closeListener = new TestClosedListener();
        latch = new CountDownLatch(1);
        executor = TransportCheck.createEventLoopGroup(1, "Test-Thread").next();
        ChannelPromise closeFuture = new DefaultChannelPromise(null, executor);

        // create WebSessionResources with above ChannelPromise to notify listener
        webSessionResources = new WebSessionResources(mock(BufferAllocator.class), mock(SocketAddress.class),
                mock(UserSession.class), closeFuture);

        // Add the Test Listener to close future
        assertTrue(!listenerComplete);
        closeFuture.addListener(closeListener);

        // Close the WebSessionResources
        webSessionResources.close();

        // Verify the states
        verify(webSessionResources.getAllocator()).close();
        verify(webSessionResources.getSession()).close();
        assertTrue(webSessionResources.getCloseFuture() == null);

        // Since listener will be invoked so test should not wait forever
        latch.await();
        assertTrue(listenerComplete);
    } catch (Exception e) {
        fail();
    } finally {
        listenerComplete = false;
        executor.shutdownGracefully();
    }
}