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 09bc72ee PROTON-2705 Ensure tracker state is in sync with future 
completion
09bc72ee is described below

commit 09bc72ee41ddc2b9fceb27d36e40d070b7ef31ac
Author: Timothy Bish <tabish...@gmail.com>
AuthorDate: Tue Apr 4 19:20:34 2023 -0400

    PROTON-2705 Ensure tracker state is in sync with future completion
    
    Ensure that remote settlement state is correct when the settlement
    future is completed.
---
 .../qpid/protonj2/client/impl/ClientTrackable.java | 19 +++---
 .../protonj2/client/impl/ClientTrackerTest.java    | 69 ++++++++++++++++++++++
 .../qpid/protonj2/client/impl/SenderTest.java      | 45 ++++++++++++++
 3 files changed, 125 insertions(+), 8 deletions(-)

diff --git 
a/protonj2-client/src/main/java/org/apache/qpid/protonj2/client/impl/ClientTrackable.java
 
b/protonj2-client/src/main/java/org/apache/qpid/protonj2/client/impl/ClientTrackable.java
index f9932713..3d41c6ea 100644
--- 
a/protonj2-client/src/main/java/org/apache/qpid/protonj2/client/impl/ClientTrackable.java
+++ 
b/protonj2-client/src/main/java/org/apache/qpid/protonj2/client/impl/ClientTrackable.java
@@ -17,6 +17,7 @@
 
 package org.apache.qpid.protonj2.client.impl;
 
+import java.util.Objects;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
@@ -61,6 +62,8 @@ public abstract class ClientTrackable<SenderType extends 
ClientSenderLinkType<?>
      *      The proton outgoing delivery object that backs this tracker.
      */
     ClientTrackable(SenderType sender, OutgoingDelivery delivery) {
+        Objects.requireNonNull(sender, "Sender cannot be null for a Tracker");
+
         this.sender = sender;
         this.delivery = delivery;
         
this.delivery.deliveryStateUpdatedHandler(this::processDeliveryUpdated);
@@ -125,10 +128,10 @@ public abstract class ClientTrackable<SenderType extends 
ClientSenderLinkType<?>
             if (remoteSettlementFuture == null) {
                 remoteSettlementFuture = 
sender.session.connection().getFutureFactory().createFuture();
             }
-        }
 
-        if (delivery.isSettled() || delivery.isRemotelySettled()) {
-            remoteSettlementFuture.complete(self());
+            if (delivery.isSettled() || remoteSettled()) {
+                remoteSettlementFuture.complete(self());
+            }
         }
 
         return remoteSettlementFuture;
