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 2016/01/27 18:13:40 UTC

incubator-tinkerpop git commit: Added a couple of ScriptEngine tests

Repository: incubator-tinkerpop
Updated Branches:
  refs/heads/master c4bc256fb -> 4dd89e914


Added a couple of ScriptEngine tests

Tests deal with thread safety issues around the ScriptEngine use of variables.


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

Branch: refs/heads/master
Commit: 4dd89e91494ac0dd79132feb277165410ee0800f
Parents: c4bc256
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Wed Jan 27 12:12:52 2016 -0500
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Wed Jan 27 12:12:52 2016 -0500

----------------------------------------------------------------------
 .../groovy/engine/GremlinExecutorTest.java      | 45 +++++++++++++++++-
 .../jsr223/GremlinGroovyScriptEngineTest.java   | 50 ++++++++++++++++++++
 2 files changed, 94 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/4dd89e91/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 7bc0708..fcab563 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
@@ -22,6 +22,8 @@ import org.apache.commons.lang3.concurrent.BasicThreadFactory;
 import org.apache.tinkerpop.gremlin.TestHelper;
 import org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.ThreadInterruptCustomizerProvider;
 import org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.TimedInterruptCustomizerProvider;
+import org.javatuples.Pair;
+import org.javatuples.Triplet;
 import org.junit.Test;
 
 import javax.script.Bindings;
@@ -37,12 +39,14 @@ import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.CyclicBarrier;
+import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Collectors;
 import java.util.stream.IntStream;
 
 import static org.hamcrest.CoreMatchers.instanceOf;
@@ -372,7 +376,6 @@ public class GremlinExecutorTest {
         assertFalse(b2.get());
 
         gremlinExecutor.close();
-
     }
 
     @Test
@@ -594,4 +597,44 @@ public class GremlinExecutorTest {
         assertSame(service, gremlinExecutor.getScheduledExecutorService());
         gremlinExecutor.close();
     }
+
+    @Test
+    public void shouldAllowVariableReuseAcrossThreads() throws Exception {
+        final ExecutorService service = Executors.newFixedThreadPool(8, testingThreadFactory);
+        final GremlinExecutor gremlinExecutor = GremlinExecutor.build().create();
+
+        final int max = 256;
+        final List<Pair<Integer, List<Integer>>> futures = new ArrayList<>(max);
+        IntStream.range(0, max).forEach(i -> {
+            final int yValue = i * 2;
+            final Bindings b = new SimpleBindings();
+            b.put("x", i);
+            b.put("y", yValue);
+            final int zValue = i * -1;
+
+            final String script = "z=" + zValue + ";[x,y,z]";
+            try {
+                service.submit(() -> {
+                    try {
+                        final List<Integer> result = (List<Integer>) gremlinExecutor.eval(script, b).get();
+                        futures.add(Pair.with(i, result));
+                    } catch (Exception ex) {
+                        throw new RuntimeException(ex);
+                    }
+                });
+            } catch (Exception ex) {
+                throw new RuntimeException(ex);
+            }
+        });
+
+        service.shutdown();
+        service.awaitTermination(30000, TimeUnit.MILLISECONDS);
+
+        assertEquals(max, futures.size());
+        futures.forEach(t -> {
+            assertEquals(t.getValue0(), t.getValue1().get(0));
+            assertEquals(t.getValue0() * 2, t.getValue1().get(1).intValue());
+            assertEquals(t.getValue0() * -1, t.getValue1().get(2).intValue());
+        });
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/4dd89e91/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
index 3d5c17c..86d7e7a 100644
--- a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
@@ -19,10 +19,13 @@
 package org.apache.tinkerpop.gremlin.groovy.jsr223;
 
 import groovy.lang.Closure;
+import org.apache.commons.lang3.concurrent.BasicThreadFactory;
 import org.apache.tinkerpop.gremlin.groovy.CompilerCustomizerProvider;
 import org.apache.tinkerpop.gremlin.groovy.NoImportCustomizerProvider;
 import org.apache.tinkerpop.gremlin.structure.Vertex;
 import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
+import org.javatuples.Pair;
+import org.javatuples.Triplet;
 import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -33,13 +36,19 @@ import javax.script.ScriptEngine;
 import javax.script.ScriptException;
 import javax.script.SimpleBindings;
 import java.awt.*;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Set;
 import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicReference;
+import java.util.stream.IntStream;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.greaterThan;
@@ -318,4 +327,45 @@ public class GremlinGroovyScriptEngineTest {
         final ScriptEngine engine = new GremlinGroovyScriptEngine();
         assertEquals("轉注", engine.eval("'轉注'"));
     }
+
+    @Test
+    public void shouldAllowVariableReuseAcrossThreads() throws Exception {
+        final BasicThreadFactory testingThreadFactory = new BasicThreadFactory.Builder().namingPattern("test-gremlin-scriptengine-%d").build();
+        final ExecutorService service = Executors.newFixedThreadPool(8, testingThreadFactory);
+        final GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine();
+
+        final int max = 256;
+        final List<Pair<Integer, List<Integer>>> futures = new ArrayList<>(max);
+        IntStream.range(0, max).forEach(i -> {
+            final int yValue = i * 2;
+            final int zValue = i * -1;
+            final Bindings b = new SimpleBindings();
+            b.put("x", i);
+            b.put("y", yValue);
+
+            final String script = "z=" + zValue + ";[x,y,z]";
+            try {
+                service.submit(() -> {
+                    try {
+                        final List<Integer> result = (List<Integer>) scriptEngine.eval(script, b);
+                        futures.add(Pair.with(i, result));
+                    } catch (Exception ex) {
+                        throw new RuntimeException(ex);
+                    }
+                });
+            } catch (Exception ex) {
+                throw new RuntimeException(ex);
+            }
+        });
+
+        service.shutdown();
+        service.awaitTermination(30000, TimeUnit.MILLISECONDS);
+
+        assertEquals(max, futures.size());
+        futures.forEach(t -> {
+            assertEquals(t.getValue0(), t.getValue1().get(0));
+            assertEquals(t.getValue0() * 2, t.getValue1().get(1).intValue());
+            assertEquals(t.getValue0() * -1, t.getValue1().get(2).intValue());
+        });
+    }
 }
\ No newline at end of file