org.apache.tinkerpop.gremlin.driver.ResultSet Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.driver.ResultSet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldWaitForAllResultsToArrive() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect();

    final AtomicInteger checked = new AtomicInteger(0);
    final ResultSet results = client.submit("[1,2,3,4,5,6,7,8,9]");
    while (!results.allItemsAvailable()) {
        assertTrue(results.getAvailableItemCount() < 10);
        checked.incrementAndGet();
        Thread.sleep(100);
    }

    assertTrue(checked.get() > 0);
    assertEquals(9, results.getAvailableItemCount());
    cluster.close();
}
 
Example #2
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSerializeToStringWhenRequestedGryoV3() throws Exception {
    final Map<String, Object> m = new HashMap<>();
    m.put("serializeResultToString", true);
    final GryoMessageSerializerV3d0 serializer = new GryoMessageSerializerV3d0();
    serializer.configure(m, null);

    final Cluster cluster = TestClientFactory.build().serializer(serializer).create();
    final Client client = cluster.connect();

    final ResultSet resultSet = client.submit("TinkerFactory.createClassic()");
    final List<Result> results = resultSet.all().join();
    assertEquals(1, results.size());
    assertEquals("tinkergraph[vertices:6 edges:6]", results.get(0).getString());

    cluster.close();
}
 
Example #3
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSerializeToStringWhenRequestedGryoV1() throws Exception {
    final Map<String, Object> m = new HashMap<>();
    m.put("serializeResultToString", true);
    final GryoMessageSerializerV1d0 serializer = new GryoMessageSerializerV1d0();
    serializer.configure(m, null);

    final Cluster cluster = TestClientFactory.build().serializer(serializer).create();
    final Client client = cluster.connect();

    final ResultSet resultSet = client.submit("TinkerFactory.createClassic()");
    final List<Result> results = resultSet.all().join();
    assertEquals(1, results.size());
    assertEquals("tinkergraph[vertices:6 edges:6]", results.get(0).getString());

    cluster.close();
}
 
Example #4
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldExecuteScriptInSession() throws Exception {
    final Cluster cluster = TestClientFactory.build().create();
    final Client client = cluster.connect(name.getMethodName());

    final ResultSet results1 = client.submit("x = [1,2,3,4,5,6,7,8,9]");
    assertEquals(9, results1.all().get().size());

    final ResultSet results2 = client.submit("x[0]+1");
    assertEquals(2, results2.all().get().get(0).getInt());

    final ResultSet results3 = client.submit("x[1]+2");
    assertEquals(4, results3.all().get().get(0).getInt());

    cluster.close();
}
 
Example #5
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldFailWithBadServerSideSerialization() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect();

    final ResultSet results = client.submit("TinkerGraph.open().variables()");

    try {
        results.all().join();
        fail();
    } catch (Exception ex) {
        final Throwable inner = ExceptionUtils.getRootCause(ex);
        assertTrue(inner instanceof ResponseException);
        assertEquals(ResponseStatusCode.SERVER_ERROR_SERIALIZATION, ((ResponseException) inner).getResponseStatusCode());
    }

    // should not die completely just because we had a bad serialization error.  that kind of stuff happens
    // from time to time, especially in the console if you're just exploring.
    assertEquals(2, client.submit("1+1").all().get().get(0).getInt());

    cluster.close();
}
 
Example #6
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldCloseSession() throws Exception {
    final Cluster cluster = TestClientFactory.build().create();
    final Client client = cluster.connect(name.getMethodName());

    final ResultSet results1 = client.submit("x = [1,2,3,4,5,6,7,8,9]");
    assertEquals(9, results1.all().get().size());
    final ResultSet results2 = client.submit("x[0]+1");
    assertEquals(2, results2.all().get().get(0).getInt());

    client.close();

    try {
        client.submit("x[0]+1").all().get();
        fail("Should have thrown an exception because the connection is closed");
    } catch (Exception ex) {
        final Throwable root = ExceptionUtils.getRootCause(ex);
        assertThat(root, instanceOf(IllegalStateException.class));
    } finally {
        cluster.close();
    }
}
 
