Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 1 addition & 4 deletions lib/MessageBuilder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include "KeyValueImpl.h"
#include "LogUtils.h"
#include "MessageImpl.h"
#include "ObjectPool.h"
#include "PulsarApi.pb.h"
#include "SharedBuffer.h"
#include "TimeUtils.h"
Expand All @@ -37,9 +36,7 @@ using namespace pulsar;

namespace pulsar {

ObjectPool<MessageImpl, 100000> messagePool;

std::shared_ptr<MessageImpl> MessageBuilder::createMessageImpl() { return messagePool.create(); }
std::shared_ptr<MessageImpl> MessageBuilder::createMessageImpl() { return std::make_shared<MessageImpl>(); }

MessageBuilder::MessageBuilder() { impl_ = createMessageImpl(); }

Expand Down
23 changes: 23 additions & 0 deletions tests/MessageTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <pulsar/MessageBuilder.h>

#include <string>
#include <thread>
#include <vector>

#include "PulsarFriend.h"
#include "lib/MessageImpl.h"
Expand Down Expand Up @@ -104,6 +106,27 @@ TEST(MessageTest, testMessageBuilder) {
}
}

TEST(MessageTest, testMessageBuilderConcurrentBuild) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This test seems meaningless. Thread safety of concurrent shared_ptr creation is guaranteed by the standard library.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Check whether it will crash.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I mean, this is guaranteed by the standard library and our unit tests should only focus on the SDK side.

This does not like a regression test as well because the object pool is not used to avoid concurrent build crash.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

From LLM:

The test testMessageBuilderConcurrentBuild is not meaningful.

Each thread creates its own independent MessageBuilder instances — there is no shared mutable state between threads. The test is equivalent to running the existing sequential testMessageBuilder test in parallel 80,000 times, which doesn't validate any concurrency behavior specific to the code under test. std::make_shared (the replacement) is inherently thread-safe, and MessageBuilder methods operate on per-instance impl_ state with no global or shared data.

To be meaningful, the test would need to exercise a shared resource — for example, if it used a shared pool (the old ObjectPool) or shared metadata state. As written, it's just a stress test of the heap allocator, which isn't what the change is about.

From myself: testing concurrent creation of std::shared_ptr objects is not commonly seen. Such tests are meaningless, otherwise, we should also test concurrent creation of Producer, Consumer, etc.

const int threadCount = 8;
const int messagesPerThread = 10000;
std::vector<std::thread> threads;
threads.reserve(threadCount);

for (int i = 0; i < threadCount; i++) {
threads.emplace_back([i] {
for (int j = 0; j < messagesPerThread; j++) {
const std::string content = "message-" + std::to_string(i) + "-" + std::to_string(j);
auto msg = MessageBuilder().setContent(content).build();
ASSERT_EQ(content, msg.getDataAsString());
}
});
}

for (auto& thread : threads) {
thread.join();
}
}

TEST(MessageTest, testMessageImplKeyValuePayloadCovert) {
const char* keyContent = "keyContent";
const char* valueContent = "valueContent";
Expand Down
Loading