Example usage for java.util.concurrent CompletableFuture isDone

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

Introduction

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

Prototype

public boolean isDone() 

Source Link

Document

Returns true if completed in any fashion: normally, exceptionally, or via cancellation.

Usage

From source file:org.apache.tinkerpop.gremlin.driver.ResultQueueTest.java

@Test
public void shouldHandleBulkSetSideEffects() throws Exception {
    final CompletableFuture<List<Result>> o = resultQueue.await(1);
    assertThat(o.isDone(), is(false));

    resultQueue.addSideEffect(Tokens.VAL_AGGREGATE_TO_BULKSET, new DefaultRemoteTraverser<>("brian", 2));
    assertThat(o.isDone(), is(false));/*from  w  w  w . jav  a 2 s . c o m*/

    resultQueue.addSideEffect(Tokens.VAL_AGGREGATE_TO_BULKSET, new DefaultRemoteTraverser<>("brian", 2));
    assertThat(o.isDone(), is(false));

    resultQueue.addSideEffect(Tokens.VAL_AGGREGATE_TO_BULKSET, new DefaultRemoteTraverser<>("belinda", 6));
    assertThat(o.isDone(), is(false));

    resultQueue.markComplete();

    assertThat(o.isDone(), is(true));
    final BulkSet<String> bulkSet = o.get().get(0).get(BulkSet.class);
    assertEquals(4, bulkSet.get("brian"));
    assertEquals(6, bulkSet.get("belinda"));
}

From source file:org.apache.tinkerpop.gremlin.driver.ResultQueueTest.java

@Test
public void shouldHandleListSideEffects() throws Exception {
    final CompletableFuture<List<Result>> o = resultQueue.await(1);
    assertThat(o.isDone(), is(false));

    resultQueue.addSideEffect(Tokens.VAL_AGGREGATE_TO_LIST, "stephen");
    assertThat(o.isDone(), is(false));//  ww w . ja v  a2  s .  c  o  m

    resultQueue.addSideEffect(Tokens.VAL_AGGREGATE_TO_LIST, "daniel");
    assertThat(o.isDone(), is(false));

    resultQueue.addSideEffect(Tokens.VAL_AGGREGATE_TO_LIST, "dave");
    assertThat(o.isDone(), is(false));

    resultQueue.markComplete();

    assertThat(o.isDone(), is(true));
    final List<String> list = o.get().get(0).get(ArrayList.class);
    assertEquals("stephen", list.get(0));
    assertEquals("daniel", list.get(1));
    assertEquals("dave", list.get(2));
}

From source file:org.apache.tinkerpop.gremlin.driver.ResultQueueTest.java

@Test
public void shouldHandleSetSideEffects() throws Exception {
    final CompletableFuture<List<Result>> o = resultQueue.await(1);
    assertThat(o.isDone(), is(false));

    resultQueue.addSideEffect(Tokens.VAL_AGGREGATE_TO_SET, "stephen");
    assertThat(o.isDone(), is(false));//from  w w w  .jav a 2 s .co  m

    resultQueue.addSideEffect(Tokens.VAL_AGGREGATE_TO_SET, "daniel");
    assertThat(o.isDone(), is(false));

    resultQueue.addSideEffect(Tokens.VAL_AGGREGATE_TO_SET, "dave");
    assertThat(o.isDone(), is(false));

    resultQueue.markComplete();

    assertThat(o.isDone(), is(true));
    final Set<String> set = o.get().get(0).get(HashSet.class);
    assertThat(set.contains("stephen"), is(true));
    assertThat(set.contains("daniel"), is(true));
    assertThat(set.contains("dave"), is(true));
}

From source file:org.apache.tinkerpop.gremlin.driver.ResultQueueTest.java

@Test
public void shouldHandleMapSideEffects() throws Exception {
    final CompletableFuture<List<Result>> o = resultQueue.await(1);
    assertThat(o.isDone(), is(false));

    final Map<String, String> m = new HashMap<>();
    m.put("s", "stephen");
    m.put("m", "marko");
    m.put("d", "daniel");

    m.entrySet().forEach(e -> {/* w w  w  .j  a v  a  2  s . com*/
        resultQueue.addSideEffect(Tokens.VAL_AGGREGATE_TO_MAP, e);
        assertThat(o.isDone(), is(false));
    });

    resultQueue.markComplete();

    assertThat(o.isDone(), is(true));
    final Map<String, String> list = o.get().get(0).get(HashMap.class);
    assertEquals("stephen", list.get("s"));
    assertEquals("daniel", list.get("d"));
    assertEquals("marko", list.get("m"));
}

