|
21 | 21 | #include <pulsar/Reader.h> |
22 | 22 | #include <time.h> |
23 | 23 |
|
| 24 | +#include <atomic> |
| 25 | +#include <functional> |
24 | 26 | #include <future> |
| 27 | +#include <set> |
25 | 28 | #include <string> |
26 | 29 | #include <thread> |
| 30 | +#include <vector> |
27 | 31 |
|
28 | 32 | #include "HttpHelper.h" |
29 | 33 | #include "PulsarFriend.h" |
@@ -110,15 +114,27 @@ TEST_P(ReaderTest, testAsyncRead) { |
110 | 114 | ASSERT_EQ(ResultOk, producer.send(msg)); |
111 | 115 | } |
112 | 116 |
|
| 117 | + // readNextAsync callbacks may complete in any order (e.g. with partitioned topic); collect all 10 then |
| 118 | + // verify set |
| 119 | + std::string received[10]; |
| 120 | + std::atomic<int> receivedCount{0}; |
113 | 121 | for (int i = 0; i < 10; i++) { |
114 | | - reader.readNextAsync([i](Result result, const Message& msg) { |
| 122 | + reader.readNextAsync([&](Result result, const Message& msg) { |
115 | 123 | ASSERT_EQ(ResultOk, result); |
116 | | - std::string content = msg.getDataAsString(); |
117 | | - std::string expected = "my-message-" + std::to_string(i); |
118 | | - ASSERT_EQ(expected, content); |
| 124 | + int idx = receivedCount.fetch_add(1); |
| 125 | + if (idx < 10) received[idx] = msg.getDataAsString(); |
119 | 126 | }); |
120 | 127 | } |
121 | 128 |
|
| 129 | + waitUntil( |
| 130 | + std::chrono::seconds(5), [&]() { return receivedCount.load() == 10; }, 1000); |
| 131 | + ASSERT_EQ(10, receivedCount.load()) << "Expected 10 messages"; |
| 132 | + |
| 133 | + std::set<std::string> receivedSet(received, received + 10); |
| 134 | + for (int i = 0; i < 10; i++) { |
| 135 | + ASSERT_TRUE(receivedSet.count("my-message-" + std::to_string(i))) << "Missing my-message-" << i; |
| 136 | + } |
| 137 | + |
122 | 138 | waitUntil( |
123 | 139 | std::chrono::seconds(5), |
124 | 140 | [&]() { |
|
0 commit comments