Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pulsar-client-cpp/lib/MultiTopicsConsumerImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ void MultiTopicsConsumerImpl::messageReceived(Consumer consumer, const Message&
}
messages_.push(msg);
if (messageListener_) {
unAckedMessageTrackerPtr_->add(msg.getMessageId());
listenerExecutor_->postWork(
std::bind(&MultiTopicsConsumerImpl::internalListener, shared_from_this(), consumer));
}
Expand Down
49 changes: 48 additions & 1 deletion pulsar-client-cpp/tests/BasicEndToEndTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include <lib/PatternMultiTopicsConsumerImpl.h>
#include "lib/Future.h"
#include "lib/Utils.h"

#include <unistd.h>
#include <functional>

DECLARE_LOG_OBJECT()
Expand Down Expand Up @@ -2940,3 +2940,50 @@ TEST(BasicEndToEndTest, testPreventDupConsumersAllowSameSubForDifferentTopics) {
// consumer C should be a different instance from A and B and should be with open state.
ASSERT_EQ(ResultOk, consumerC.close());
}

static long regexTestMessagesReceived = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gets updated from one thread and read from another. We should use std::atomic<long>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can but does it really matter since its only one writer? Also we doing the same thing in a couple places:
https://github.com/apache/pulsar/blob/master/pulsar-client-cpp/tests/BasicEndToEndTest.cc#L47

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the number will be correct because of the single writer, but the reader is technically not guaranteed to see the latest value. Though, yes, with sleep and retries it would settle anyway.


static void regexMessageListenerFunction(Consumer consumer, const Message &msg) {
regexTestMessagesReceived++;
}

TEST(BasicEndToEndTest, testRegexTopicsWithMessageListener) {
ClientConfiguration config;
Client client(lookupUrl);
long unAckedMessagesTimeoutMs = 10000;
std::string subsName = "testRegexTopicsWithMessageListener-sub";
std::string pattern =
"persistent://public/default/testRegexTopicsWithMessageListenerTopic-.*";
ConsumerConfiguration consumerConf;
consumerConf.setConsumerType(ConsumerShared);
consumerConf.setMessageListener(
std::bind(regexMessageListenerFunction, std::placeholders::_1, std::placeholders::_2));
consumerConf.setUnAckedMessagesTimeoutMs(unAckedMessagesTimeoutMs);

Producer producer;
ProducerConfiguration producerConf;
Result result = client.createProducer("persistent://public/default/testRegexTopicsWithMessageListenerTopic-1", producerConf, producer);
ASSERT_EQ(ResultOk, result);

Consumer consumer;
result = client.subscribeWithRegex(pattern, subsName, consumerConf, consumer);
ASSERT_EQ(ResultOk, result);
ASSERT_EQ(consumer.getSubscriptionName(), subsName);

for (int i = 0; i < 10; i++) {
Message msg = MessageBuilder().setContent("test-" + std::to_string(i)).build();
producer.sendAsync(msg, nullptr);
}

producer.flush();
long timeWaited = 0;
while(true) {
// maximum wait time
ASSERT_LE(timeWaited, unAckedMessagesTimeoutMs * 1000 * 3);
if (regexTestMessagesReceived >= 10 * 2) {
break;
}
usleep(500000);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unistd.h is only posix specific. std::this_thread::sleep is the preferred c++ 11 way to sleep

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok will update

timeWaited += 500000;
}
}