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/05/04 15:03:58 UTC

[05/10] incubator-tinkerpop git commit: Added process tests for traversal interruption

Added process tests for traversal interruption

Covered GraphStep, VertexStep and PropertiesStep which should yield some coverage to provider implementations.


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

Branch: refs/heads/master
Commit: 3a5f738c5ef30711bce227cfb1946739fda9050c
Parents: 0d4c07f
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Tue Apr 19 19:09:20 2016 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Wed May 4 06:43:35 2016 -0400

----------------------------------------------------------------------
 .../util/TraversalInterruptedException.java     |  18 +++
 .../traversal/util/DefaultTraversalTest.java    |   1 -
 .../process/traversal/CoreTraversalTest.java    | 130 +++++++++++++++++++
 3 files changed, 148 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/3a5f738c/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/util/TraversalInterruptedException.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/util/TraversalInterruptedException.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/util/TraversalInterruptedException.java
index 216a762..f3db464 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/util/TraversalInterruptedException.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/util/TraversalInterruptedException.java
@@ -1,3 +1,21 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
 package org.apache.tinkerpop.gremlin.process.traversal.util;
 
 import org.apache.tinkerpop.gremlin.process.traversal.Traversal;

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/3a5f738c/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/util/DefaultTraversalTest.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/util/DefaultTraversalTest.java b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/util/DefaultTraversalTest.java
index c1a4c6b..6f6a6c6 100644
--- a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/util/DefaultTraversalTest.java
+++ b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/process/traversal/util/DefaultTraversalTest.java
@@ -69,7 +69,6 @@ public class DefaultTraversalTest {
                     startedIterating.countDown();
                     counter.incrementAndGet();
                 }).iterate();
-                fail("Traversal should have been interrupted");
             } catch (Exception ex) {
                 exceptionThrown.set(ex instanceof TraversalInterruptedException);
             }

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/3a5f738c/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/CoreTraversalTest.java
----------------------------------------------------------------------
diff --git a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/CoreTraversalTest.java b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/CoreTraversalTest.java
index 930030e..200d94b 100644
--- a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/CoreTraversalTest.java
+++ b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/CoreTraversalTest.java
@@ -24,11 +24,13 @@ import org.apache.tinkerpop.gremlin.LoadGraphWith;
 import org.apache.tinkerpop.gremlin.process.AbstractGremlinProcessTest;
 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
 import org.apache.tinkerpop.gremlin.process.traversal.step.util.BulkSet;
+import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalInterruptedException;
 import org.apache.tinkerpop.gremlin.structure.Edge;
 import org.apache.tinkerpop.gremlin.structure.Graph;
 import org.apache.tinkerpop.gremlin.structure.Transaction;
 import org.apache.tinkerpop.gremlin.structure.Vertex;
 import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
+import org.hamcrest.CoreMatchers;
 import org.junit.Test;
 
 import java.util.HashSet;
@@ -36,8 +38,12 @@ import java.util.List;
 import java.util.Map;
 import java.util.Random;
 import java.util.Set;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.stream.Collectors;
 
+import static org.apache.tinkerpop.gremlin.LoadGraphWith.GraphData.GRATEFUL;
 import static org.apache.tinkerpop.gremlin.LoadGraphWith.GraphData.MODERN;
 import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.*;
 import static org.apache.tinkerpop.gremlin.structure.Graph.Features.GraphFeatures.FEATURE_TRANSACTIONS;
