You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by ta...@apache.org on 2024/03/14 19:56:19 UTC

(qpid-protonj2) branch main updated: PROTON-2804 Send half the idle timeout value in ConnectionOptions

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

tabish pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/qpid-protonj2.git


The following commit(s) were added to refs/heads/main by this push:
     new 3593a3fc PROTON-2804 Send half the idle timeout value in ConnectionOptions
3593a3fc is described below

commit 3593a3fc679fd2f210754ae4659ea2b5b2accdff
Author: Timothy Bish <ta...@gmail.com>
AuthorDate: Thu Mar 14 15:51:39 2024 -0400

    PROTON-2804 Send half the idle timeout value in ConnectionOptions
    
    The ConnectionOptions API docs state the idle timeout value will be
    halved when sent in the open frame but this is not being done currently.
    Fix and test this to ensure it is handled and check the range as the
    value is a UInt ranged setting.
---
 .../qpid/protonj2/client/ConnectionOptions.java    |  3 +-
 .../protonj2/client/impl/ClientConnection.java     |  2 +-
 .../protonj2/client/ConnectionOptionsTest.java     |  9 +++++
 .../qpid/protonj2/client/impl/ConnectionTest.java  | 38 ++++++++++++++++++++++
 4 files changed, 50 insertions(+), 2 deletions(-)

diff --git a/protonj2-client/src/main/java/org/apache/qpid/protonj2/client/ConnectionOptions.java b/protonj2-client/src/main/java/org/apache/qpid/protonj2/client/ConnectionOptions.java
index c37e2990..04428410 100644
--- a/protonj2-client/src/main/java/org/apache/qpid/protonj2/client/ConnectionOptions.java
+++ b/protonj2-client/src/main/java/org/apache/qpid/protonj2/client/ConnectionOptions.java
@@ -26,6 +26,7 @@ import java.util.function.BiConsumer;
 
 import org.apache.qpid.protonj2.client.exceptions.ClientOperationTimedOutException;
 import org.apache.qpid.protonj2.client.exceptions.ClientSendTimedOutException;
+import org.apache.qpid.protonj2.types.UnsignedInteger;
 import org.apache.qpid.protonj2.types.transport.Open;
 
 /**
@@ -388,7 +389,7 @@ public class ConnectionOptions implements Cloneable {
      * @return this {@link ConnectionOptions} instance.
      */
     public ConnectionOptions idleTimeout(long timeout, TimeUnit units) {
-        this.idleTimeout = units.toMillis(timeout);
+        this.idleTimeout = UnsignedInteger.valueOf(units.toMillis(timeout)).longValue();;
         return this;
     }
 
diff --git a/protonj2-client/src/main/java/org/apache/qpid/protonj2/client/impl/ClientConnection.java b/protonj2-client/src/main/java/org/apache/qpid/protonj2/client/impl/ClientConnection.java
index 97ae1d67..a3057e51 100644
--- a/protonj2-client/src/main/java/org/apache/qpid/protonj2/client/impl/ClientConnection.java
+++ b/protonj2-client/src/main/java/org/apache/qpid/protonj2/client/impl/ClientConnection.java
@@ -895,7 +895,7 @@ public final class ClientConnection implements Connection {
         protonConnection.setLinkedResource(this);
         protonConnection.setChannelMax(options.channelMax());
         protonConnection.setMaxFrameSize(options.maxFrameSize());
-        protonConnection.setIdleTimeout((int) options.idleTimeout());
+        protonConnection.setIdleTimeout(options.idleTimeout() / 2);
         protonConnection.setOfferedCapabilities(ClientConversionSupport.toSymbolArray(options.offeredCapabilities()));
         protonConnection.setDesiredCapabilities(ClientConversionSupport.toSymbolArray(options.desiredCapabilities()));
         protonConnection.setProperties(ClientConversionSupport.toSymbolKeyedMap(options.properties()));
diff --git a/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/ConnectionOptionsTest.java b/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/ConnectionOptionsTest.java
index 3edda0c5..3b6f7af8 100644
--- a/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/ConnectionOptionsTest.java
+++ b/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/ConnectionOptionsTest.java
@@ -20,10 +20,12 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotSame;
 import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.qpid.protonj2.types.UnsignedInteger;
 import org.junit.jupiter.api.Test;
 
 public class ConnectionOptionsTest {
@@ -37,6 +39,13 @@ public class ConnectionOptionsTest {
         assertNull(options.virtualHost());
     }
 
+    @Test
+    public void testSetIdleTimeoutValidesUIntRange() {
+        ConnectionOptions options = new ConnectionOptions();
+
+        assertThrows(NumberFormatException.class, () -> options.idleTimeout(UnsignedInteger.MAX_VALUE.longValue() + 1));
+    }
+
     @Test
     public void testCreateDefaultsTimeouts() {
         ConnectionOptions options = new ConnectionOptions();
diff --git a/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/impl/ConnectionTest.java b/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/impl/ConnectionTest.java
index 44a69b9d..e6915238 100644
--- a/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/impl/ConnectionTest.java
+++ b/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/impl/ConnectionTest.java
@@ -58,6 +58,7 @@ import org.apache.qpid.protonj2.test.driver.codec.messaging.TerminusDurability;
 import org.apache.qpid.protonj2.test.driver.codec.messaging.TerminusExpiryPolicy;
 import org.apache.qpid.protonj2.test.driver.codec.security.SaslCode;
 import org.apache.qpid.protonj2.test.driver.matchers.messaging.SourceMatcher;
+import org.apache.qpid.protonj2.types.UnsignedInteger;
 import org.apache.qpid.protonj2.types.messaging.AmqpValue;
 import org.apache.qpid.protonj2.types.transport.AMQPHeader;
 import org.apache.qpid.protonj2.types.transport.AmqpError;
@@ -1827,6 +1828,43 @@ public class ConnectionTest extends ImperativeClientTestCase {
         }
     }
 
+    @Test
+    public void testCreateConnectionWithNoIdleTimeout() throws Exception {
+        doTestCreateConnectionWithIdleTimeoutSendsExpectedValue(0, 0);
+    }
+
+    @Test
+    public void testCreateConnectionWithHalfIdleTimeout() throws Exception {
+        doTestCreateConnectionWithIdleTimeoutSendsExpectedValue(10_000, 5_000);
+    }
+
+    @Test
+    public void testCreateConnectionWithHalfLargeIdleTimeout() throws Exception {
+        doTestCreateConnectionWithIdleTimeoutSendsExpectedValue(UnsignedInteger.MAX_VALUE.longValue(), UnsignedInteger.MAX_VALUE.longValue() / 2);
+    }
+
+    private void doTestCreateConnectionWithIdleTimeoutSendsExpectedValue(long setValue, long expectedValue) throws Exception {
+        try (ProtonTestServer peer = new ProtonTestServer(testServerOptions())) {
+            peer.expectSASLAnonymousConnect();
+            peer.expectOpen().withIdleTimeOut(expectedValue).respond();
+            peer.expectClose().respond();
+            peer.start();
+
+            URI remoteURI = peer.getServerURI();
+
+            LOG.info("Connect test started, peer listening on: {}", remoteURI);
+
+            Client container = Client.create();
+            ConnectionOptions options = connectionOptions().idleTimeout(setValue);
+            Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort(), options);
+
+            connection.openFuture().get(10, TimeUnit.SECONDS);
+            connection.closeAsync().get(10, TimeUnit.SECONDS);
+
+            peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+        }
+    }
+
     @Disabled("Disabled due to requirement of hard coded port")
     @Test
     public void testLocalPortOption() throws Exception {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org