Example usage for java.util.concurrent CompletableFuture isCompletedExceptionally

List of usage examples for java.util.concurrent CompletableFuture isCompletedExceptionally

Introduction

In this page you can find the example usage for java.util.concurrent CompletableFuture isCompletedExceptionally.

Prototype

public boolean isCompletedExceptionally() 

Source Link

Document

Returns true if this CompletableFuture completed exceptionally, in any way.

Usage

From source file:io.ventu.rpc.amqp.AmqpInvokerimplTest.java

@Test
public void responseReceiver_handleDelivery_onEncodingException_exception()
        throws EncodingException, IOException, InterruptedException, ExecutionException, TimeoutException {
    ResponseReceiverImpl receiver = new ResponseReceiverImpl(serializer, new Validator() {
    }, 1, TimeUnit.MINUTES);//from w  w w  .j  a  v a 2 s .  c  o m

    String correlationId = "987654321";
    CompletableFuture<Res> answer = receiver.put(correlationId, Res.class);
    assertFalse(answer.isDone());
    assertFalse(answer.isCompletedExceptionally());

    receiver.handleDelivery(correlationId, new byte[] {});
    assertTrue(answer.isDone());
    assertTrue(answer.isCompletedExceptionally());

    exception.expect(ExecutionException.class);
    try {
        answer.get();
    } catch (ExecutionException ex) {
        assertTrue(ex.getCause() instanceof EncodingException);
        assertEquals("failed to decode JSON", ex.getCause().getMessage());
        throw ex;
    }
}

From source file:io.ventu.rpc.amqp.AmqpInvokerimplTest.java

@Test
public void responseReceiver_put_createsFuture_andCleansUpExpires()
        throws InterruptedException, ExecutionException, TimeoutException {
    ResponseReceiver receiver = new ResponseReceiverImpl(serializer, new Validator() {
    }, 100, TimeUnit.MILLISECONDS);
    CompletableFuture<Res> actual = receiver.put("987654321", Res.class);
    assertFalse(actual.isDone());//w  w w.  j a  v a  2s .  co  m
    assertFalse(actual.isCompletedExceptionally());

    // wait for expiry timeout
    Thread.sleep(150);
    // trigger cache operations (to evict the record)
    for (int i = 0; i < 1000; i++) {
        receiver.put(UUID.randomUUID().toString(), Res.class);
    }

    exception.expect(ExecutionException.class);
    try {
        actual.get();
    } catch (ExecutionException ex) {
        assertTrue(ex.getCause() instanceof TimeoutException);
        assertEquals(
                "Request io.ventu.rpc.amqp.AmqpInvokerimplTest$Res with correlationId 987654321 has expired.",
                ex.getCause().getMessage());
        throw ex;
    }
}

From source file:io.ventu.rpc.amqp.AmqpInvokerimplTest.java

@Test
public void responseReceiver_handleDelivery_onEncodingException_withErrorField_APIException()
        throws EncodingException, IOException, InterruptedException, ExecutionException, TimeoutException {
    ResponseReceiverImpl receiver = new ResponseReceiverImpl(serializer, new Validator() {
    }, 1, TimeUnit.MINUTES);//from   w ww  . ja v a  2  s.com

    String correlationId = "987654321";
    CompletableFuture<Res> answer = receiver.put(correlationId, Res.class);
    assertFalse(answer.isDone());
    assertFalse(answer.isCompletedExceptionally());

    Map<String, Object> res = Maps.newHashMap();
    res.put("error", Integer.valueOf(371));

    receiver.handleDelivery(correlationId, serializer.encode(res));
    assertTrue(answer.isDone());
    assertTrue(answer.isCompletedExceptionally());

    exception.expect(ExecutionException.class);
    try {
        answer.get();
    } catch (ExecutionException ex) {
        assertTrue(ex.getCause() instanceof ApiException);
        assertEquals("371", ex.getCause().getMessage());
        throw ex;
    }
}

From source file:io.ventu.rpc.amqp.AmqpInvokerimplTest.java

@Test
public void responseReceiver_handleDelivery_onEncodingException_MapWithNoError_exception()
        throws EncodingException, IOException, InterruptedException, ExecutionException, TimeoutException {
    ResponseReceiverImpl receiver = new ResponseReceiverImpl(serializer, new Validator() {
    }, 1, TimeUnit.MINUTES);/*from   w  w  w. ja v  a2  s.  co m*/

    String correlationId = "987654321";
    CompletableFuture<Res> answer = receiver.put(correlationId, Res.class);
    assertFalse(answer.isDone());
    assertFalse(answer.isCompletedExceptionally());

    Map<String, Object> res = Maps.newHashMap();
    res.put("value", "notAnInt");

    receiver.handleDelivery(correlationId, serializer.encode(res));
    assertTrue(answer.isDone());
    assertTrue(answer.isCompletedExceptionally());

    exception.expect(ExecutionException.class);
    try {
        answer.get();
    } catch (ExecutionException ex) {
        assertTrue(ex.getCause() instanceof EncodingException);
        assertEquals("failed to decode JSON", ex.getCause().getMessage());
        throw ex;
    }
}