@@ -56,6 +62,130 @@ import static org.junit.Assert.*;
 public class CoreTraversalTest extends AbstractGremlinProcessTest {
 
     @Test
+    @LoadGraphWith(GRATEFUL)
+    public void shouldRespectThreadInterruptionInGraphStep() throws Exception {
+        final AtomicBoolean exceptionThrown = new AtomicBoolean(false);
+        final CountDownLatch startedIterating = new CountDownLatch(1);
+        final Thread t = new Thread(() -> {
+            try {
+                final Traversal traversal = g.V().sideEffect(traverser -> {
+                    startedIterating.countDown();
+
+                    // ensure that the whole traversal doesn't iterate out before we get a chance to interrupt
+                    if (startedIterating.getCount() == 0) {
+                        try {
+                            Thread.sleep(3000);
+                        } catch (Exception ignored) {
+                            Thread.currentThread().interrupt();
+                        }
+                    }
+                });
+                traversal.iterate();
+            } catch (Exception ex) {
+                exceptionThrown.set(ex instanceof TraversalInterruptedException);
+            }
+        });
+
+        t.start();
+
+        // total time for test should not exceed 5 seconds - this prevents the test from just hanging and allows
+        // it to finish with failure
+        assertThat(startedIterating.await(5000, TimeUnit.MILLISECONDS), CoreMatchers.is(true));
+
+        t.interrupt();
+        t.join();
+
+        // ensure that some but not all of the traversal was iterated and that the right exception was tossed
+        assertThat(exceptionThrown.get(), CoreMatchers.is(true));
+    }
+
+    @Test
+    @LoadGraphWith(GRATEFUL)
+    public void shouldRespectThreadInterruptionInVertexStep() throws Exception {
+        final AtomicBoolean exceptionThrown = new AtomicBoolean(false);
+        final CountDownLatch startedIterating = new CountDownLatch(1);
+        final Thread t = new Thread(() -> {
+            try {
+                final AtomicBoolean first = new AtomicBoolean(true);
+                final Traversal traversal = g.V().sideEffect(traverser -> {
+                    // let the first iteration flow through
+                    if (!first.compareAndSet(true, false)) {
+                        // ensure that the whole traversal doesn't iterate out before we get a chance to interrupt
+                        // the next iteration should stop so we can force the interrupt to be handled by VertexStep
+                        try {
+                            Thread.sleep(3000);
+                        } catch (Exception ignored) {
+                            // make sure that the interrupt propagates in case the interrupt occurs during sleep.
+                            // this should ensure VertexStep gets to try to throw the TraversalInterruptedException
+                            Thread.currentThread().interrupt();
+                        }
+                    }
+                }).out().sideEffect(traverser -> {
+                    startedIterating.countDown();
+                });
+                traversal.iterate();
+            } catch (Exception ex) {
+                exceptionThrown.set(ex instanceof TraversalInterruptedException);
+            }
+        });
+
+        t.start();
+
+        // total time for test should not exceed 5 seconds - this prevents the test from just hanging and allows
+        // it to finish with failure
+        assertThat(startedIterating.await(5000, TimeUnit.MILLISECONDS), CoreMatchers.is(true));
+
+        t.interrupt();
+        t.join();
+
+        // ensure that some but not all of the traversal was iterated and that the right exception was tossed
+        assertThat(exceptionThrown.get(), CoreMatchers.is(true));
+    }
+
+    @Test
+    @LoadGraphWith(GRATEFUL)
+    public void shouldRespectThreadInterruptionInPropertyStep() throws Exception {
+        final AtomicBoolean exceptionThrown = new AtomicBoolean(false);
+        final CountDownLatch startedIterating = new CountDownLatch(1);
+        final Thread t = new Thread(() -> {
+            try {
+                final AtomicBoolean first = new AtomicBoolean(true);
+                final Traversal traversal = g.V().sideEffect(traverser -> {
+                    // let the first iteration flow through
+                    if (!first.compareAndSet(true, false)) {
+                        // ensure that the whole traversal doesn't iterate out before we get a chance to interrupt
+                        // the next iteration should stop so we can force the interrupt to be handled by PropertyStep
+                        try {
+                            Thread.sleep(3000);
+                        } catch (Exception ignored) {
+                            // make sure that the interrupt propagates in case the interrupt occurs during sleep.
+                            // this should ensure PropertyStep gets to try to throw the TraversalInterruptedException
+                            Thread.currentThread().interrupt();
+                        }
+                    }
+                }).properties().sideEffect(traverser -> {
+                    startedIterating.countDown();
+                });
+                traversal.iterate();
+            } catch (Exception ex) {
+                exceptionThrown.set(ex instanceof TraversalInterruptedException);
+            }
+        });
+
+        t.start();
+
+        // total time for test should not exceed 5 seconds - this prevents the test from just hanging and allows
+        // it to finish with failure
+        assertThat(startedIterating.await(5000, TimeUnit.MILLISECONDS), CoreMatchers.is(true));
+
+        t.interrupt();
+        t.join();
+
+        // ensure that some but not all of the traversal was iterated and that the right exception was tossed
+        assertThat(exceptionThrown.get(), CoreMatchers.is(true));
+    }
+
+    @Test
     @LoadGraphWith
     public void shouldNeverPropagateANoBulkTraverser() {
         assertFalse(g.V().dedup().sideEffect(t -> t.asAdmin().setBulk(0)).hasNext());