Java Code Examples for org.apache.tinkerpop.gremlin.driver.Client#submit()

The following examples show how to use org.apache.tinkerpop.gremlin.driver.Client#submit() . 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 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 2
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 3
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 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 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 6
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 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 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 9
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 10
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 11
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 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: 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 14
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 15
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 16
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 17
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 18
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 19
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 20
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());
}