You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by xi...@apache.org on 2023/02/03 00:31:02 UTC

[tinkerpop] branch master updated: Adds tests for per-request-settings in GLV's (#1938)

This is an automated email from the ASF dual-hosted git repository.

xiazcy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git


The following commit(s) were added to refs/heads/master by this push:
     new ccf27a9b83 Adds tests for per-request-settings in GLV's (#1938)
ccf27a9b83 is described below

commit ccf27a9b836ff1183b4c61a8156c586e06de91f6
Author: Cole Greer <11...@users.noreply.github.com>
AuthorDate: Thu Feb 2 16:30:55 2023 -0800

    Adds tests for per-request-settings in GLV's (#1938)
    
    Creates a test where a driver sends a request to gremlin-socket-server
    with all per-request settings set. gremlin-socket-server extracts these
    arguments and returns them in a string to the driver.
    
    Test is added in Java, .net, JS, and Python drivers.
    Go is excluded from this commit as the test is failing and needs
    additional work.
---
 .../GremlinClientBehaviorIntegrationTests.cs       | 22 ++++++++++++++++--
 .../Util/SocketServerSettings.cs                   |  8 +++++++
 .../WebSocketClientBehaviorIntegrateTest.java      | 23 +++++++++++++++++++
 .../test/integration/client-behavior-tests.js      | 11 ++++++++-
 .../driver/test_web_socket_client_behavior.py      | 26 +++++++++++++++++++++-
 .../conf/test-ws-gremlin.yaml                      |  5 +++++
 .../socket/server/SocketServerSettings.java        |  7 ++++++
 .../socket/server/TestWSGremlinInitializer.java    |  5 +++++
 8 files changed, 103 insertions(+), 4 deletions(-)

diff --git a/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Driver/GremlinClientBehaviorIntegrationTests.cs b/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Driver/GremlinClientBehaviorIntegrationTests.cs
index b6354d0409..7f5b957f9a 100644
--- a/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Driver/GremlinClientBehaviorIntegrationTests.cs
+++ b/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Driver/GremlinClientBehaviorIntegrationTests.cs
@@ -95,11 +95,11 @@ namespace Gremlin.Net.IntegrationTest.Driver
                 .OverrideRequestId(Settings.UserAgentRequestId).Create());
             Assert.Equal(Gremlin.Net.Process.Utils.UserAgent, userAgentResponse);
         }
-        
+
         [Fact]
         public async Task ShouldNotIncludeUserAgentInHandshakeRequestIfDisabled()
         {
-            var poolSettings = new ConnectionPoolSettings {EnableUserAgentOnConnect = false};
+            var poolSettings = new ConnectionPoolSettings { EnableUserAgentOnConnect = false };
 
             var gremlinServer = new GremlinServer(TestHost, Settings.Port);
             using var gremlinClient = new GremlinClient(gremlinServer, messageSerializer: Serializer,
@@ -110,5 +110,23 @@ namespace Gremlin.Net.IntegrationTest.Driver
                 .OverrideRequestId(Settings.UserAgentRequestId).Create());
             Assert.Equal("", userAgentResponse);
         }
+
+        [Fact]
+        public async Task ShouldSendPerRequestSettingsToServer()
+        {
+            var gremlinServer = new GremlinServer(TestHost, Settings.Port);
+            using var gremlinClient = new GremlinClient(gremlinServer, messageSerializer: Serializer);
+
+            //verify that new client reconnects and new requests can be made again
+            var response = await gremlinClient.SubmitWithSingleResultAsync<String>(RequestMessage.Build("1")
+                .OverrideRequestId(Settings.PerRequestSettingsRequestId)
+                .AddArgument(Tokens.ArgsEvalTimeout, 1234)
+                .AddArgument(Tokens.ArgsBatchSize, 12)
+                .AddArgument(Tokens.ArgsUserAgent, "helloWorld")
+                .Create());
+
+            var expectedResponse = $"requestId={Settings.PerRequestSettingsRequestId} evaluationTimeout=1234, batchSize=12, userAgent=helloWorld";
+            Assert.Equal(expectedResponse, response);
+        }
     }
 }
diff --git a/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Util/SocketServerSettings.cs b/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Util/SocketServerSettings.cs
index 1c28e47b1f..dae1e40782 100644
--- a/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Util/SocketServerSettings.cs
+++ b/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Util/SocketServerSettings.cs
@@ -77,6 +77,14 @@ public class SocketServerSettings
     [YamlMember(Alias = "USER_AGENT_REQUEST_ID", ApplyNamingConventions = false)]
     public Guid UserAgentRequestId { get; set; }
     
