3232#include " HttpHelper.h"
3333#include " NoOpsCryptoKeyReader.h"
3434#include " PulsarFriend.h"
35+ #include " SynchronizedQueue.h"
36+ #include " WaitUtils.h"
3537#include " lib/ClientConnection.h"
3638#include " lib/Future.h"
3739#include " lib/LogUtils.h"
@@ -64,34 +66,13 @@ class ConsumerStateEventListener : public ConsumerEventListener {
6466 inActiveQueue_.push (partitionId);
6567 }
6668
67- std::queue <int > activeQueue_;
68- std::queue <int > inActiveQueue_;
69+ SynchronizedQueue <int > activeQueue_;
70+ SynchronizedQueue <int > inActiveQueue_;
6971 std::string name_;
7072};
7173
7274typedef std::shared_ptr<ConsumerStateEventListener> ConsumerStateEventListenerPtr;
7375
74- void verifyConsumerNotReceiveAnyStateChanges (ConsumerStateEventListenerPtr listener) {
75- ASSERT_EQ (0 , listener->activeQueue_ .size ());
76- ASSERT_EQ (0 , listener->inActiveQueue_ .size ());
77- }
78-
79- void verifyConsumerActive (ConsumerStateEventListenerPtr listener, int partitionId) {
80- ASSERT_NE (0 , listener->activeQueue_ .size ());
81- int pid = listener->activeQueue_ .front ();
82- listener->activeQueue_ .pop ();
83- ASSERT_EQ (partitionId, pid);
84- ASSERT_EQ (0 , listener->inActiveQueue_ .size ());
85- }
86-
87- void verifyConsumerInactive (ConsumerStateEventListenerPtr listener, int partitionId) {
88- ASSERT_NE (0 , listener->inActiveQueue_ .size ());
89- int pid = listener->inActiveQueue_ .front ();
90- listener->inActiveQueue_ .pop ();
91- ASSERT_EQ (partitionId, pid);
92- ASSERT_EQ (0 , listener->activeQueue_ .size ());
93- }
94-
9576class ActiveInactiveListenerEvent : public ConsumerEventListener {
9677 public:
9778 void becameActive (Consumer consumer, int partitionId) override {
@@ -119,9 +100,7 @@ TEST(ConsumerTest, testConsumerEventWithoutPartition) {
119100
120101 const std::string topicName = " testConsumerEventWithoutPartition-topic-" + std::to_string (time (nullptr ));
121102 const std::string subName = " sub" ;
122- const int waitTimeInMs = 1000 ;
123- // constexpr int unAckedMessagesTimeoutMs = 10000;
124- // constexpr int tickDurationInMs = 1000;
103+ const auto waitTime = std::chrono::seconds (3 );
125104
126105 // 1. two consumers on the same subscription
127106 Consumer consumer1;
@@ -132,7 +111,9 @@ TEST(ConsumerTest, testConsumerEventWithoutPartition) {
132111 config1.setConsumerType (ConsumerType::ConsumerFailover);
133112
134113 ASSERT_EQ (pulsar::ResultOk, client.subscribe (topicName, subName, config1, consumer1));
135- std::this_thread::sleep_for (std::chrono::milliseconds (waitTimeInMs * 2 ));
114+ waitUntil (waitTime, [&listener1]() -> bool { return listener1->activeQueue_ .size () == 1 ; });
115+ ASSERT_EQ (listener1->activeQueue_ .size (), 1 );
116+ ASSERT_EQ (listener1->activeQueue_ .pop (), -1 );
136117
137118 Consumer consumer2;
138119 ConsumerConfiguration config2;
@@ -142,18 +123,22 @@ TEST(ConsumerTest, testConsumerEventWithoutPartition) {
142123 config2.setConsumerType (ConsumerType::ConsumerFailover);
143124
144125 ASSERT_EQ (pulsar::ResultOk, client.subscribe (topicName, subName, config2, consumer2));
145- std::this_thread::sleep_for (std::chrono::milliseconds (waitTimeInMs * 2 ));
146-
147- verifyConsumerActive (listener1, -1 );
148- verifyConsumerInactive (listener2, -1 );
149-
150- // clear inActiveQueue_
151- std::queue<int >().swap (listener2->inActiveQueue_ );
126+ // Since https://github.com/apache/pulsar/pull/19502, both consumer and consumer2 could receive the
127+ // inactive event
128+ waitUntil (waitTime, [&listener1, &listener2]() -> bool {
129+ return listener1->inActiveQueue_ .size () == 1 || listener2->inActiveQueue_ .size () == 1 ;
130+ });
131+ if (listener1->inActiveQueue_ .size () == 1 ) {
132+ ASSERT_EQ (listener1->inActiveQueue_ .pop (), -1 );
133+ } else {
134+ ASSERT_EQ (listener2->inActiveQueue_ .size (), 1 );
135+ ASSERT_EQ (listener2->inActiveQueue_ .pop (), -1 );
136+ }
152137
153138 consumer1.close ();
154- std::this_thread::sleep_for ( std::chrono::milliseconds (waitTimeInMs * 2 ) );
155- verifyConsumerActive (listener2, - 1 );
156- verifyConsumerNotReceiveAnyStateChanges (listener1 );
139+ waitUntil (waitTime, [&listener2]() -> bool { return listener2-> activeQueue_ . size () == 1 ; } );
140+ ASSERT_EQ (listener2-> activeQueue_ . size (), 1 );
141+ ASSERT_EQ (listener2-> activeQueue_ . pop (), - 1 );
157142}
158143
159144TEST (ConsumerTest, testConsumerEventWithPartition) {
0 commit comments