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/07/06 19:17:56 UTC

incubator-tinkerpop git commit: Fixed a problem with result iteration in the driver.

Repository: incubator-tinkerpop
Updated Branches:
  refs/heads/master 96dab7625 -> 6cdea4a11


Fixed a problem with result iteration in the driver.

Corrected an issue where the iterator was requiring a call to hasNext() in order to advance properly.  Added tests.


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

Branch: refs/heads/master
Commit: 6cdea4a112d8ac3f5231cdfcf60cf37cc7c07644
Parents: 96dab76
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Mon Jul 6 12:34:42 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Mon Jul 6 12:34:42 2015 -0400

----------------------------------------------------------------------
 .../tinkerpop/gremlin/driver/ResultSet.java     | 11 +++++-
 .../tinkerpop/gremlin/driver/ResultSetTest.java | 39 ++++++++++++++++++++
 2 files changed, 49 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/6cdea4a1/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ResultSet.java
----------------------------------------------------------------------
diff --git a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ResultSet.java b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ResultSet.java
index 75ecce3..e7b2b0c 100644
--- a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ResultSet.java
+++ b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/ResultSet.java
@@ -21,6 +21,7 @@ package org.apache.tinkerpop.gremlin.driver;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.NoSuchElementException;
 import java.util.Spliterator;
 import java.util.Spliterators;
 import java.util.concurrent.CompletableFuture;
@@ -110,6 +111,9 @@ public final class ResultSet implements Iterable<Result> {
         return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator(), Spliterator.IMMUTABLE | Spliterator.SIZED), false);
     }
 
+    /**
+     * Returns a blocking iterator of the items streaming from the server to the client.
+     */
     @Override
     public Iterator<Result> iterator() {
         return new Iterator<Result>() {
@@ -127,7 +131,12 @@ public final class ResultSet implements Iterable<Result> {
 
             @Override
             public Result next() {
-                return nextOne;
+                if (null != nextOne || hasNext()) {
+                    final Result r = nextOne;
+                    nextOne = null;
+                    return r;
+                } else
+                    throw new NoSuchElementException();
             }
 
             @Override

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/6cdea4a1/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ResultSetTest.java
----------------------------------------------------------------------
diff --git a/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ResultSetTest.java b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ResultSetTest.java
index 2d84c6f..392566a 100644
--- a/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ResultSetTest.java
+++ b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/ResultSetTest.java
@@ -23,6 +23,7 @@ import org.junit.Test;
 
 import java.util.Iterator;
 import java.util.List;
+import java.util.NoSuchElementException;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -31,6 +32,7 @@ import java.util.stream.Stream;
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
 
 /**
  * @author Stephen Mallette (http://stephen.genoprime.com)
@@ -125,6 +127,43 @@ public class ResultSetTest extends AbstractResultQueueTest {
     }
 
     @Test
+    public void shouldIterateWithoutCheckingHasNext() throws Exception {
+        final Iterator itty = resultSet.iterator();
+        final AtomicInteger counter = new AtomicInteger(0);
+
+        addToQueue(100, 1, true, true);
+
+        for (int ix = 0; ix < 100; ix++) {
+            itty.next();
+            counter.incrementAndGet();
+        }
+
+        assertEquals(100, counter.get());
+        assertThat(itty.hasNext(), is(false));
+    }
+
+    @Test
+    public void shouldIterateAndThenThrowIfCallToNextExceedsAvailableResultItems() throws Exception {
+        final Iterator itty = resultSet.iterator();
+
+        addToQueue(3, 1, true, true);
+
+        assertThat(itty.hasNext(), is(true));
+        itty.next();
+        assertThat(itty.hasNext(), is(true));
+        itty.next();
+        itty.next();
+        assertThat(itty.hasNext(), is(false));
+
+        try {
+            itty.next();
+            fail("Should throw exception");
+        } catch (NoSuchElementException ignore) {
+            // this is the right path - an exception should toss here
+        }
+    }
+
+    @Test
     public void shouldStream() throws Exception {
         final Stream<Result> stream = resultSet.stream();
         final AtomicInteger counter = new AtomicInteger(0);