+    /**
+     * If a request with this ID comes to the server, the server responds with a string containing all overridden
+     * per request settings from the request message. String will be of the form
+     * "requestId=19436d9e-f8fc-4b67-8a76-deec60918424 evaluationTimeout=1234, batchSize=12, userAgent=testUserAgent"
+     */
+    [YamlMember(Alias = "PER_REQUEST_SETTINGS_REQUEST_ID", ApplyNamingConventions = false)]
+    public Guid PerRequestSettingsRequestId { get; set; }
+    
     public static SocketServerSettings FromYaml(String path)
     {
         var deserializer = new YamlDotNet.Serialization.DeserializerBuilder().Build();
diff --git a/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/WebSocketClientBehaviorIntegrateTest.java b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/WebSocketClientBehaviorIntegrateTest.java
index 53ed77c053..f3ba7b3ee4 100644
--- a/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/WebSocketClientBehaviorIntegrateTest.java
+++ b/gremlin-driver/src/test/java/org/apache/tinkerpop/gremlin/driver/WebSocketClientBehaviorIntegrateTest.java
@@ -517,4 +517,27 @@ public class WebSocketClientBehaviorIntegrateTest {
 
         cluster.close();
     }
+
+    /**
+     * Tests that client is correctly sending all overridable per request settings (requestId, batchSize,
+     * evaluationTimeout, and userAgent) to the server.
+     */
+    @Test
+    public void shouldSendPerRequestSettingsToServer() {
+        final Cluster cluster = Cluster.build("localhost").port(settings.PORT)
+                .minConnectionPoolSize(1)
+                .maxConnectionPoolSize(1)
+                .serializer(Serializers.GRAPHSON_V2D0)
+                .create();
+        final Client.ClusteredClient client = cluster.connect();
+
+        // trigger the testing server to return captured request settings
+        String response = client.submit("1", RequestOptions.build()
+                .overrideRequestId(settings.PER_REQUEST_SETTINGS_REQUEST_ID)
+                .timeout(1234).userAgent("helloWorld").batchSize(12).create()).one().getString();
+
+        String expectedResponse = String.format("requestId=%s evaluationTimeout=%d, batchSize=%d, userAgent=%s",
+                settings.PER_REQUEST_SETTINGS_REQUEST_ID, 1234, 12, "helloWorld");
+        assertEquals(expectedResponse, response);
+    }
 }
diff --git a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/integration/client-behavior-tests.js b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/integration/client-behavior-tests.js
index 55236e05ad..0530cca8aa 100644
--- a/gremlin-javascript/src/main/javascript/gremlin-javascript/test/integration/client-behavior-tests.js
+++ b/gremlin-javascript/src/main/javascript/gremlin-javascript/test/integration/client-behavior-tests.js
@@ -47,7 +47,6 @@ describe('Client', function () {
             assert.equal(connectionClosed, true);
 
             let result = await client.submit('1', null, {requestId: settings.SINGLE_VERTEX_REQUEST_ID})
-            console.log("result received: "+JSON.stringify(result));
             assert.ok(result);
         });
         it('should include user agent in handshake request', async function () {
@@ -64,5 +63,15 @@ describe('Client', function () {
 
             await noUserAgentClient.close();
         });
+        it('should send per request settings to server', async function () {
+            const resultSet = await client.submit('1', null, {
+                requestId: settings.PER_REQUEST_SETTINGS_REQUEST_ID,
+                evaluationTimeout: 1234,
+                batchSize: 12,
+                userAgent: "helloWorld"
+            })
+            const expectedResult = `requestId=${settings.PER_REQUEST_SETTINGS_REQUEST_ID} evaluationTimeout=1234, batchSize=12, userAgent=helloWorld`;
+            assert.equal(expectedResult, resultSet.first());
+        });
     });
 });
diff --git a/gremlin-python/src/main/python/tests/driver/test_web_socket_client_behavior.py b/gremlin-python/src/main/python/tests/driver/test_web_socket_client_behavior.py
index 7a3c1dac53..cd23f18b01 100644
--- a/gremlin-python/src/main/python/tests/driver/test_web_socket_client_behavior.py
+++ b/gremlin-python/src/main/python/tests/driver/test_web_socket_client_behavior.py
@@ -19,8 +19,10 @@
 
 __author__ = 'Cole Greer (cole@colegreer.ca)'
 
-import re
 
+import re
+import operator
+from functools import reduce
 from gremlin_python.driver import useragent
 
 