From source file:org.apache.tinkerpop.gremlin.driver.ResultQueueTest.java

@Test
public void shouldHandleNotAggregateSideEffects() throws Exception {
    final CompletableFuture<List<Result>> o = resultQueue.await(1);
    assertThat(o.isDone(), is(false));

    final Map<String, String> m = new HashMap<>();
    m.put("s", "stephen");
    m.put("m", "marko");
    m.put("d", "daniel");

    resultQueue.addSideEffect(Tokens.VAL_AGGREGATE_TO_NONE, m);

    resultQueue.markComplete();/*w  w  w. jav  a 2  s  .c om*/

    assertThat(o.isDone(), is(true));
    final Map<String, String> list = o.get().get(0).get(HashMap.class);
    assertEquals("stephen", list.get("s"));
    assertEquals("daniel", list.get("d"));
    assertEquals("marko", list.get("m"));
}

From source file:org.apache.tinkerpop.gremlin.driver.simple.AbstractClient.java

@Override
public CompletableFuture<List<ResponseMessage>> submitAsync(final RequestMessage requestMessage)
        throws Exception {
    final List<ResponseMessage> results = new ArrayList<>();
    final CompletableFuture<List<ResponseMessage>> f = new CompletableFuture<>();
    callbackResponseHandler.callback = response -> {
        if (f.isDone())
            throw new RuntimeException(
                    "A terminating message was already encountered - no more messages should have been received");

        results.add(response);//w  w w.  ja va 2 s  .c  o m

        // check if the current message is terminating - if it is then we can mark complete
        if (!response.getStatus().getCode().equals(ResponseStatusCode.PARTIAL_CONTENT)) {
            f.complete(results);
        }
    };

    writeAndFlush(requestMessage);

    return f;
}

From source file:org.apache.tinkerpop.gremlin.server.GremlinDriverIntegrateTest.java

@Test
public void shouldProcessRequestsOutOfOrder() throws Exception {
    final Cluster cluster = Cluster.open();
    final Client client = cluster.connect();

    final ResultSet rsFive = client.submit("Thread.sleep(5000);'five'");
    final ResultSet rsZero = client.submit("'zero'");

    final CompletableFuture<List<Result>> futureFive = rsFive.all();
    final CompletableFuture<List<Result>> futureZero = rsZero.all();

    final long start = System.nanoTime();
    assertFalse(futureFive.isDone());
    assertEquals("zero", futureZero.get().get(0).getString());

    logger.info("Eval of 'zero' complete: " + TimeUtil.millisSince(start));

    assertFalse(futureFive.isDone());/*from  w  ww. ja  va  2 s . c o m*/
    assertEquals("five", futureFive.get(10, TimeUnit.SECONDS).get(0).getString());

    logger.info("Eval of 'five' complete: " + TimeUtil.millisSince(start));
}

From source file:org.apache.tinkerpop.gremlin.server.GremlinDriverIntegrateTest.java

@Test
public void shouldProcessSessionRequestsInOrder() throws Exception {
    final Cluster cluster = Cluster.open();
    final Client client = cluster.connect(name.getMethodName());

    final ResultSet rsFive = client.submit("Thread.sleep(5000);'five'");
    final ResultSet rsZero = client.submit("'zero'");

    final CompletableFuture<List<Result>> futureFive = rsFive.all();
    final CompletableFuture<List<Result>> futureZero = rsZero.all();

    final AtomicBoolean hit = new AtomicBoolean(false);
    while (!futureFive.isDone()) {
        // futureZero can't finish before futureFive - racy business here?
        assertThat(futureZero.isDone(), is(false));
        hit.set(true);/*from w w  w  .j a v a  2 s .c o m*/
    }

    // should have entered the loop at least once and thus proven that futureZero didn't return ahead of
    // futureFive
    assertThat(hit.get(), is(true));

    assertEquals("zero", futureZero.get().get(0).getString());
    assertEquals("five", futureFive.get(10, TimeUnit.SECONDS).get(0).getString());
}