From f0bbe7c90f5f66028203f21dcf720bf0948f1950 Mon Sep 17 00:00:00 2001 From: davebshow Date: Wed, 24 Jan 2018 13:14:03 -0800 Subject: [PATCH] added proper response message serialization, run all remote connection tests with graphson 2 and 3 --- .../jython/gremlin_python/driver/protocol.py | 32 +++++++------------ .../src/main/jython/tests/conftest.py | 12 ++++--- .../main/jython/tests/driver/test_client.py | 11 ++++--- .../driver/test_driver_remote_connection.py | 4 +-- 4 files changed, 28 insertions(+), 31 deletions(-) diff --git a/gremlin-python/src/main/jython/gremlin_python/driver/protocol.py b/gremlin-python/src/main/jython/gremlin_python/driver/protocol.py index 75c99bcf6d0..1330483ad40 100644 --- a/gremlin-python/src/main/jython/gremlin_python/driver/protocol.py +++ b/gremlin-python/src/main/jython/gremlin_python/driver/protocol.py @@ -65,12 +65,13 @@ def write(self, request_id, request_message): request_id, request_message) self._transport.write(message) - def data_received(self, data, results_dict): - data = json.loads(data.decode('utf-8')) - request_id = data['requestId'] + def data_received(self, message, results_dict): + message = self._message_serializer.deserialize_message(json.loads(message.decode('utf-8'))) + request_id = message['requestId'] result_set = results_dict[request_id] - status_code = data['status']['code'] - aggregate_to = data['result']['meta'].get('aggregateTo', 'list') + status_code = message['status']['code'] + aggregate_to = message['result']['meta'].get('aggregateTo', 'list') + data = message['result']['data'] result_set.aggregate_to = aggregate_to if status_code == 407: auth = b''.join([b'\x00', self._username.encode('utf-8'), @@ -79,28 +80,19 @@ def data_received(self, data, results_dict): 'traversal', 'authentication', {'sasl': base64.b64encode(auth).decode()}) self.write(request_id, request_message) - data = self._transport.read() - self.data_received(data, results_dict) + message = self._transport.read() + self.data_received(message, results_dict) elif status_code == 204: result_set.stream.put_nowait([]) del results_dict[request_id] elif status_code in [200, 206]: - results = [] - # this is a bit of a hack for now. basically the protocol.py picks the json apart and doesn't - # account for types too well right now. - if self._message_serializer.version == b"application/vnd.gremlin-v2.0+json": - for msg in data["result"]["data"]: - results.append( - self._message_serializer.deserialize_message(msg)) - else: - results = self._message_serializer.deserialize_message(data["result"]["data"]["@value"]) - result_set.stream.put_nowait(results) + result_set.stream.put_nowait(data) if status_code == 206: - data = self._transport.read() - self.data_received(data, results_dict) + message = self._transport.read() + self.data_received(message, results_dict) else: del results_dict[request_id] else: del results_dict[request_id] raise GremlinServerError( - "{0}: {1}".format(status_code, data["status"]["message"])) + "{0}: {1}".format(status_code, message["status"]["message"])) diff --git a/gremlin-python/src/main/jython/tests/conftest.py b/gremlin-python/src/main/jython/tests/conftest.py index 6fcb8dbbd13..9b12180a816 100644 --- a/gremlin-python/src/main/jython/tests/conftest.py +++ b/gremlin-python/src/main/jython/tests/conftest.py @@ -1,4 +1,4 @@ -''' +""" 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 @@ -15,7 +15,7 @@ KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -''' +""" import concurrent.futures import pytest @@ -65,10 +65,14 @@ def fin(): request.addfinalizer(fin) return client -@pytest.fixture +@pytest.fixture(params=['v2', 'v3']) def remote_connection(request): try: - remote_conn = DriverRemoteConnection('ws://localhost:45940/gremlin', 'g') + if request.param == 'v2': + remote_conn = DriverRemoteConnection('ws://localhost:45940/gremlin', 'g', + message_serializer=serializer.GraphSONSerializersV2d0()) + else: + remote_conn = DriverRemoteConnection('ws://localhost:45940/gremlin', 'g') except OSError: pytest.skip('Gremlin Server is not running') else: diff --git a/gremlin-python/src/main/jython/tests/driver/test_client.py b/gremlin-python/src/main/jython/tests/driver/test_client.py index 7a6f3b4ed5c..c804bd1683f 100644 --- a/gremlin-python/src/main/jython/tests/driver/test_client.py +++ b/gremlin-python/src/main/jython/tests/driver/test_client.py @@ -79,12 +79,13 @@ def test_client_async(client): def test_connection_share(client): # Overwrite fixture with pool_size=1 client - client = Client('ws://localhost:45940/gremlin', 'g', pool_size=1, message_serializer=serializer.GraphSONSerializersV2d0()) + client = Client('ws://localhost:45940/gremlin', 'g', pool_size=1) g = Graph().traversal() t = g.V() message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode}) + message2 = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode}) future = client.submitAsync(message) - future2 = client.submitAsync(message) + future2 = client.submitAsync(message2) result_set2 = future2.result() assert len(result_set2.all().result()) == 6 @@ -99,10 +100,10 @@ def test_multi_conn_pool(client): g = Graph().traversal() t = g.V() message = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode}) - - client = Client('ws://localhost:45940/gremlin', 'g', pool_size=1, message_serializer=serializer.GraphSONSerializersV2d0()) + message2 = RequestMessage('traversal', 'bytecode', {'gremlin': t.bytecode}) + client = Client('ws://localhost:45940/gremlin', 'g', pool_size=1) future = client.submitAsync(message) - future2 = client.submitAsync(message) + future2 = client.submitAsync(message2) result_set2 = future2.result() assert len(result_set2.all().result()) == 6 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 5471637774c..1071493d264 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 @@ -119,10 +119,10 @@ def test_strategies(self, remote_connection): assert 6 == g.V().count().next() assert 6 == g.E().count().next() - def test_side_effects(self, remote_connection_v2): + def test_side_effects(self, remote_connection): statics.load_statics(globals()) # - g = Graph().traversal().withRemote(remote_connection_v2) + g = Graph().traversal().withRemote(remote_connection) ### t = g.V().hasLabel("project").name.iterate() assert 0 == len(t.side_effects.keys())