@@ -212,18 +215,18 @@ public abstract class ClientTrackable<SenderType extends 
ClientSenderLinkType<?>
 
     private void processDeliveryUpdated(OutgoingDelivery delivery) {
         if (delivery.isRemotelySettled()) {
-            if (sender.options.autoSettle()) {
-                delivery.settle();
-            }
-
             synchronized (this) {
-                REMOTELY_SETTLED_UPDATER.lazySet(this, 1);
                 REMOTEL_DELIVERY_STATE_UPDATER.lazySet(this, 
ClientDeliveryState.fromProtonType(delivery.getRemoteState()));
+                REMOTELY_SETTLED_UPDATER.lazySet(this, 1);
 
                 if (remoteSettlementFuture != null) {
                     remoteSettlementFuture.complete(self());
                 }
             }
+
+            if (sender.options.autoSettle()) {
+                delivery.settle();
+            }
         } else {
             REMOTEL_DELIVERY_STATE_UPDATER.set(this, 
ClientDeliveryState.fromProtonType(delivery.getRemoteState()));
         }
diff --git 
a/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/impl/ClientTrackerTest.java
 
b/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/impl/ClientTrackerTest.java
new file mode 100644
index 00000000..42319e02
--- /dev/null
+++ 
b/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/impl/ClientTrackerTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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.
+ */
+
+package org.apache.qpid.protonj2.client.impl;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.apache.qpid.protonj2.engine.EventHandler;
+import org.apache.qpid.protonj2.engine.OutgoingDelivery;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mockito;
+
+/**
+ * Test the behavior of {@link ClientTracker}
+ */
+@Timeout(20)
+public class ClientTrackerTest {
+
+    @Test
+    public void testCreateTrackerWithNullArgs() {
+        assertThrows(NullPointerException.class, () -> new ClientTracker(null, 
null));
+    }
+
+    @Test
+    public void testCreateTrackerWithNullDelivery() {
+        final ClientSender sender = Mockito.mock(ClientSender.class);
+
+        assertThrows(NullPointerException.class, () -> new 
ClientTracker(sender, null));
+    }
+
+    @Test
+    public void testCreateTrackerWithNullSender() {
+        final OutgoingDelivery delivery = Mockito.mock(OutgoingDelivery.class);
+
+        assertThrows(NullPointerException.class, () -> new ClientTracker(null, 
delivery));
+    }
+
+    @Test
+    public void testCreateTrackerAndCompleteSettlement() {
+        final ClientSender sender = Mockito.mock(ClientSender.class);
+        final OutgoingDelivery delivery = Mockito.mock(OutgoingDelivery.class);
+
+        assertDoesNotThrow(() -> new ClientTracker(sender, delivery));
+
+        @SuppressWarnings("unchecked")
+        final ArgumentCaptor<EventHandler<OutgoingDelivery>> 
deliveryUpdaterCapture = ArgumentCaptor.forClass(EventHandler.class);
+        
Mockito.verify(delivery).deliveryStateUpdatedHandler(deliveryUpdaterCapture.capture());
+
+        assertNotNull(deliveryUpdaterCapture.getValue());
+    }
+}
diff --git 
a/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/impl/SenderTest.java
 
b/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/impl/SenderTest.java
index 02f97dd6..24567aad 100644
--- 
a/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/impl/SenderTest.java
+++ 
b/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/impl/SenderTest.java
@@ -82,6 +82,7 @@ import 
org.apache.qpid.protonj2.types.transport.ReceiverSettleMode;
 import org.apache.qpid.protonj2.types.transport.Role;
 import org.apache.qpid.protonj2.types.transport.SenderSettleMode;
 import org.hamcrest.Matchers;
+import org.junit.jupiter.api.RepeatedTest;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.Timeout;
 import org.slf4j.Logger;
@@ -2667,4 +2668,48 @@ public class SenderTest extends ImperativeClientTestCase 
{
             peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
         }
     }
+
+    @RepeatedTest(2)
+    public void testAwaitSettlementAndCheckRemoteState() throws Exception {
+        try (ProtonTestServer peer = new ProtonTestServer()) {
+            peer.expectSASLAnonymousConnect();
+            peer.expectOpen().respond();
+            peer.expectBegin().respond();
+            
peer.expectAttach().ofSender().withTarget().withAddress("test").and().respond();
+            peer.remoteFlow().withLinkCredit(5).queue();
+            peer.expectTransfer().accept();
+            peer.expectTransfer().accept();
+            peer.expectTransfer().accept();
+            peer.expectTransfer().accept();
+            peer.expectTransfer().accept();
+            peer.start();
+
+            URI remoteURI = peer.getServerURI();
+
+            Message<String> message = Message.create("test-message");
+            Client container = Client.create();
+            Connection connection = container.connect(remoteURI.getHost(), 
remoteURI.getPort());
+            Session session = connection.openSession();
+            Sender sender = session.openSender("test");
+
+            final Tracker tracker1 = sender.send(message);
+            final Tracker tracker2 = sender.send(message);
+            final Tracker tracker3 = sender.send(message);
+            final Tracker tracker4 = sender.send(message);
+            final Tracker tracker5 = sender.send(message);
+
+            assertEquals(ClientDeliveryState.ClientAccepted.getInstance() 
,tracker1.awaitSettlement().remoteState());
+            assertEquals(ClientDeliveryState.ClientAccepted.getInstance() 
,tracker2.awaitSettlement().remoteState());
+            assertEquals(ClientDeliveryState.ClientAccepted.getInstance() 
,tracker3.awaitSettlement().remoteState());
+            assertEquals(ClientDeliveryState.ClientAccepted.getInstance() 
,tracker4.awaitSettlement().remoteState());
+            assertEquals(ClientDeliveryState.ClientAccepted.getInstance() 
,tracker5.awaitSettlement().remoteState());
+            assertTrue(tracker1.remoteSettled());
+            assertTrue(tracker2.remoteSettled());
+            assertTrue(tracker3.remoteSettled());
+            assertTrue(tracker4.remoteSettled());
+            assertTrue(tracker5.remoteSettled());
+
+            peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+        }
+    }
 }


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

Reply via email to