You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2015/05/28 15:06:24 UTC

incubator-tinkerpop git commit: Write more tests for the GremlinExecutor.

Repository: incubator-tinkerpop
Updated Branches:
  refs/heads/master fa7592d7e -> ac9bb03a7


Write more tests for the GremlinExecutor.


Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/ac9bb03a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/ac9bb03a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/ac9bb03a

Branch: refs/heads/master
Commit: ac9bb03a7cbe5b145cb886f8704c493fcab626fc
Parents: fa7592d
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Thu May 28 09:06:04 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Thu May 28 09:06:04 2015 -0400

----------------------------------------------------------------------
 .../gremlin/groovy/engine/GremlinExecutor.java  |   4 +-
 .../groovy/engine/GremlinExecutorTest.java      | 113 ++++++++++++++++++-
 .../groovy/engine/GremlinExecutorInit.groovy    |   5 +-
 3 files changed, 114 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ac9bb03a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java
index 884eb39..5351275 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java
@@ -260,11 +260,11 @@ public class GremlinExecutor implements AutoCloseable {
 
                 afterSuccess.accept(bindings);
             } catch (Exception ex) {
-                final Throwable root = ExceptionUtils.getRootCause(ex);
+                final Throwable root = null == ex.getCause() ? ex : ExceptionUtils.getRootCause(ex);
 
                 // thread interruptions will typically come as the result of a timeout, so in those cases,
                 // check for that situation and convert to TimeoutException
-                if (root.getClass().equals(InterruptedException.class))
+                if (root instanceof InterruptedException)
                     evaluationFuture.completeExceptionally(new TimeoutException(
                             String.format("Script evaluation exceeded the configured threshold of %s ms for request [%s]: %s", scriptEvaluationTimeout, script, root.getMessage())));
                 else {

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ac9bb03a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java
index 5b8463d..4031890 100644
--- a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java
@@ -44,8 +44,12 @@ import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.IntStream;
 
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.contains;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
@@ -104,6 +108,54 @@ public class GremlinExecutorTest {
     }
 
     @Test
+    public void shouldEvalScriptWithMapBindings() throws Exception {
+        final GremlinExecutor gremlinExecutor = GremlinExecutor.build().create();
+        final Map<String,Object> b = new HashMap<>();
+        b.put("x", 1);
+        assertEquals(2, gremlinExecutor.eval("1+x", b).get());
+        gremlinExecutor.close();
+    }
+
+    @Test
+    public void shouldEvalScriptWithMapBindingsAndLanguage() throws Exception {
+        final GremlinExecutor gremlinExecutor = GremlinExecutor.build()
+                .addEngineSettings("nashorn", Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Collections.emptyMap()).create();
+        final Map<String,Object> b = new HashMap<>();
+        b.put("x", 1);
+        assertEquals(2.0, gremlinExecutor.eval("1+x", "nashorn", b).get());
+        gremlinExecutor.close();
+    }
+
+    @Test
+    public void shouldEvalScriptWithMapBindingsAndLanguageThenTransform() throws Exception {
+        final GremlinExecutor gremlinExecutor = GremlinExecutor.build()
+                .addEngineSettings("nashorn", Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Collections.emptyMap()).create();
+        final Map<String,Object> b = new HashMap<>();
+        b.put("x", 1);
+        assertEquals(4, gremlinExecutor.eval("1+x", "nashorn", b, r -> ((Double) r).intValue() * 2).get());
+        gremlinExecutor.close();
+    }
+
+    @Test
+    public void shouldEvalScriptWithMapBindingsAndLanguageThenConsume() throws Exception {
+        final GremlinExecutor gremlinExecutor = GremlinExecutor.build()
+                .addEngineSettings("nashorn", Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Collections.emptyMap()).create();
+        final Map<String,Object> b = new HashMap<>();
+        b.put("x", 1);
+
+        final CountDownLatch latch = new CountDownLatch(1);
+        final AtomicInteger result = new AtomicInteger(0);
+        assertEquals(2.0, gremlinExecutor.eval("1+x", "nashorn", b, r -> {
+            result.set(((Double) r).intValue() * 2);
+            latch.countDown();
+        }).get());
+
+        latch.await();
+        assertEquals(4, result.get());
+        gremlinExecutor.close();
+    }
+
+    @Test
     public void shouldEvalScriptWithGlobalBindings() throws Exception {
         final Bindings b = new SimpleBindings();
         b.put("x", 1);
@@ -113,6 +165,16 @@ public class GremlinExecutorTest {
     }
 
     @Test
+    public void shouldGetGlobalBindings() throws Exception {
+        final Bindings b = new SimpleBindings();
+        final Object bound = new Object();
+        b.put("x", bound);
+        final GremlinExecutor gremlinExecutor = GremlinExecutor.build().globalBindings(b).create();
+        assertEquals(bound, gremlinExecutor.getGlobalBindings().get("x"));
+        gremlinExecutor.close();
+    }
+
+    @Test
     public void shouldEvalScriptWithGlobalAndLocalBindings() throws Exception {
         final Bindings g = new SimpleBindings();
         g.put("x", 1);
@@ -173,9 +235,7 @@ public class GremlinExecutorTest {
         try {
             gremlinExecutor.eval("10/0").get();
             fail();
-        } catch (Exception ex) {
-
-        }
+        } catch (Exception ignored) { }
 
         // need to wait long enough for the script to complete
         Thread.sleep(750);
@@ -297,8 +357,7 @@ public class GremlinExecutorTest {
         );
 
         final Thread t2 = new Thread(() -> {
-            while (failures.get() < 500) {
-            }
+            while (failures.get() < 500) {   }
             gremlinExecutor.getScriptEngines().addImports(imports);
         });
 
@@ -330,6 +389,28 @@ public class GremlinExecutorTest {
     }
 
     @Test
+    public void shouldInitializeWithScriptAndPromoteBinding() throws Exception {
+        final GremlinExecutor gremlinExecutor = GremlinExecutor.build()
+                .addEngineSettings("gremlin-groovy",
+                        Collections.emptyList(),
+                        Collections.emptyList(),
+                        Arrays.asList(PATHS.get("GremlinExecutorInit.groovy")),
+                        Collections.emptyMap())
+                .promoteBindings(kv -> kv.getValue() instanceof Set)
+                .create();
+
+        assertEquals(2, gremlinExecutor.eval("add(1,1)").get());
+        assertThat(gremlinExecutor.getGlobalBindings().keySet(), contains("someSet"));
+        assertThat(gremlinExecutor.getGlobalBindings().keySet(), not(contains("someMap")));
+
+        final Set<String> s = (Set<String>) gremlinExecutor.getGlobalBindings().get("someSet");
+        assertThat(s, contains("test"));
+        assertEquals(1, s.size());
+
+        gremlinExecutor.close();
+    }
+
+    @Test
     public void shouldSecureAll() throws Exception {
         GroovyInterceptor.getApplicableInterceptors().forEach(GroovyInterceptor::unregister);
         final Map<String, Object> config = new HashMap<>();
@@ -411,4 +492,26 @@ public class GremlinExecutorTest {
         service.shutdown();
         service.awaitTermination(30000, TimeUnit.MILLISECONDS);
     }
+
+    @Test
+    public void shouldGetExecutorService() throws Exception {
+        final ScheduledExecutorService service = Executors.newScheduledThreadPool(4, testingThreadFactory);
+        final GremlinExecutor gremlinExecutor = GremlinExecutor.build()
+                .executorService(service)
+                .scheduledExecutorService(service).create();
+
+        assertSame(service, gremlinExecutor.getExecutorService());
+        gremlinExecutor.close();
+    }
+
+    @Test
+    public void shouldGetScheduledExecutorService() throws Exception {
+        final ScheduledExecutorService service = Executors.newScheduledThreadPool(4, testingThreadFactory);
+        final GremlinExecutor gremlinExecutor = GremlinExecutor.build()
+                .executorService(service)
+                .scheduledExecutorService(service).create();
+
+        assertSame(service, gremlinExecutor.getScheduledExecutorService());
+        gremlinExecutor.close();
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ac9bb03a/gremlin-groovy/src/test/resources/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorInit.groovy
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/resources/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorInit.groovy b/gremlin-groovy/src/test/resources/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorInit.groovy
index c320195..513ccd4 100644
--- a/gremlin-groovy/src/test/resources/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorInit.groovy
+++ b/gremlin-groovy/src/test/resources/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorInit.groovy
@@ -16,4 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-def add(x, y) { x + y }
\ No newline at end of file
+def add(x, y) { x + y }
+
+someSet = ["test"] as Set
+someMap = [name:"stephen"]
\ No newline at end of file