Example #7
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldIterate() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect();

    final ResultSet results = client.submit("[1,2,3,4,5,6,7,8,9]");
    final Iterator<Result> itty = results.iterator();
    final AtomicInteger counter = new AtomicInteger(0);
    while (itty.hasNext()) {
        counter.incrementAndGet();
        assertEquals(counter.get(), itty.next().getInt());
    }

    assertEquals(9, counter.get());
    assertThat(results.allItemsAvailable(), is(true));

    // can't stream it again
    assertThat(results.iterator().hasNext(), is(false));

    cluster.close();
}
 
Example #8
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldStream() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect();

    final ResultSet results = client.submit("[1,2,3,4,5,6,7,8,9]");
    final AtomicInteger counter = new AtomicInteger(0);
    results.stream().map(i -> i.get(Integer.class) * 2).forEach(i -> assertEquals(counter.incrementAndGet() * 2, Integer.parseInt(i.toString())));
    assertEquals(9, counter.get());
    assertThat(results.allItemsAvailable(), is(true));

    // cant stream it again
    assertThat(results.stream().iterator().hasNext(), is(false));

    cluster.close();
}
 
Example #9
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldProcessRequestsOutOfOrder() throws Exception {
    final Cluster cluster = TestClientFactory.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());
    assertEquals("five", futureFive.get(10, TimeUnit.SECONDS).get(0).getString());

    logger.info("Eval of 'five' complete: " + TimeUtil.millisSince(start));
}
 
Example #10
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldExecuteScriptsInMultipleSession() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client1 = cluster.connect(name.getMethodName() + "1");
    final Client client2 = cluster.connect(name.getMethodName() + "2");
    final Client client3 = cluster.connect(name.getMethodName() + "3");

    final ResultSet results11 = client1.submit("x = 1");
    final ResultSet results21 = client2.submit("x = 2");
    final ResultSet results31 = client3.submit("x = 3");
    assertEquals(1, results11.all().get().get(0).getInt());
    assertEquals(2, results21.all().get().get(0).getInt());
    assertEquals(3, results31.all().get().get(0).getInt());

    final ResultSet results12 = client1.submit("x + 100");
    final ResultSet results22 = client2.submit("x * 2");
    final ResultSet results32 = client3.submit("x * 10");
    assertEquals(101, results12.all().get().get(0).getInt());
    assertEquals(4, results22.all().get().get(0).getInt());
    assertEquals(30, results32.all().get().get(0).getInt());

    cluster.close();
}
 
Example #11
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldFailWithBadClientSideSerialization() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect();

    final ResultSet results = client.submit("java.awt.Color.RED");

    try {
        results.all().join();
        fail("Should have thrown exception over bad serialization");
    } catch (Exception ex) {
        final Throwable inner = ExceptionUtils.getRootCause(ex);
        assertThat(inner, instanceOf(ResponseException.class));
        assertEquals(ResponseStatusCode.SERVER_ERROR_SERIALIZATION, ((ResponseException) inner).getResponseStatusCode());
    }

    // should not die completely just because we had a bad serialization error.  that kind of stuff happens
    // from time to time, especially in the console if you're just exploring.
    assertEquals(2, client.submit("1+1").all().get().get(0).getInt());

    cluster.close();
}
 
Example #12
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldExecuteScriptInSessionWithBindingsSavedOnServerBetweenRequests() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect(name.getMethodName());

    final Map<String, Object> bindings1 = new HashMap<>();
    bindings1.put("a", 100);
    bindings1.put("b", 200);
    final ResultSet results1 = client.submit("x = a + b", bindings1);
    assertEquals(300, results1.one().getInt());

    final Map<String, Object> bindings2 = new HashMap<>();
    bindings2.put("b", 100);
    final ResultSet results2 = client.submit("x + b + a", bindings2);
    assertEquals(500, results2.one().getInt());

    final Map<String, Object> bindings3 = new HashMap<>();
    bindings3.put("x", 100);
    final ResultSet results3 = client.submit("x + b + a + 1", bindings3);
    assertEquals(301, results3.one().getInt());

    final Map<String, Object> bindings4 = new HashMap<>();
    final ResultSet results4 = client.submit("x + b + a + 1", bindings4);
    assertEquals(301, results4.one().getInt());

    cluster.close();
}
 