From source file:io.ventu.rpc.amqp.AmqpInvokerimplTest.java

@Test
public void responseReceiver_handleDelivery_onAPIException_exception()
        throws EncodingException, IOException, InterruptedException, ExecutionException {
    Validator validator = new Validator() {
        @Override/*from   w ww  .j  a  v a  2 s.c o  m*/
        public <T> void validate(T value) throws ApiException, IllegalArgumentException {
            throw new ApiException("boom");
        }
    };
    ResponseReceiverImpl receiver = new ResponseReceiverImpl(serializer, validator, 1, TimeUnit.MINUTES);

    String correlationId = "987654321";
    CompletableFuture<Res> answer = receiver.put(correlationId, Res.class);
    assertFalse(answer.isDone());
    assertFalse(answer.isCompletedExceptionally());

    receiver.handleDelivery(correlationId, serializer.encode(Maps.newHashMap()));
    assertTrue(answer.isDone());
    assertTrue(answer.isCompletedExceptionally());

    exception.expect(ExecutionException.class);
    try {
        answer.get();
    } catch (ExecutionException ex) {
        assertTrue(ex.getCause() instanceof ApiException);
        assertEquals("boom", ex.getCause().getMessage());
        throw ex;
    }
}

From source file:io.ventu.rpc.amqp.AmqpInvokerimplTest.java

@Test
public void responseReceiver_handleDelivery_responseValidated()
        throws EncodingException, IOException, InterruptedException, ExecutionException, TimeoutException {
    final List<Boolean> invocations = Lists.newArrayList();
    Validator validator = new Validator() {
        @Override// w  ww.  j  a  v  a 2 s. c  o m
        public <T> void validate(T value) throws ApiException, IllegalArgumentException {
            invocations.add(Boolean.TRUE);
            assertTrue(value instanceof Res);
            assertEquals(25, ((Res) value).value);
        }
    };
    ResponseReceiverImpl receiver = new ResponseReceiverImpl(serializer, validator, 1, TimeUnit.MINUTES);

    Res res = new Res();
    res.value = 25;

    String correlationId = "987654321";
    CompletableFuture<Res> answer = receiver.put(correlationId, Res.class);
    assertFalse(answer.isDone());
    assertFalse(answer.isCompletedExceptionally());

    receiver.handleDelivery(correlationId, serializer.encode(res));
    assertEquals(1, invocations.size());
    assertTrue(answer.isDone());
    assertFalse(answer.isCompletedExceptionally());

    Res actual = answer.get(500, TimeUnit.MILLISECONDS);
    assertEquals(25, actual.value);
}

From source file:com.ikanow.aleph2.data_import_manager.analytics.actors.DataBucketAnalyticsChangeActor.java

/** Wraps the communications with the tech module so that calls to completeExceptionally are handled
 * @param bucket//  ww  w. jav a 2 s .  co m
 * @param m
 * @param source
 * @param context
 * @param err_or_tech_module - the tech module (is ignored unless the user code got called ie implies err_or_tech_module.isRight)
 * @param return_value - either the user return value or a wrap of the exception
 * @return
 */
public static final CompletableFuture<BucketActionReplyMessage> handleTechnologyErrors(
        final DataBucketBean bucket, final BucketActionMessage m, final String source,
        final Validation<BasicMessageBean, Tuple2<IAnalyticsTechnologyModule, ClassLoader>> err_or_tech_module,
        final CompletableFuture<BucketActionReplyMessage> return_value // "pipeline element"
) {
    if (return_value.isCompletedExceptionally()) { // Harvest Tech developer called completeExceptionally, ugh
        try {
            return_value.get(); // (causes an exception)
        } catch (Throwable t) { // e.getCause() is the exception we want
            // Note if we're here then err_or_tech_module must be "right"
            return CompletableFuture.completedFuture(new BucketActionHandlerMessage(source,
                    SharedErrorUtils.buildErrorMessage(source, m,
                            ErrorUtils.getLongForm(AnalyticsErrorUtils.NO_TECHNOLOGY_NAME_OR_ID, t.getCause(),
                                    m.bucket().full_name(), err_or_tech_module.success()._1().getClass()))));
        }
    }
    //(else fall through to...)
    return return_value;
}

