You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by ke...@apache.org on 2025/04/14 20:18:21 UTC
(tinkerpop) 01/01: Update feature tests to assert traversal construction exceptions.
This is an automated email from the ASF dual-hosted git repository.
kenhuuu pushed a commit to branch assert-traversal-construction-exception
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git
commit c4e005cdfdb51eb7009cdc4ca1a002137ede580e
Author: Ken Hu <10...@users.noreply.github.com>
AuthorDate: Mon Apr 14 11:41:53 2025 -0700
Update feature tests to assert traversal construction exceptions.
It wasn't possible to assert for failures that occurred during traversal
construction in the feature tests which made them unsuitable for testing
steps that threw exceptions.
Go Traversal needed to be updated as well since HasNext and Next didn't
properly surface those exceptions.
---
CHANGELOG.asciidoc | 1 +
docs/src/upgrade/release-3.8.x.asciidoc | 8 ++++++++
.../tinkerpop/gremlin/jsr223/JavaTranslator.java | 14 +++++++++++---
.../Gremlin.Net.IntegrationTest/Gherkin/Gremlin.cs | 1 +
gremlin-go/driver/cucumber/gremlin.go | 1 +
gremlin-go/driver/resultSet.go | 12 ++++++++----
gremlin-go/driver/resultSet_test.go | 20 ++++++++++++++++----
gremlin-go/driver/traversal.go | 17 ++++++++++++++---
.../gremlin-javascript/test/cucumber/gremlin.js | 1 +
gremlin-python/src/main/python/radish/gremlin.py | 1 +
.../tinkerpop/gremlin/features/StepDefinition.java | 8 ++++++++
.../gremlin/test/features/filter/Range.feature | 9 +++++++++
12 files changed, 79 insertions(+), 14 deletions(-)
diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc
index f50e509d06..3fa550d008 100644
--- a/CHANGELOG.asciidoc
+++ b/CHANGELOG.asciidoc
@@ -45,6 +45,7 @@ This release also includes changes from <<release-3-7-XXX, 3.7.XXX>>.
* Updated `OptionsStrategy` in `gremlin-python` to take options directly as keyword arguments.
* Added static `instance()` method to `ElementIdStrategy` to an instance with the default configuration.
* Updated `ElementIdStrategy.getConfiguration()` to help with serialization.
+* Changed behavior of `resultSet.IsEmpty` in `gremlin-go` to return channel errors.
* Changed type for `ReservedKeysVerificationStrategy.keys` in .NET to take a `Set<string>` rather than `List<string>`.
* Fixed bug in `group()` value traversal of the second `by()` where a `CollectingBarrierStep` could produce an unexpected filtering effect when `ReducingBarrierStep` or `SupplyingBarrierStep` instances were not taken into account.
* Changed `DetachedFactory` to special case the handling of `ComputerAdjacentVertex` which doesn't carry properties but still needs to be detachable for OLAP cases.
diff --git a/docs/src/upgrade/release-3.8.x.asciidoc b/docs/src/upgrade/release-3.8.x.asciidoc
index b241931d94..d269b94eda 100644
--- a/docs/src/upgrade/release-3.8.x.asciidoc
+++ b/docs/src/upgrade/release-3.8.x.asciidoc
@@ -253,6 +253,14 @@ The `GremlinLangScriptEngine` has been modified to treat float literals without
or 'd') as Double by default. Users who need BigDecimal precision can still use the 'm' suffix (e.g., 1.0m).
`GremlinGroovyScriptEngine` will still default to BigDecimal for float literals.
+==== Gremlin Go API Changes
+
+The `IsEmpty` function in `resultSet` has been changed to return `(bool, error)`. If an error is detected in the
+channel then `false` will be returned along with the error. This is in contrast to the previous behavior where false
+was returned and the error was ignored which lead to error information being missing from the result. This may have
+lead you to believe that there were simply no results when there was actually an issue with the query. This change also
+means the `HasNext` and `Next` functions for `Traversal` will correctly return the error.
+
==== group() Value Traversal Semantics
The `group()` step takes two `by()` modulators. The first defines the key for the grouping and the second acts upon the
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/JavaTranslator.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/JavaTranslator.java
index 7689b7d60b..fa56a2cd4f 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/JavaTranslator.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/JavaTranslator.java
@@ -241,6 +241,7 @@ public final class JavaTranslator<S extends TraversalSource, T extends Traversal
"Could not locate method", delegate, methodName, argumentsCopy));
}
+ boolean found = false;
try {
for (final ReflectedMethod methodx : methodCache.get(methodName)) {
final Method method = methodx.method;
@@ -248,7 +249,7 @@ public final class JavaTranslator<S extends TraversalSource, T extends Traversal
final Parameter[] parameters = methodx.parameters;
if (parameters.length == argumentsCopy.length || methodx.hasVarArgs) {
final Object[] newArguments = new Object[parameters.length];
- boolean found = true;
+ found = true;
for (int i = 0; i < parameters.length; i++) {
if (parameters[i].isVarArgs()) {
final Class<?> parameterClass = parameters[i].getType().getComponentType();
@@ -323,8 +324,15 @@ public final class JavaTranslator<S extends TraversalSource, T extends Traversal
}
}
} catch (final Throwable e) {
- throw new IllegalStateException(generateMethodNotFoundMessage(
- e.getMessage(), null, methodName, argumentsCopy), e);
+ if (found && (e instanceof InvocationTargetException)) {
+ // get the target exception out for a better message, otherwise, it will be null.
+ final InvocationTargetException ite = (InvocationTargetException) e;
+ throw new IllegalStateException(generateMethodNotFoundMessage(
+ ite.getTargetException().getMessage(), null, methodName, argumentsCopy), ite.getTargetException());
+ } else {
+ throw new IllegalStateException(generateMethodNotFoundMessage(
+ e.getMessage(), null, methodName, argumentsCopy), e);
+ }
}
// if it got down here then the method was in the cache but it was never called as it could not be found
diff --git a/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Gherkin/Gremlin.cs b/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Gherkin/Gremlin.cs
index 28ac0f0e3a..1c661ef43f 100644
--- a/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Gherkin/Gremlin.cs
+++ b/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Gherkin/Gremlin.cs
@@ -345,6 +345,7 @@ namespace Gremlin.Net.IntegrationTest.Gherkin
{"g_V_hasLabelXpersonX_order_byXageX_valuesXnameX_skipX1X", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.V().HasLabel("person").Order().By("age").Values<object>("name").Skip<object>(1)}},
{"g_VX1X_valuesXageX_rangeXlocal_20_30X", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.V(p["vid1"]).Values<object>("age").Range<object>(Scope.Local, 20, 30)}},
{"g_V_mapXin_hasIdX1XX_limitX2X_valuesXnameX", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.V().Map<object>(__.In().HasId(p["vid1"])).Limit<object>(2).Values<object>("name")}},
+ {"g_V_rangeX2_1X", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.V().Range<object>(2, 1)}},
{"g_E_sampleX1X", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.E().Sample(1)}},
{"g_E_sampleX2X_byXweightX", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.E().Sample(2).By("weight")}},
{"g_V_localXoutE_sampleX1X_byXweightXX", new List<Func<GraphTraversalSource, IDictionary<string, object>, ITraversal>> {(g,p) =>g.V().Local<object>(__.OutE().Sample(1).By("weight"))}},
diff --git a/gremlin-go/driver/cucumber/gremlin.go b/gremlin-go/driver/cucumber/gremlin.go
index c569150664..8ea73ee999 100644
--- a/gremlin-go/driver/cucumber/gremlin.go
+++ b/gremlin-go/driver/cucumber/gremlin.go
@@ -314,6 +314,7 @@ var translationMap = map[string][]func(g *gremlingo.GraphTraversalSource, p map[
"g_V_hasLabelXpersonX_order_byXageX_valuesXnameX_skipX1X": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V().HasLabel("person").Order().By("age").Values("name").Skip(1)}},
"g_VX1X_valuesXageX_rangeXlocal_20_30X": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V(p["vid1"]).Values("age").Range(gremlingo.Scope.Local, 20, 30)}},
"g_V_mapXin_hasIdX1XX_limitX2X_valuesXnameX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V().Map(gremlingo.T__.In().HasId(p["vid1"])).Limit(2).Values("name")}},
+ "g_V_rangeX2_1X": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V().Range(2, 1)}},
"g_E_sampleX1X": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.E().Sample(1)}},
"g_E_sampleX2X_byXweightX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.E().Sample(2).By("weight")}},
"g_V_localXoutE_sampleX1X_byXweightXX": {func(g *gremlingo.GraphTraversalSource, p map[string]interface{}) *gremlingo.GraphTraversal {return g.V().Local(gremlingo.T__.OutE().Sample(1).By("weight"))}},
diff --git a/gremlin-go/driver/resultSet.go b/gremlin-go/driver/resultSet.go
index 73269deddd..4450cdfb67 100644
--- a/gremlin-go/driver/resultSet.go
+++ b/gremlin-go/driver/resultSet.go
@@ -33,7 +33,7 @@ type ResultSet interface {
setStatusAttributes(statusAttributes map[string]interface{})
GetStatusAttributes() map[string]interface{}
GetRequestID() string
- IsEmpty() bool
+ IsEmpty() (bool, error)
Close()
unlockedClose()
Channel() chan *Result
@@ -78,17 +78,21 @@ func (channelResultSet *channelResultSet) setError(err error) {
}
// IsEmpty returns true when the channelResultSet is empty.
-func (channelResultSet *channelResultSet) IsEmpty() bool {
+func (channelResultSet *channelResultSet) IsEmpty() (bool, error) {
channelResultSet.channelMutex.Lock()
// If our channel is empty and we have no data in it, wait for signal that the state has been updated.
if len(channelResultSet.channel) != 0 {
// Channel is not empty.
channelResultSet.channelMutex.Unlock()
- return false
+ return false, nil
+ } else if channelResultSet.err != nil {
+ // Channel has an error.
+ channelResultSet.channelMutex.Unlock()
+ return true, channelResultSet.err
} else if channelResultSet.closed {
// Channel is empty and closed.
channelResultSet.channelMutex.Unlock()
- return true
+ return true, nil
} else {
// Channel is empty and not closed. Need to wait for signal that state has changed, otherwise
// we do not know if it is empty or not.
diff --git a/gremlin-go/driver/resultSet_test.go b/gremlin-go/driver/resultSet_test.go
index df9752cac8..7e4a81c728 100644
--- a/gremlin-go/driver/resultSet_test.go
+++ b/gremlin-go/driver/resultSet_test.go
@@ -123,28 +123,36 @@ func TestChannelResultSet(t *testing.T) {
t.Run("Test ResultSet IsEmpty before signal.", func(t *testing.T) {
channelResultSet := newChannelResultSet(mockID, getSyncMap())
go closeAfterTime(500, channelResultSet)
- empty := channelResultSet.IsEmpty()
+ empty, _ := channelResultSet.IsEmpty()
assert.True(t, empty)
})
t.Run("Test ResultSet IsEmpty after signal.", func(t *testing.T) {
channelResultSet := newChannelResultSet(mockID, getSyncMap())
channelResultSet.Close()
- empty := channelResultSet.IsEmpty()
+ empty, _ := channelResultSet.IsEmpty()
assert.True(t, empty)
})
t.Run("Test ResultSet IsEmpty after close.", func(t *testing.T) {
channelResultSet := newChannelResultSet(mockID, getSyncMap())
go addAfterTime(500, channelResultSet)
- empty := channelResultSet.IsEmpty()
+ empty, _ := channelResultSet.IsEmpty()
assert.False(t, empty)
channelResultSet.One()
go closeAfterTime(500, channelResultSet)
- empty = channelResultSet.IsEmpty()
+ empty, _ = channelResultSet.IsEmpty()
assert.True(t, empty)
})
+ t.Run("Test ResultSet IsEmpty handles error.", func(t *testing.T) {
+ channelResultSet := newChannelResultSet(mockID, getSyncMap())
+ setError(channelResultSet, fmt.Errorf("injected %s error", "ResultSet"))
+ empty, err := channelResultSet.IsEmpty()
+ assert.True(t, empty)
+ assert.True(t, err != nil)
+ })
+
t.Run("Test ResultSet removes self from container.", func(t *testing.T) {
container := getSyncMap()
assert.Equal(t, 0, container.size())
@@ -172,6 +180,10 @@ func AddResults(resultSet ResultSet, count int) {
}
}
+func setError(resultSet ResultSet, resultError error) {
+ resultSet.setError(resultError)
+}
+
func closeAfterTime(millisecondTicks time.Duration, resultSet ResultSet) {
time.Sleep(millisecondTicks * time.Millisecond)
resultSet.Close()
diff --git a/gremlin-go/driver/traversal.go b/gremlin-go/driver/traversal.go
index a6011eab53..037ea2f269 100644
--- a/gremlin-go/driver/traversal.go
+++ b/gremlin-go/driver/traversal.go
@@ -102,7 +102,13 @@ func (t *Traversal) HasNext() (bool, error) {
if err != nil {
return false, err
}
- return !results.IsEmpty(), nil
+
+ isEmpty, emptyErr := results.IsEmpty()
+ if emptyErr != nil {
+ return false, emptyErr
+ } else {
+ return !isEmpty, nil
+ }
}
// Next returns next result.
@@ -111,9 +117,14 @@ func (t *Traversal) Next() (*Result, error) {
if err != nil {
return nil, err
}
- if results.IsEmpty() {
+
+ isEmpty, emptyErr := results.IsEmpty()
+ if emptyErr != nil {
+ return nil, emptyErr
+ } else if isEmpty {
return nil, newError(err0903NextNoResultsLeftError)
}
+
result, _, err := results.One()
return result, err
}
@@ -718,7 +729,7 @@ func ParseBigDecimal(strValue string) *BigDecimal {
}
// resolve big int
unscaled := new(big.Int)
- unscaled, ok = unscaled.SetString(strings.Replace(bfVal.String(), ".", "",-1), 10)
+ unscaled, ok = unscaled.SetString(strings.Replace(bfVal.String(), ".", "", -1), 10)
if !ok {
return nil
}
diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/gremlin.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/gremlin.js
index 4485104c9a..46227416d5 100644
--- a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/gremlin.js
+++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/cucumber/gremlin.js
@@ -335,6 +335,7 @@ const gremlins = {
g_V_hasLabelXpersonX_order_byXageX_valuesXnameX_skipX1X: [function({g}) { return g.V().hasLabel("person").order().by("age").values("name").skip(1) }],
g_VX1X_valuesXageX_rangeXlocal_20_30X: [function({g, vid1}) { return g.V(vid1).values("age").range(Scope.local, 20, 30) }],
g_V_mapXin_hasIdX1XX_limitX2X_valuesXnameX: [function({g, vid1}) { return g.V().map(__.in_().hasId(vid1)).limit(2).values("name") }],
+ g_V_rangeX2_1X: [function({g}) { return g.V().range(2, 1) }],
g_E_sampleX1X: [function({g}) { return g.E().sample(1) }],
g_E_sampleX2X_byXweightX: [function({g}) { return g.E().sample(2).by("weight") }],
g_V_localXoutE_sampleX1X_byXweightXX: [function({g}) { return g.V().local(__.outE().sample(1).by("weight")) }],
diff --git a/gremlin-python/src/main/python/radish/gremlin.py b/gremlin-python/src/main/python/radish/gremlin.py
index eddc2a5a9a..e3c53ee38c 100644
--- a/gremlin-python/src/main/python/radish/gremlin.py
+++ b/gremlin-python/src/main/python/radish/gremlin.py
@@ -317,6 +317,7 @@ world.gremlins = {
'g_V_hasLabelXpersonX_order_byXageX_valuesXnameX_skipX1X': [(lambda g:g.V().has_label('person').order().by('age').values('name').skip(1))],
'g_VX1X_valuesXageX_rangeXlocal_20_30X': [(lambda g, vid1=None:g.V(vid1).values('age').range_(Scope.local, 20, 30))],
'g_V_mapXin_hasIdX1XX_limitX2X_valuesXnameX': [(lambda g, vid1=None:g.V().map(__.in_().has_id(vid1)).limit(2).values('name'))],
+ 'g_V_rangeX2_1X': [(lambda g:g.V().range_(2, 1))],
'g_E_sampleX1X': [(lambda g:g.E().sample(1))],
'g_E_sampleX2X_byXweightX': [(lambda g:g.E().sample(2).by('weight'))],
'g_V_localXoutE_sampleX1X_byXweightXX': [(lambda g:g.V().local(__.out_e().sample(1).by('weight')))],
diff --git a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/features/StepDefinition.java b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/features/StepDefinition.java
index 13c6debeac..f14a1a5235 100644
--- a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/features/StepDefinition.java
+++ b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/features/StepDefinition.java
@@ -307,6 +307,10 @@ public final class StepDefinition {
@When("iterated to list")
public void iteratedToList() {
+ if ((null == traversal) && (error != null)) {
+ return; // Error probably occurred during traversal construction. Skip to prevent NPE.
+ }
+
try {
result = traversal.toList();
} catch (Exception ex) {
@@ -316,6 +320,10 @@ public final class StepDefinition {
@When("iterated next")
public void iteratedNext() {
+ if ((null == traversal) && (error != null)) {
+ return; // Error probably occurred during traversal construction. Skip to prevent NPE.
+ }
+
try {
result = traversal.next();
} catch (Exception ex) {
diff --git a/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/test/features/filter/Range.feature b/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/test/features/filter/Range.feature
index d185d47f8e..982a1c3e1b 100644
--- a/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/test/features/filter/Range.feature
+++ b/gremlin-test/src/main/resources/org/apache/tinkerpop/gremlin/test/features/filter/Range.feature
@@ -242,3 +242,12 @@ Feature: Step - range()
| result |
| marko |
| marko |
+
+ Scenario: g_V_rangeX2_1X
+ Given the modern graph
+ And the traversal of
+ """
+ g.V().range(2, 1)
+ """
+ When iterated to list
+ Then the traversal will raise an error with message containing text of "Not a legal range: [2, 1]"