Example #13
Source File: GremlinResultSetIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHandleMapIteratedResult() throws Exception {
    final ResultSet results = client.submit("gmodern.V().groupCount().by(bothE().count())");
    final List<Result> resultList = results.all().get();
    final Map m = resultList.get(0).get(HashMap.class);
    assertEquals(2, m.size());
    assertEquals(3L, m.get(1L));
    assertEquals(3L, m.get(3L));
}
 
Example #14
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldExecuteScriptInSessionAssumingDefaultedImports() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect(name.getMethodName());

    final ResultSet results1 = client.submit("TinkerFactory.class.name");
    assertEquals(TinkerFactory.class.getName(), results1.all().get().get(0).getString());

    cluster.close();
}
 
Example #15
Source File: GremlinServerIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
@Test
public void shouldBlockRequestWhenTooBig() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect();

    try {
        final String fatty = IntStream.range(0, 1024).mapToObj(String::valueOf).collect(Collectors.joining());
        final CompletableFuture<ResultSet> result = client.submitAsync("'" + fatty + "';'test'");
        final ResultSet resultSet = result.get(10000, TimeUnit.MILLISECONDS);
        resultSet.all().get(10000, TimeUnit.MILLISECONDS);
        fail("Should throw an exception.");
    } catch (TimeoutException te) {
        // the request should not have timed-out - the connection should have been reset, but it seems that
        // timeout seems to occur as well on some systems (it's not clear why).  however, the nature of this
        // test is to ensure that the script isn't processed if it exceeds a certain size, so in this sense
        // it seems ok to pass in this case.
    } catch (Exception re) {
        final Throwable root = ExceptionUtils.getRootCause(re);

        // went with two possible error messages here as i think that there is some either non-deterministic
        // behavior around the error message or it's environmentally dependent (e.g. different jdk, versions, etc)
        assertThat(root.getMessage(), Matchers.anyOf(is("Connection to server is no longer active"), is("Connection reset by peer")));

        // validate that we can still send messages to the server
        assertEquals(2, client.submit("1+1").all().join().get(0).getInt());
    } finally {
        cluster.close();
    }
}
 
Example #16
Source File: GremlinResultSetIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHandleVertexResultFromTraversalBulked() throws Exception {
    final Graph graph = TinkerGraph.open();
    final GraphTraversalSource g = graph.traversal();
    final Client aliased = client.alias("gmodern");
    final ResultSet resultSetUnrolled = aliased.submit(g.V().both().barrier().both().barrier());
    final List<Result> results = resultSetUnrolled.all().get();

    assertThat(results.get(0).getObject(), CoreMatchers.instanceOf(Traverser.class));
    assertEquals(6, results.size());
}
 
Example #17
Source File: GremlinResultSetIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHandleTinkerGraphResult() throws Exception {
    final ResultSet results = client.submit("modern");
    final Graph graph = results.all().get().get(0).get(TinkerGraph.class);

    // test is "lossy for id" because TinkerGraph is configured by default to use the ANY id manager
    // and doesn't coerce to specific types - which is how it is on the server as well so we can expect
    // some id shiftiness
    IoTest.assertModernGraph(graph, true, true);
}
 