From source file:io.ventu.rpc.amqp.AmqpInvokerimplTest.java

@Test
public void invoke_onOkRequest_onIOException_futureCompletesExceptionally()
        throws IOException, TimeoutException, ExecutionException, InterruptedException {
    String instanceId = "123456789";

    Req req = new Req();

    Channel channel = mock(Channel.class);
    doAnswer(invocation -> {/*from  ww  w . jav  a2s .  com*/
        throw new IOException("boom");
    }).when(channel).basicPublish(anyString(), any(), any(), any());

    CompletableFuture<Res> answer = new CompletableFuture<>();
    ResponseReceiver receiver = mock(ResponseReceiver.class);
    doReturn(answer).when(receiver).put(anyString(), any());

    ChannelProvider channelProvider = mock(ChannelProvider.class);
    doReturn(channel).when(channelProvider).provide(instanceId, receiver);
    doReturn(DEFAULT_RPC_EXCHANGE).when(channelProvider).rpcExchange();

    RemoteInvoker invoker = new AmqpInvokerImpl(instanceId, channelProvider, receiver);
    CompletableFuture<Res> actual = invoker.invoke(req, Res.class);

    assertSame(answer, actual);
    assertTrue(actual.isDone());
    assertTrue(actual.isCompletedExceptionally());

    exception.expect(ExecutionException.class);
    try {
        actual.get();
    } catch (ExecutionException ex) {
        assertTrue(ex.getCause() instanceof IOException);
        throw ex;
    }
}

From source file:io.ventu.rpc.amqp.AmqpInvokerimplTest.java

@Test
public void invoke_onOkRequest_encodedAndCorrectlyPublishedToAMQP()
        throws IOException, TimeoutException, ExecutionException, InterruptedException {
    String instanceId = "123456789";

    Req req = new Req();

    Channel channel = mock(Channel.class);

    CompletableFuture<Res> answer = new CompletableFuture<>();
    ResponseReceiver receiver = mock(ResponseReceiver.class);
    doReturn(answer).when(receiver).put(anyString(), any());

    ChannelProvider channelProvider = mock(ChannelProvider.class);
    doReturn(channel).when(channelProvider).provide(instanceId, receiver);
    doReturn(DEFAULT_RPC_EXCHANGE).when(channelProvider).rpcExchange();

    RemoteInvoker invoker = new AmqpInvokerImpl(instanceId, channelProvider, receiver);
    CompletableFuture<Res> actual = invoker.invoke(req, Res.class);

    assertSame(answer, actual);//from   ww  w .j  a  v a2 s. co m
    assertFalse(actual.isDone());
    assertFalse(actual.isCompletedExceptionally());

    verify(channelProvider).provide(instanceId, receiver);
    verify(channelProvider).rpcExchange();
    verifyNoMoreInteractions(channelProvider);

    verify(channel).basicPublish(anyString(), any(), any(), any());
    verifyNoMoreInteractions(channel);

    verify(receiver).put(anyString(), any());
    verifyNoMoreInteractions(receiver);
}

From source file:io.ventu.rpc.amqp.AmqpInvokerimplTest.java

@Test
public void invoke_onNokRequest_onIllegalArgEx_futureCompletesExceptionally()
        throws IOException, TimeoutException, ExecutionException, InterruptedException {
    String instanceId = "123456789";

    Req req = new Req();

    Channel channel = mock(Channel.class);

    CompletableFuture<Res> answer = new CompletableFuture<>();
    ResponseReceiver receiver = mock(ResponseReceiver.class);
    doReturn(answer).when(receiver).put(anyString(), any());

    ChannelProvider channelProvider = mock(ChannelProvider.class);
    doReturn(channel).when(channelProvider).provide(instanceId, receiver);

    ObjectMapper mapper = mock(ObjectMapper.class);
    doThrow(IllegalArgumentException.class).when(mapper).writeValueAsBytes(any());

    RemoteInvoker invoker = new AmqpInvokerImpl(instanceId, channelProvider, receiver,
            new DefaultRequestRouter(), new UidGenerator() {
            }, new DefaultSerializer(mapper), Maps.newHashMap());
    CompletableFuture<Res> actual = invoker.invoke(req, Res.class);

    assertSame(answer, actual);/*ww  w .jav  a 2s  .  c o m*/
    assertTrue(actual.isDone());
    assertTrue(actual.isCompletedExceptionally());

    exception.expect(ExecutionException.class);
    try {
        actual.get();
    } catch (ExecutionException ex) {
        assertTrue(ex.getCause() instanceof IllegalArgumentException);
        throw ex;
    }
}