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

xyz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/pulsar-client-cpp.git


The following commit(s) were added to refs/heads/main by this push:
     new 2108d54  Fix flaky testAcknowledgeCumulativeWithPartition (#214)
2108d54 is described below

commit 2108d547c788f0bc735e6fbcb32384e921e3520e
Author: Yunze Xu <xyzinfern...@163.com>
AuthorDate: Wed Mar 15 13:51:23 2023 +0800

    Fix flaky testAcknowledgeCumulativeWithPartition (#214)
    
    Fixes https://github.com/apache/pulsar-client-cpp/issues/213
    
    ### Motivation
    
    In `ConsumerTest.testAcknowledgeCumulativeWithPartition`, the last two
    `MessageId`s are acknowledged because they are treated as the latest two
    `MessageId`s of the two partitions. However, this assumption is false
    because `MultiTopicsConsumerImpl` does not guarantee receiving messages
    in a round robin way. Here is an example output when I added the debug
    logs for the `MessageId` received.
    
    ```
    (519,48,1,-1)
    (519,49,1,-1)
    (518,48,0,-1)
    (518,49,0,-1)
    ```
    
    We can see the last two `MessageId`s are both from partition 0.
    
    ### Modifications
    
    Use an array `latestMsgIds` to record the latest `MessageId` of a given
    partition in this test.
---
 tests/ConsumerTest.cc | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/tests/ConsumerTest.cc b/tests/ConsumerTest.cc
index 944b87b..b1329e4 100644
--- a/tests/ConsumerTest.cc
+++ b/tests/ConsumerTest.cc
@@ -19,6 +19,7 @@
 #include <gtest/gtest.h>
 #include <pulsar/Client.h>
 
+#include <array>
 #include <chrono>
 #include <ctime>
 #include <map>
@@ -291,14 +292,16 @@ TEST(ConsumerTest, 
testAcknowledgeCumulativeWithPartition) {
     }
 
     Message msg;
+    std::array<MessageId, 2> latestMsgIds;
     for (int i = 0; i < numMessages; i++) {
         ASSERT_EQ(ResultOk, consumer.receive(msg));
         // The last message of each partition topic be ACK
-        if (i >= numMessages - 2) {
-            consumer.acknowledgeCumulative(msg.getMessageId());
-        }
+        latestMsgIds[msg.getMessageId().partition()] = msg.getMessageId();
     }
     ASSERT_EQ(ResultTimeout, consumer.receive(msg, 2000));
+    for (auto&& msgId : latestMsgIds) {
+        consumer.acknowledgeCumulative(msgId);
+    }
 
     // Assert that there is no message in the tracker.
     auto multiConsumerImpl = 
PulsarFriend::getMultiTopicsConsumerImplPtr(consumer);

Reply via email to