@@ -35,13 +35,26 @@ enum class AckType
3535 CUMULATIVE
3636};
3737
38+ inline MessageIdList subMessageIdList (const MessageIdList& messageIdList,
39+ const std::vector<size_t >& indexes) {
40+ std::vector<MessageId> subMessageIdList;
41+ for (size_t index : indexes) {
42+ subMessageIdList.emplace_back (messageIdList.at (index));
43+ }
44+ return subMessageIdList;
45+ }
46+
3847class ConsumerWrapper {
3948 public:
40- void initialize (Client& client, const std::string& topic, const std::string& subscription) {
49+ void initialize (Client& client, const std::string& topic, const std::string& subscription,
50+ bool enableBatchIndexAck = false ) {
51+ client_ = &client;
52+ topic_ = topic;
53+ subscription_ = subscription;
4154 // Enable the stats for cumulative ack
42- ConsumerConfiguration conf ;
43- conf. setUnAckedMessagesTimeoutMs ( 10000 );
44- ASSERT_EQ (ResultOk, client. subscribe (topic, subscription, conf , consumer_));
55+ conf_. setUnAckedMessagesTimeoutMs ( 10000 ) ;
56+ conf_. setBatchIndexAckEnabled (enableBatchIndexAck );
57+ ASSERT_EQ (ResultOk, client_-> subscribe (topic_, subscription_, conf_ , consumer_));
4558 }
4659
4760 const std::vector<MessageId>& messageIdList () const noexcept { return messageIdList_; }
@@ -58,11 +71,8 @@ class ConsumerWrapper {
5871
5972 unsigned long getNumAcked (CommandAck_AckType ackType) const ;
6073
61- void acknowledgeAndRedeliver (const std::vector<size_t >& indexes, AckType ackType) {
62- std::vector<MessageId> msgIds;
63- for (size_t index : indexes) {
64- msgIds.emplace_back (messageIdList_.at (index));
65- }
74+ void acknowledge (const std::vector<size_t >& indexes, AckType ackType) {
75+ auto msgIds = subMessageIdList (messageIdList_, indexes);
6676 if (ackType == AckType::INDIVIDUAL_LIST ) {
6777 consumer_.acknowledge (msgIds);
6878 } else {
@@ -76,10 +86,27 @@ class ConsumerWrapper {
7686 }
7787 // Wait until the acknowledge command is sent
7888 std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
89+ }
90+
91+ void acknowledgeAndRedeliver (const std::vector<size_t >& indexes, AckType ackType) {
92+ acknowledge (indexes, ackType);
7993 consumer_.redeliverUnacknowledgedMessages ();
8094 }
8195
96+ // NOTE: Currently Pulsar broker doesn't support redelivery with batch index ACK well, so here we verify
97+ // the acknowledgment by restarting the consumer.
98+ void acknowledgeAndRestart (const std::vector<size_t >& indexes, AckType ackType) {
99+ acknowledge (indexes, ackType);
100+ messageIdList_.clear ();
101+ consumer_.close ();
102+ ASSERT_EQ (ResultOk, client_->subscribe (topic_, subscription_, conf_, consumer_));
103+ }
104+
82105 private:
106+ Client* client_;
107+ std::string topic_;
108+ std::string subscription_;
109+ ConsumerConfiguration conf_;
83110 Consumer consumer_;
84111 std::vector<MessageId> messageIdList_;
85112};
0 commit comments