Example #18
Source File: DriverRemoteAcceptor.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
private List<Result> send(final String gremlin) throws SaslException {
    try {
        final RequestOptions.Builder options = RequestOptions.build();
        aliases.forEach(options::addAlias);
        if (timeout > NO_TIMEOUT)
            options.timeout(timeout);

        options.userAgent(USER_AGENT);

        final ResultSet rs = this.currentClient.submit(gremlin, options.create());
        final List<Result> results = rs.all().get();
        final Map<String, Object> statusAttributes = rs.statusAttributes().getNow(null);

        // Check for and print warnings
        if (null != statusAttributes && statusAttributes.containsKey(Tokens.STATUS_ATTRIBUTE_WARNINGS)) {
            final Object warningAttributeObject = statusAttributes.get(Tokens.STATUS_ATTRIBUTE_WARNINGS);
            if (warningAttributeObject instanceof List) {
                for (Object warningListItem : (List<?>)warningAttributeObject)
                    shellEnvironment.errPrintln(String.valueOf(warningListItem));
            } else {
                shellEnvironment.errPrintln(String.valueOf(warningAttributeObject));
            }
        }

        return results;
    } catch (Exception e) {
        // handle security error as-is and unwrapped
        final Optional<Throwable> throwable  = Stream.of(ExceptionUtils.getThrowables(e)).filter(t -> t instanceof SaslException).findFirst();
        if (throwable.isPresent())
            throw (SaslException) throwable.get();

        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example #19
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetOneThenSomeThenSomeMore() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect();

    final ResultSet results = client.submit("[1,2,3,4,5,6,7,8,9]");
    final Result one = results.one();
    final CompletableFuture<List<Result>> batch1 = results.some(4);
    final CompletableFuture<List<Result>> batch2 = results.some(5);
    final CompletableFuture<List<Result>> batchNothingLeft = results.some(5);

    assertEquals(1, one.getInt());

    assertEquals(4, batch1.get().size());
    assertEquals(2, batch1.get().get(0).getInt());
    assertEquals(3, batch1.get().get(1).getInt());
    assertEquals(4, batch1.get().get(2).getInt());
    assertEquals(5, batch1.get().get(3).getInt());

    assertEquals(4, batch2.get().size());
    assertEquals(6, batch2.get().get(0).getInt());
    assertEquals(7, batch2.get().get(1).getInt());
    assertEquals(8, batch2.get().get(2).getInt());
    assertEquals(9, batch2.get().get(3).getInt());

    assertEquals(0, batchNothingLeft.get().size());

    cluster.close();
}
 
Example #20
Source File: DriverRemoteTraversal.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public DriverRemoteTraversal(final ResultSet rs, final Client client, final boolean attach, final Optional<Configuration> conf) {
    // attaching is really just for testing purposes. it doesn't make sense in any real-world scenario as it would
    // require that the client have access to the Graph instance that produced the result. tests need that
    // attachment process to properly execute in full hence this little hack.
    if (attach) {
        if (!conf.isPresent()) throw new IllegalStateException("Traverser can't be reattached for testing");
        final Graph graph = ((Supplier<Graph>) conf.get().getProperty(GREMLIN_REMOTE + "attachment")).get();
        this.traversers = new AttachingTraverserIterator<>(rs.iterator(), graph);
    } else {
        this.traversers = new TraverserIterator<>(rs.iterator());
    }
}
 
Example #21
Source File: GremlinServerSessionIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCloseSessionOnceOnRequest() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect(name.getMethodName());

    final ResultSet results1 = client.submit("x = [1,2,3,4,5,6,7,8,9]");
    final AtomicInteger counter = new AtomicInteger(0);
    results1.stream().map(i -> i.get(Integer.class) * 2).forEach(i -> assertEquals(counter.incrementAndGet() * 2, Integer.parseInt(i.toString())));

    final ResultSet results2 = client.submit("x[0]+1");
    assertEquals(2, results2.all().get().get(0).getInt());

    // explicitly close the session
    client.close();

    // wait past automatic session expiration
    Thread.sleep(3500);

    try {
        // the original session should be dead so this call will open a new session with the same name but fail
        // because the state is now gone - x is an invalid property
        client.submit("x[1]+2").all().get();
        fail("Session should be dead");
    } catch (Exception ex) {
        final Throwable root = ExceptionUtils.getRootCause(ex);
        assertThat(root, instanceOf(IllegalStateException.class));
    } finally {
        cluster.close();
    }

    assertEquals(1, recordingAppender.getMessages().stream()
            .filter(msg -> msg.equals("INFO - Session shouldCloseSessionOnceOnRequest closed\n")).count());
}
 