@@ -66,3 +68,25 @@ def test_should_not_include_user_agent_in_handshake_request_if_disabled(socket_s
     # If the gremlin user agent is disabled, the underlying web socket library reverts to sending its default user agent
     # during connection requests.
     assert re.search("^Python/(\d\.)*\d aiohttp/(\d\.)*\d$", user_agent_response)
+
+# Tests that client is correctly sending all overridable per request settings (requestId, batchSize,
+# evaluationTimeout, and userAgent) to the server.
+def test_should_send_per_request_settings_to_server(socket_server_client, socket_server_settings):
+
+    result = socket_server_client.submit(
+        "1", request_options={
+            'requestId': socket_server_settings["PER_REQUEST_SETTINGS_REQUEST_ID"],
+            'evaluationTimeout': 1234,
+            'batchSize': 12,
+            'userAgent': "helloWorld"
+        }).all().result()
+
+    expected_result = "requestId={} evaluationTimeout={}, batchSize={}, userAgent={}".format(
+        socket_server_settings["PER_REQUEST_SETTINGS_REQUEST_ID"], 1234, 12, "helloWorld"
+    )
+
+    # Socket Server is sending a simple string response which after being serialized in and out of graphBinary,
+    # becomes a list of length 1 strings. This operation folds the list back to a single string for comparison.
+    result = reduce(operator.add, result)
+
+    assert result == expected_result
diff --git a/gremlin-tools/gremlin-socket-server/conf/test-ws-gremlin.yaml b/gremlin-tools/gremlin-socket-server/conf/test-ws-gremlin.yaml
index 1b7686a73d..a358d4ce12 100644
--- a/gremlin-tools/gremlin-socket-server/conf/test-ws-gremlin.yaml
+++ b/gremlin-tools/gremlin-socket-server/conf/test-ws-gremlin.yaml
@@ -49,3 +49,8 @@ CLOSE_CONNECTION_REQUEST_ID_2: 3c4cf18a-c7f2-4dad-b9bf-5c701eb33000
 # If a request with this ID comes to the server, the server responds with the user agent (if any)
 # that was captured during the web socket handshake.
 USER_AGENT_REQUEST_ID: 20ad7bfb-4abf-d7f4-f9d3-9f1d55bee4ad
+
+# If a request with this ID comes to the server, the server responds with a string containing all overridden
+# per request settings from the request message. String will be of the form
+# "requestId=19436d9e-f8fc-4b67-8a76-deec60918424 evaluationTimeout=1234, batchSize=12, userAgent=testUserAgent"
+PER_REQUEST_SETTINGS_REQUEST_ID: 19436d9e-f8fc-4b67-8a76-deec60918424
diff --git a/gremlin-tools/gremlin-socket-server/src/main/java/org/apache/tinkerpop/gremlin/socket/server/SocketServerSettings.java b/gremlin-tools/gremlin-socket-server/src/main/java/org/apache/tinkerpop/gremlin/socket/server/SocketServerSettings.java
index d75d9f68fa..b186c83615 100644
--- a/gremlin-tools/gremlin-socket-server/src/main/java/org/apache/tinkerpop/gremlin/socket/server/SocketServerSettings.java
+++ b/gremlin-tools/gremlin-socket-server/src/main/java/org/apache/tinkerpop/gremlin/socket/server/SocketServerSettings.java
@@ -72,6 +72,13 @@ public class SocketServerSettings {
      */
     public UUID USER_AGENT_REQUEST_ID = null;
 
+    /**
+     * If a request with this ID comes to the server, the server responds with a string containing all overridden
+     * per request settings from the request message. String will be of the form
+     * "requestId=19436d9e-f8fc-4b67-8a76-deec60918424 evaluationTimeout=1234, batchSize=12, userAgent=testUserAgent"
+     */
+    public UUID PER_REQUEST_SETTINGS_REQUEST_ID = null;
+
     public static SocketServerSettings read(final Path confFilePath) throws IOException {
         return read(Files.newInputStream(confFilePath));
     }
diff --git a/gremlin-tools/gremlin-socket-server/src/main/java/org/apache/tinkerpop/gremlin/socket/server/TestWSGremlinInitializer.java b/gremlin-tools/gremlin-socket-server/src/main/java/org/apache/tinkerpop/gremlin/socket/server/TestWSGremlinInitializer.java
index 2f28799122..489d835a4f 100644
--- a/gremlin-tools/gremlin-socket-server/src/main/java/org/apache/tinkerpop/gremlin/socket/server/TestWSGremlinInitializer.java
+++ b/gremlin-tools/gremlin-socket-server/src/main/java/org/apache/tinkerpop/gremlin/socket/server/TestWSGremlinInitializer.java
@@ -137,6 +137,11 @@ public class TestWSGremlinInitializer extends TestChannelizers.TestWebSocketServ
                 ctx.channel().writeAndFlush(new CloseWebSocketFrame());
             } else if (msg.getRequestId().equals(settings.USER_AGENT_REQUEST_ID)) {
                 ctx.channel().writeAndFlush(new BinaryWebSocketFrame(returnSimpleBinaryResponse(settings.USER_AGENT_REQUEST_ID, userAgent)));
+            } else if (msg.getRequestId().equals(settings.PER_REQUEST_SETTINGS_REQUEST_ID)) {
+                String response = String.format("requestId=%s evaluationTimeout=%d, batchSize=%d, userAgent=%s",
+                        msg.getRequestId(), msg.getArgs().get("evaluationTimeout"),
+                        msg.getArgs().get("batchSize"), msg.getArgs().get("userAgent"));
+                ctx.channel().writeAndFlush(new BinaryWebSocketFrame(returnSimpleBinaryResponse(settings.PER_REQUEST_SETTINGS_REQUEST_ID, response)));
             } else {
                 try {
                     Thread.sleep(Long.parseLong((String) msg.getArgs().get("gremlin")));