allow user to pass loop to DriverRemoteConnection. added multi-threaded tests.
Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/4649c812 Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/4649c812 Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/4649c812 Branch: refs/heads/TINKERPOP-1581 Commit: 4649c81201889e29f011d1aa46b5b6a2fab58470 Parents: 347a0a9 Author: davebshow <[email protected]> Authored: Fri Dec 9 16:40:53 2016 -0500 Committer: Stephen Mallette <[email protected]> Committed: Fri Dec 16 11:52:15 2016 -0500 ---------------------------------------------------------------------- .../driver/driver_remote_connection.py | 4 +- .../test_driver_remote_connection_threaded.py | 69 ++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/4649c812/gremlin-python/src/main/jython/gremlin_python/driver/driver_remote_connection.py ---------------------------------------------------------------------- diff --git a/gremlin-python/src/main/jython/gremlin_python/driver/driver_remote_connection.py b/gremlin-python/src/main/jython/gremlin_python/driver/driver_remote_connection.py index d975f60..37fcd1a 100644 --- a/gremlin-python/src/main/jython/gremlin_python/driver/driver_remote_connection.py +++ b/gremlin-python/src/main/jython/gremlin_python/driver/driver_remote_connection.py @@ -39,7 +39,9 @@ class DriverRemoteConnection(RemoteConnection): self._url = url self._username = username self._password = password - if loop is None: self._loop = ioloop.IOLoop.current() + if loop is None: + loop = ioloop.IOLoop.current() + self._loop = loop self._websocket = self._loop.run_sync(lambda: websocket.websocket_connect(self.url)) self._graphson_reader = graphson_reader or GraphSONReader() self._graphson_writer = graphson_writer or GraphSONWriter() http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/4649c812/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 new file mode 100644 index 0000000..756ef90 --- /dev/null +++ b/gremlin-python/src/main/jython/tests/driver/test_driver_remote_connection_threaded.py @@ -0,0 +1,69 @@ +import sys +from queue import Queue +from threading import Thread + +import pytest + +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:8182/gremlin', 'g') + connection.close() +except: + skip = True + + [email protected](skip, reason='Gremlin Server is not running') +class TestDriverRemoteConnectionThreaded: + + def test_threaded_client(self): + q = 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_threaded_client_error(self): + q = Queue() + # This scenario fails because both threads try to access the main + # thread event loop - bad - each thread needs its own loop. + # This is what happens when you can't manually set the loop. + child = Thread(target=self._executor, args=(q, None)) + child2 = Thread(target=self._executor, args=(q, None)) + child.start() + child2.start() + with pytest.raises(RuntimeError): + try: + for x in range(2): + exc = q.get() + if issubclass(exc, Exception): + raise exc() + finally: + child.join() + child2.join() + + def _executor(self, q, loop): + try: + connection = DriverRemoteConnection( + 'ws://localhost:8182/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()