Example #22
Source File: GremlinServerSessionIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHaveTheSessionTimeout() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect(name.getMethodName());

    final ResultSet results1 = client.submit("x = [1,2,3,4,5,6,7,8,9]");
    final AtomicInteger counter = new AtomicInteger(0);
    results1.stream().map(i -> i.get(Integer.class) * 2).forEach(i -> assertEquals(counter.incrementAndGet() * 2, Integer.parseInt(i.toString())));

    final ResultSet results2 = client.submit("x[0]+1");
    assertEquals(2, results2.all().get().get(0).getInt());

    // session times out in 3 seconds
    Thread.sleep(3500);

    try {
        // the original session should be dead so this call will open a new session with the same name but fail
        // because the state is now gone - x is an invalid property
        client.submit("x[1]+2").all().get();
        fail("Session should be dead");
    } catch (Exception ex) {
        final Throwable cause = ExceptionUtils.getCause(ex);
        assertThat(cause, instanceOf(ResponseException.class));
        assertEquals(ResponseStatusCode.SERVER_ERROR_EVALUATION, ((ResponseException) cause).getResponseStatusCode());

        // validate that we can still send messages to the server
        assertEquals(2, client.submit("1+1").all().join().get(0).getInt());
    } finally {
        cluster.close();
    }

    // there will be one for the timeout and a second for closing the cluster
    assertEquals(2, recordingAppender.getMessages().stream()
            .filter(msg -> msg.equals("INFO - Session shouldHaveTheSessionTimeout closed\n")).count());
}
 
Example #23
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHandleResultsOfAllSizes() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect();

    final String script = "g.V().drop().iterate();\n" +
            "\n" +
            "List ids = new ArrayList();\n" +
            "\n" +
            "int ii = 0;\n" +
            "Vertex v = graph.addVertex();\n" +
            "v.property(\"ii\", ii);\n" +
            "v.property(\"sin\", Math.sin(ii));\n" +
            "ids.add(v.id());\n" +
            "\n" +
            "Random rand = new Random();\n" +
            "for (; ii < size; ii++) {\n" +
            "    v = graph.addVertex();\n" +
            "    v.property(\"ii\", ii);\n" +
            "    v.property(\"sin\", Math.sin(ii/5.0));\n" +
            "    Vertex u = graph.vertices(ids.get(rand.nextInt(ids.size()))).next();\n" +
            "    v.addEdge(\"linked\", u);\n" +
            "    ids.add(u.id());\n" +
            "    ids.add(v.id());\n" +
            "}\n" +
            "g.V()";

    final List<Integer> sizes = Arrays.asList(1, 10, 20, 50, 75, 100, 250, 500, 750, 1000, 5000, 10000);
    for (Integer size : sizes) {
        final Map<String, Object> params = new HashMap<>();
        params.put("size", size - 1);
        final ResultSet results = client.submit(script, params);

        assertEquals(size.intValue(), results.all().get().size());
    }

    cluster.close();
}
 
Example #24
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFailWithScriptExecutionException() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect();

    final ResultSet results = client.submit("1/0");

    try {
        results.all().join();
        fail("Should have thrown exception over bad serialization");
    } catch (Exception ex) {
        final Throwable inner = ExceptionUtils.getRootCause(ex);
        assertTrue(inner instanceof ResponseException);
        assertThat(inner.getMessage(), endsWith("Division by zero"));

        final ResponseException rex = (ResponseException) inner;
        assertEquals("java.lang.ArithmeticException", rex.getRemoteExceptionHierarchy().get().get(0));
        assertEquals(1, rex.getRemoteExceptionHierarchy().get().size());
        assertThat(rex.getRemoteStackTrace().get(), startsWith("java.lang.ArithmeticException: Division by zero\n\tat java.math.BigDecimal.divide(BigDecimal.java"));
    }

    // should not die completely just because we had a bad serialization error.  that kind of stuff happens
    // from time to time, especially in the console if you're just exploring.
    assertEquals(2, client.submit("1+1").all().get().get(0).getInt());

    cluster.close();
}
 
Example #25
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldProcessSessionRequestsInOrder() throws Exception {
    final Cluster cluster = TestClientFactory.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 CountDownLatch latch = new CountDownLatch(2);
    final List<String> order = new ArrayList<>();
    final ExecutorService executor = Executors.newSingleThreadExecutor();

    futureFive.thenAcceptAsync(r -> {
        order.add(r.get(0).getString());
        latch.countDown();
    }, executor);

    futureZero.thenAcceptAsync(r -> {
        order.add(r.get(0).getString());
        latch.countDown();
    }, executor);

    // wait for both results
    latch.await(30000, TimeUnit.MILLISECONDS);

    // should be two results
    assertEquals(2, order.size());

    // ensure that "five" is first then "zero"
    assertThat(order, contains("five", "zero"));
}
 
