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 invoke_onNokRequest_onEncodingEx_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(JsonProcessingException.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);//from  ww  w .  ja v a2 s .c o m
    assertTrue(actual.isDone());
    assertTrue(actual.isCompletedExceptionally());

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

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

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

    Channel channel = mock(Channel.class);
    doThrow(new IOException("boom")).when(channel).close();
    ResponseReceiver receiver = mock(ResponseReceiver.class);
    ChannelProvider channelProvider = mock(ChannelProvider.class);
    doReturn(channel).when(channelProvider).provide(instanceId, receiver);

    RemoteInvoker invoker = new AmqpInvokerImpl(instanceId, channelProvider, receiver);

    CompletableFuture<Void> actual = invoker.close();

    verify(channel).close();//from ww  w . j a v a 2 s. c o  m
    verifyNoMoreInteractions(channel);

    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 closingInvoker_success()
        throws IOException, TimeoutException, ExecutionException, InterruptedException {
    String instanceId = "123456789";

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

    RemoteInvoker invoker = new AmqpInvokerImpl(instanceId, channelProvider, receiver);

    CompletableFuture<Void> actual = invoker.close();

    verify(channel).close();/*from w  w w.j av a 2  s .  c o  m*/
    verifyNoMoreInteractions(channel);

    actual.get();

    assertTrue(actual.isDone());
    assertFalse(actual.isCompletedExceptionally());
}

From source file:com.yahoo.pulsar.broker.service.BrokerService.java

public Topic getTopicReference(String topic) throws Exception {
    CompletableFuture<Topic> future = topics.get(topic);
    if (future != null && future.isDone() && !future.isCompletedExceptionally()) {
        return future.get();
    } else {//from ww  w.  j  a  va 2  s  .co m
        return null;
    }
}

From source file:com.eventsourcing.repository.RepositoryTest.java

@Test
@SneakyThrows/*w ww.  j  ava 2 s .  c  om*/
public void streamExceptionIndexing() {
    IndexedCollection<EntityHandle<TestEvent>> coll = indexEngine.getIndexedCollection(TestEvent.class);
    coll.clear();
    UUID eventUUID = UUID.randomUUID();
    StreamExceptionCommand command = StreamExceptionCommand.builder().eventUUID(eventUUID).build();
    CompletableFuture<Void> future = repository.publish(command);
    while (!future.isDone()) {
        Thread.sleep(10);
    } // to avoid throwing an exception
    assertTrue(future.isCompletedExceptionally());
    // result() was not called
    assertFalse(command.isResultCalled());
    try (ResultSet<EntityHandle<CommandTerminatedExceptionally>> resultSet = repository.query(
            CommandTerminatedExceptionally.class,
            and(all(CommandTerminatedExceptionally.class),
                    existsIn(indexEngine.getIndexedCollection(EventCausalityEstablished.class),
                            CommandTerminatedExceptionally.ID, EventCausalityEstablished.EVENT)))) {
        assertEquals(resultSet.size(), 1);
    }
    assertTrue(journal.get(command.uuid()).isPresent());
    assertFalse(journal.get(eventUUID).isPresent());
    assertTrue(repository.query(TestEvent.class, equal(TestEvent.ATTR, "test")).isEmpty());
}

From source file:com.yahoo.pulsar.broker.service.ServerCnx.java

@Override
protected void handleAck(CommandAck ack) {
    checkArgument(state == State.Connected);
    CompletableFuture<Consumer> consumerFuture = consumers.get(ack.getConsumerId());

    if (consumerFuture != null && consumerFuture.isDone() && !consumerFuture.isCompletedExceptionally()) {
        consumerFuture.getNow(null).messageAcked(ack);
    }/*from  www.  jav a2s  .c o  m*/
}

From source file:com.yahoo.pulsar.broker.service.ServerCnx.java

@Override
protected void handleFlow(CommandFlow flow) {
    checkArgument(state == State.Connected);
    if (log.isDebugEnabled()) {
        log.debug("[{}] Received flow from consumer {} permits: {}", remoteAddress, flow.getConsumerId(),
                flow.getMessagePermits());
    }/*w w  w  .  ja  va2s.c  o  m*/

    CompletableFuture<Consumer> consumerFuture = consumers.get(flow.getConsumerId());

    if (consumerFuture != null && consumerFuture.isDone() && !consumerFuture.isCompletedExceptionally()) {
        consumerFuture.getNow(null).flowPermits(flow.getMessagePermits());
    }
}

From source file:com.yahoo.pulsar.broker.service.ServerCnx.java

@Override
protected void handleUnsubscribe(CommandUnsubscribe unsubscribe) {
    checkArgument(state == State.Connected);

    CompletableFuture<Consumer> consumerFuture = consumers.get(unsubscribe.getConsumerId());

    if (consumerFuture != null && consumerFuture.isDone() && !consumerFuture.isCompletedExceptionally()) {
        consumerFuture.getNow(null).doUnsubscribe(unsubscribe.getRequestId());
    } else {//  w  ww . jav a 2s.c  o  m
        ctx.writeAndFlush(
                Commands.newError(unsubscribe.getRequestId(), ServerError.MetadataError, "Consumer not found"));
    }
}

From source file:com.yahoo.pulsar.broker.service.ServerCnx.java

@Override
protected void handleSend(CommandSend send, ByteBuf headersAndPayload) {
    checkArgument(state == State.Connected);

    CompletableFuture<Producer> producerFuture = producers.get(send.getProducerId());

    if (producerFuture == null || !producerFuture.isDone() || producerFuture.isCompletedExceptionally()) {
        log.warn("[{}] Producer had already been closed: {}", remoteAddress, send.getProducerId());
        return;/*from  w  w w.  j a  v  a 2s.  c  o m*/
    }

    Producer producer = producerFuture.getNow(null);
    if (log.isDebugEnabled()) {
        printSendCommandDebug(send, headersAndPayload);
    }

    startSendOperation();

    // Persist the message
    producer.publishMessage(send.getProducerId(), send.getSequenceId(), headersAndPayload,
            send.getNumMessages());
}

From source file:com.yahoo.pulsar.broker.service.ServerCnx.java

@Override
protected void handleRedeliverUnacknowledged(CommandRedeliverUnacknowledgedMessages redeliver) {
    checkArgument(state == State.Connected);
    if (log.isDebugEnabled()) {
        log.debug("[{}] Received Resend Command from consumer {} ", remoteAddress, redeliver.getConsumerId());
    }//  w w  w .  j ava  2 s . c  o m

    CompletableFuture<Consumer> consumerFuture = consumers.get(redeliver.getConsumerId());

    if (consumerFuture != null && consumerFuture.isDone() && !consumerFuture.isCompletedExceptionally()) {
        Consumer consumer = consumerFuture.getNow(null);
        if (redeliver.getMessageIdsCount() > 0 && consumer.subType() == SubType.Shared) {
            consumer.redeliverUnacknowledgedMessages(redeliver.getMessageIdsList());
        } else {
            consumer.redeliverUnacknowledgedMessages();
        }
    }
}