You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by da...@apache.org on 2017/01/30 18:00:29 UTC
[2/7] tinkerpop git commit: added fixtures,
spruced up the driver tests a bit
added fixtures, spruced up the driver tests a bit
Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/484c91ef
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/484c91ef
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/484c91ef
Branch: refs/heads/TINKERPOP-1599
Commit: 484c91ef07259d0b531492401a36ca629bd65253
Parents: 2e639b8
Author: davebshow <da...@gmail.com>
Authored: Sat Jan 28 13:48:05 2017 -0500
Committer: davebshow <da...@gmail.com>
Committed: Mon Jan 30 11:51:24 2017 -0500
----------------------------------------------------------------------
.../src/main/jython/tests/conftest.py | 72 +++++++++
.../src/main/jython/tests/driver/test_client.py | 98 ++++++++++++
.../driver/test_driver_remote_connection.py | 148 ++++++-------------
.../test_driver_remote_connection_threaded.py | 77 +++++-----
4 files changed, 249 insertions(+), 146 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/484c91ef/gremlin-python/src/main/jython/tests/conftest.py
----------------------------------------------------------------------
diff --git a/gremlin-python/src/main/jython/tests/conftest.py b/gremlin-python/src/main/jython/tests/conftest.py
new file mode 100644
index 0000000..be0de8a
--- /dev/null
+++ b/gremlin-python/src/main/jython/tests/conftest.py
@@ -0,0 +1,72 @@
+'''
+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.
+'''
+import concurrent.futures
+import pytest
+
+from six.moves import queue
+
+from gremlin_python.driver.client import Client
+from gremlin_python.driver.connection import Connection
+from gremlin_python.driver.driver_remote_connection import (
+ DriverRemoteConnection)
+from gremlin_python.driver.protocol import GremlinServerWSProtocol
+from gremlin_python.driver.tornado.transport import TornadoTransport
+
+
+@pytest.fixture
+def connection(request):
+ try:
+ protocol = GremlinServerWSProtocol(
+ username='stephen', password='password')
+ executor = concurrent.futures.ThreadPoolExecutor(5)
+ pool = queue.Queue()
+ conn = Connection('ws://localhost:8182/gremlin', 'g', protocol,
+ lambda: TornadoTransport(), executor, pool)
+ except:
+ pytest.skip('Gremlin Server is not running')
+ else:
+ def fin():
+ executor.shutdown()
+ conn.close()
+ request.addfinalizer(fin)
+ return conn
+
+@pytest.fixture
+def client(request):
+ try:
+ client = Client('ws://localhost:8182/gremlin', 'g')
+ except:
+ pytest.skip('Gremlin Server is not running')
+ else:
+ def fin():
+ client.close()
+ request.addfinalizer(fin)
+ return client
+
+@pytest.fixture
+def remote_connection(request):
+ try:
+ remote_conn = DriverRemoteConnection('ws://localhost:8182/gremlin', 'g')
+ except:
+ pytest.skip('Gremlin Server is not running')
+ else:
+ def fin():
+ remote_conn.close()
+ request.addfinalizer(fin)
+ return remote_conn
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/484c91ef/gremlin-python/src/main/jython/tests/driver/test_client.py
----------------------------------------------------------------------
diff --git a/gremlin-python/src/main/jython/tests/driver/test_client.py b/gremlin-python/src/main/jython/tests/driver/test_client.py
new file mode 100644
index 0000000..9e62687
--- /dev/null
+++ b/gremlin-python/src/main/jython/tests/driver/test_client.py
@@ -0,0 +1,98 @@
+'''
+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.
+'''
+import pytest
+
+from gremlin_python.driver.client import Client
+from gremlin_python.driver.request import RequestMessage
+from gremlin_python.structure.graph import Graph
+
+__author__ = 'David M. Brown (davebshow@gmail.com)'
+
+
+def test_connection(connection):
+ g = Graph().traversal()
+ t = g.V()
+ message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode})
+ results_set = connection.write(message).result()
+ future = results_set.all()
+ results = future.result()
+ assert len(results) == 6
+ assert isinstance(results, list)
+
+def test_client(client):
+ g = Graph().traversal()
+ t = g.V()
+ message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode})
+ result_set = client.submit(message)
+ assert len(result_set.all().result()) == 6
+ client.close()
+
+def test_iterate_result_set(client):
+ g = Graph().traversal()
+ t = g.V()
+ message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode})
+ result_set = client.submit(message)
+ results = []
+ for result in result_set:
+ results += result
+ assert len(results) == 6
+ client.close()
+
+def test_client_async(client):
+ g = Graph().traversal()
+ t = g.V()
+ message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode})
+ future = client.submitAsync(message)
+ assert not future.done()
+ result_set = future.result()
+ assert len(result_set.all().result()) == 6
+ client.close()
+
+def test_connection_share(client):
+ # Overwrite fixture with pool_size=1 client
+ client = Client('ws://localhost:8182/gremlin', 'g', pool_size=1)
+ g = Graph().traversal()
+ t = g.V()
+ message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode})
+ future = client.submitAsync(message)
+ future2 = client.submitAsync(message)
+
+ result_set2 = future2.result()
+ assert len(result_set2.all().result()) == 6
+
+ # This future has to finish for the second to yield result - pool_size=1
+ assert future.done()
+ result_set = future.result()
+ assert len(result_set.all().result()) == 6
+ client.close()
+
+def test_multi_conn_pool(client):
+ g = Graph().traversal()
+ t = g.V()
+ message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode})
+ future = client.submitAsync(message)
+ future2 = client.submitAsync(message)
+
+ result_set2 = future2.result()
+ assert len(result_set2.all().result()) == 6
+
+ # with connection pool `future` may or may not be done here
+ result_set = future.result()
+ assert len(result_set.all().result()) == 6
+ client.close()
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/484c91ef/gremlin-python/src/main/jython/tests/driver/test_driver_remote_connection.py
----------------------------------------------------------------------
diff --git a/gremlin-python/src/main/jython/tests/driver/test_driver_remote_connection.py b/gremlin-python/src/main/jython/tests/driver/test_driver_remote_connection.py
index 3e70928..c1c0ef5 100644
--- a/gremlin-python/src/main/jython/tests/driver/test_driver_remote_connection.py
+++ b/gremlin-python/src/main/jython/tests/driver/test_driver_remote_connection.py
@@ -16,19 +16,14 @@ KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
'''
-
-__author__ = 'Marko A. Rodriguez (http://markorodriguez.com)'
-
-import unittest
-from unittest import TestCase
-
import pytest
from tornado import ioloop, gen
from gremlin_python import statics
from gremlin_python.statics import long
-from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
+from gremlin_python.driver.driver_remote_connection import (
+ DriverRemoteConnection)
from gremlin_python.process.traversal import Traverser
from gremlin_python.process.traversal import TraversalStrategy
from gremlin_python.process.graph_traversal import __
@@ -36,16 +31,17 @@ from gremlin_python.structure.graph import Graph
from gremlin_python.structure.graph import Vertex
from gremlin_python.process.strategies import SubgraphStrategy
+__author__ = 'Marko A. Rodriguez (http://markorodriguez.com)'
+
-class TestDriverRemoteConnection(TestCase):
- def test_traversals(self):
+class TestDriverRemoteConnection(object):
+ def test_traversals(self, remote_connection):
statics.load_statics(globals())
- connection = DriverRemoteConnection('ws://localhost:45940/gremlin', 'g')
- assert "remoteconnection[ws://localhost:45940/gremlin,g]" == str(connection)
- g = Graph().traversal().withRemote(connection)
+ assert "remoteconnection[ws://localhost:8182/gremlin,g]" == str(remote_connection)
+ g = Graph().traversal().withRemote(remote_connection)
assert long(6) == g.V().count().toList()[0]
- #
+ # #
assert Vertex(1) == g.V(1).next()
assert 1 == g.V(1).id().next()
assert Traverser(Vertex(1)) == g.V(1).nextTraverser()
@@ -56,26 +52,24 @@ class TestDriverRemoteConnection(TestCase):
assert 2 == len(results)
assert "lop" in results
assert "ripple" in results
- #
+ # #
assert 10 == g.V().repeat(both()).times(5)[0:10].count().next()
assert 1 == g.V().repeat(both()).times(5)[0:1].count().next()
assert 0 == g.V().repeat(both()).times(5)[0:0].count().next()
assert 4 == g.V()[2:].count().next()
assert 2 == g.V()[:2].count().next()
- #
+ # #
results = g.withSideEffect('a',['josh','peter']).V(1).out('created').in_('created').values('name').where(within('a')).toList()
assert 2 == len(results)
assert 'josh' in results
assert 'peter' in results
- # todo: need a traversal metrics deserializer
+ # # todo: need a traversal metrics deserializer
g.V().out().profile().next()
- connection.close()
- def test_strategies(self):
+ def test_strategies(self, remote_connection):
statics.load_statics(globals())
- connection = DriverRemoteConnection('ws://localhost:45940/gremlin', 'g')
#
- g = Graph().traversal().withRemote(connection). \
+ g = Graph().traversal().withRemote(remote_connection). \
withStrategies(TraversalStrategy("SubgraphStrategy",
{"vertices": __.hasLabel("person"),
"edges": __.hasLabel("created")}))
@@ -84,7 +78,7 @@ class TestDriverRemoteConnection(TestCase):
assert 1 == g.V().label().dedup().count().next()
assert "person" == g.V().label().dedup().next()
#
- g = Graph().traversal().withRemote(connection). \
+ g = Graph().traversal().withRemote(remote_connection). \
withStrategies(SubgraphStrategy(vertices=__.hasLabel("person"), edges=__.hasLabel("created")))
assert 4 == g.V().count().next()
assert 0 == g.E().count().next()
@@ -98,24 +92,19 @@ class TestDriverRemoteConnection(TestCase):
assert "person" == g.V().label().next()
assert "marko" == g.V().name.next()
#
- g = Graph().traversal().withRemote(connection).withComputer()
+ g = Graph().traversal().withRemote(remote_connection).withComputer()
assert 6 == g.V().count().next()
assert 6 == g.E().count().next()
- connection.close()
- def test_side_effects(self):
+ def test_side_effects(self, remote_connection):
statics.load_statics(globals())
- connection = DriverRemoteConnection('ws://localhost:45940/gremlin', 'g')
#
- g = Graph().traversal().withRemote(connection)
+ g = Graph().traversal().withRemote(remote_connection)
###
t = g.V().hasLabel("project").name.iterate()
assert 0 == len(t.side_effects.keys())
- try:
+ with pytest.raises(Exception):
m = t.side_effects["m"]
- raise Exception("Accessing a non-existent key should throw an error")
- except KeyError:
- pass
###
t = g.V().out("created").groupCount("m").by("name")
results = t.toSet()
@@ -131,7 +120,7 @@ class TestDriverRemoteConnection(TestCase):
assert 1 == m["ripple"]
assert isinstance(m["lop"], long)
assert isinstance(m["ripple"], long)
- ###
+ ##
t = g.V().out("created").groupCount("m").by("name").name.aggregate("n")
results = t.toSet()
assert 2 == len(results)
@@ -154,16 +143,11 @@ class TestDriverRemoteConnection(TestCase):
assert 32 == list(results)[0]
assert 32 == t.side_effects['m']
assert 1 == len(t.side_effects.keys())
- try:
+ with pytest.raises(Exception):
x = t.side_effects["x"]
- raise Exception("Accessing a non-existent key should throw an error")
- except KeyError:
- pass
- connection.close()
- def test_side_effect_close(self):
- connection = DriverRemoteConnection('ws://localhost:45940/gremlin', 'g')
- g = Graph().traversal().withRemote(connection)
+ def test_side_effect_close(self, remote_connection):
+ g = Graph().traversal().withRemote(remote_connection)
t = g.V().aggregate('a').aggregate('b')
t.toList()
@@ -193,76 +177,30 @@ class TestDriverRemoteConnection(TestCase):
# Try to get 'b' directly from server, should throw error
with pytest.raises(Exception):
t.side_effects.value_lambda('b')
- connection.close()
- def test_promise(self):
- loop = ioloop.IOLoop.current()
- connection = DriverRemoteConnection('ws://localhost:45940/gremlin', 'g')
- g = Graph().traversal().withRemote(connection)
-
- @gen.coroutine
- def go():
- future_traversal = g.V().promise(lambda x: x.toList())
- assert not future_traversal.done()
- resp = yield future_traversal
- assert future_traversal.done()
- assert len(resp) == 6
- count = yield g.V().count().promise(lambda x: x.next())
- assert count == 6
-
- loop.run_sync(go)
- connection.close()
-
- def test_promise_side_effects(self):
- loop = ioloop.IOLoop.current()
- connection = DriverRemoteConnection('ws://localhost:45940/gremlin', 'g')
- g = Graph().traversal().withRemote(connection)
-
- # Side effects are problematic in coroutines.
- # Because they are designed to be synchronous (calling `run_sync`)
- # they result in an error if called from a coroutine because
- # the event loop is already running
- @gen.coroutine
- def go():
- traversal = yield g.V().aggregate('a').promise()
- # Calling synchronous side effect methods from coroutine raises.
- with pytest.raises(RuntimeError):
- keys = traversal.side_effects.keys()
-
- with pytest.raises(RuntimeError):
- keys = traversal.side_effects.get('a')
-
- with pytest.raises(RuntimeError):
- keys = traversal.side_effects.close()
-
- loop.run_sync(go)
-
- # If we return the traversal though, we can use side effects per usual.
- @gen.coroutine
- def go():
- traversal = yield g.V().aggregate('a').promise()
- raise gen.Return(traversal) # Weird legacy Python compatible idiom
-
- # See, normal side effects.
- traversal = loop.run_sync(go)
- a, = traversal.side_effects.keys()
+ def test_promise(self, remote_connection):
+ g = Graph().traversal().withRemote(remote_connection)
+ future = g.V().aggregate('a').promise()
+ t = future.result()
+ assert len(t.toList()) == 6
+ a, = t.side_effects.keys()
assert a == 'a'
- results = traversal.side_effects.get('a')
+ results = t.side_effects.get('a')
assert results
- results = traversal.side_effects.close()
+ results = t.side_effects.close()
assert not results
- connection.close()
-
-if __name__ == '__main__':
- test = False
- try:
- connection = DriverRemoteConnection('ws://localhost:45940/gremlin', 'g')
- test = True
- connection.close()
- except:
- print("GremlinServer is not running and this test case will not execute: " + __file__)
+def test_in_tornado_app(remote_connection):
+ # Make sure nothing weird with loops
+ @gen.coroutine
+ def go():
+ conn = DriverRemoteConnection(
+ 'ws://localhost:8182/gremlin', 'g', pool_size=4)
+ g = Graph().traversal().withRemote(conn)
+ yield gen.sleep(0)
+ assert len(g.V().toList()) == 6
+ conn.close()
- if test:
- unittest.main()
+ io_loop = ioloop.IOLoop.current()
+ io_loop.run_sync(go)
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/484c91ef/gremlin-python/src/main/jython/tests/driver/test_driver_remote_connection_threaded.py
----------------------------------------------------------------------
diff --git a/gremlin-python/src/main/jython/tests/driver/test_driver_remote_connection_threaded.py b/gremlin-python/src/main/jython/tests/driver/test_driver_remote_connection_threaded.py
index 0c18651..075ba3d 100644
--- a/gremlin-python/src/main/jython/tests/driver/test_driver_remote_connection_threaded.py
+++ b/gremlin-python/src/main/jython/tests/driver/test_driver_remote_connection_threaded.py
@@ -16,58 +16,53 @@ KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
'''
-
-
-__author__ = 'David M. Brown (davebshow@gmail.com)'
-
-
import sys
from threading import Thread
import pytest
-
from six.moves import queue
-from tornado import ioloop
from gremlin_python.driver.driver_remote_connection import (
DriverRemoteConnection)
from gremlin_python.structure.graph import Graph
-
-skip = False
-try:
- connection = DriverRemoteConnection('ws://localhost:45940/gremlin', 'g')
- connection.close()
-except:
- skip = True
+__author__ = 'David M. Brown (davebshow@gmail.com)'
-@pytest.mark.skipif(skip, reason='Gremlin Server is not running')
-class TestDriverRemoteConnectionThreaded:
+def test_conns_in_threads(remote_connection):
+ q = queue.Queue()
+ child = Thread(target=_executor, args=(q, None))
+ child2 = Thread(target=_executor, args=(q, None))
+ child.start()
+ child2.start()
+ for x in range(2):
+ success = q.get()
+ assert success == 'success!'
+ child.join()
+ child2.join()
- def test_threaded_client(self):
- q = queue.Queue()
- # Here if we give each thread its own loop there is no problem.
- loop1 = ioloop.IOLoop()
- loop2 = ioloop.IOLoop()
- child = Thread(target=self._executor, args=(q, loop1))
- child2 = Thread(target=self._executor, args=(q, loop2))
- child.start()
- child2.start()
- for x in range(2):
- success = q.get()
- assert success == 'success!'
- child.join()
- child2.join()
+def test_conn_in_threads(remote_connection):
+ q = queue.Queue()
+ child = Thread(target=_executor, args=(q, remote_connection))
+ child2 = Thread(target=_executor, args=(q, remote_connection))
+ child.start()
+ child2.start()
+ for x in range(2):
+ success = q.get()
+ assert success == 'success!'
+ child.join()
+ child2.join()
- def _executor(self, q, loop):
- try:
- connection = DriverRemoteConnection(
- 'ws://localhost:45940/gremlin', 'g', loop=loop)
- g = Graph().traversal().withRemote(connection)
- assert len(g.V().toList()) == 6
- except:
- q.put(sys.exc_info()[0])
- else:
- q.put('success!')
- connection.close()
+def _executor(q, conn):
+ if not conn:
+ conn = DriverRemoteConnection(
+ 'ws://localhost:8182/gremlin', 'g', pool_size=4)
+ try:
+ g = Graph().traversal().withRemote(conn)
+ future = g.V().promise()
+ t = future.result()
+ assert len(t.toList()) == 6
+ except:
+ q.put(sys.exc_info()[0])
+ else:
+ q.put('success!')