Example #26
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetSomeThenSomeMore() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect();

    final ResultSet results = client.submit("[1,2,3,4,5,6,7,8,9]");
    final CompletableFuture<List<Result>> batch1 = results.some(5);
    final CompletableFuture<List<Result>> batch2 = results.some(5);
    final CompletableFuture<List<Result>> batchNothingLeft = results.some(5);

    assertEquals(5, batch1.get().size());
    assertEquals(1, batch1.get().get(0).getInt());
    assertEquals(2, batch1.get().get(1).getInt());
    assertEquals(3, batch1.get().get(2).getInt());
    assertEquals(4, batch1.get().get(3).getInt());
    assertEquals(5, batch1.get().get(4).getInt());

    assertEquals(4, batch2.get().size());
    assertEquals(6, batch2.get().get(0).getInt());
    assertEquals(7, batch2.get().get(1).getInt());
    assertEquals(8, batch2.get().get(2).getInt());
    assertEquals(9, batch2.get().get(3).getInt());

    assertEquals(0, batchNothingLeft.get().size());

    cluster.close();
}
 
Example #27
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSerializeToStringWhenRequestedGraphBinaryV1() throws Exception {
    final Map<String, Object> m = new HashMap<>();
    m.put("serializeResultToString", true);
    final GraphBinaryMessageSerializerV1 serializer = new GraphBinaryMessageSerializerV1();
    serializer.configure(m, null);

    final Cluster cluster = TestClientFactory.build().serializer(serializer).create();
    final Client client = cluster.connect();

    final ResultSet resultSet = client.submit("TinkerFactory.createClassic()");
    final List<Result> results = resultSet.all().join();
    assertEquals(1, results.size());
    assertEquals("tinkergraph[vertices:6 edges:6]", results.get(0).getString());

    cluster.close();
}
 
Example #28
Source File: GremlinDriverIntegrateTest.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldProcessSessionRequestsInOrderAfterTimeout() throws Exception {
    final Cluster cluster = TestClientFactory.open();
    final Client client = cluster.connect(name.getMethodName());

    for(int index = 0; index < 50; index++)
    {
        final CompletableFuture<ResultSet> first = client.submitAsync(
                "Object mon1 = 'mon1';\n" +
                        "synchronized (mon1) {\n" +
                        "    mon1.wait();\n" +
                        "} ");

        final CompletableFuture<ResultSet> second = client.submitAsync(
                "Object mon2 = 'mon2';\n" +
                        "synchronized (mon2) {\n" +
                        "    mon2.wait();\n" +
                        "}");

        final CompletableFuture<ResultSet> third = client.submitAsync(
                "Object mon3 = 'mon3';\n" +
                        "synchronized (mon3) {\n" +
                        "    mon3.wait();\n" +
                        "}");

        final CompletableFuture<ResultSet> fourth = client.submitAsync(
                "Object mon4 = 'mon4';\n" +
                        "synchronized (mon4) {\n" +
                        "    mon4.wait();\n" +
                        "}");

        final CompletableFuture<List<Result>> futureFirst = first.get().all();
        final CompletableFuture<List<Result>> futureSecond = second.get().all();
        final CompletableFuture<List<Result>> futureThird = third.get().all();
        final CompletableFuture<List<Result>> futureFourth = fourth.get().all();

        assertFutureTimeout(futureFirst);
        assertFutureTimeout(futureSecond);
        assertFutureTimeout(futureThird);
        assertFutureTimeout(futureFourth);
    }
}
 
Example #29
Source File: GremlinResultSetIntegrateTest.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldReturnResponseAttributesViaNoContent() throws Exception {
    final ResultSet results = client.submit("[]");
    final Map<String,Object> attr = results.statusAttributes().get(20000, TimeUnit.MILLISECONDS);
    assertThat(attr.containsKey(Tokens.ARGS_HOST), is(true));
}
 
Example #30
Source File: GremlinResultSetIntegrateTest.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldReturnResponseAttributesViaSuccess() throws Exception {
    final ResultSet results = client.submit("gmodern.V()");
    final Map<String,Object> attr = results.statusAttributes().get(20000, TimeUnit.MILLISECONDS);
    assertThat(attr.containsKey(Tokens.ARGS_HOST), is(true));
}