Skip to content

Commit dd1481b

Browse files
[flaky-test] Fix very flaky tests for TEST_P (#59)
Fixes #58 #24 ### Motivation gtest-parallel runs tests in different processes, not threads. So the topic name could be the same even if it has the timestamp suffix. Then `ConsumerBusy` error would occur. ### Modifications In each `TEST_P` method, convert `GetParam()` to a unique string to avoid topic conflict.
1 parent 55b4bc9 commit dd1481b

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

tests/ProducerTest.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ class ProducerTest : public ::testing::TestWithParam<bool> {};
219219
TEST_P(ProducerTest, testMaxMessageSize) {
220220
Client client(serviceUrl);
221221

222-
const std::string topic = "ProducerTest-NoBatchMaxMessageSize-" + std::to_string(time(nullptr));
222+
const auto topic = std::string("ProducerTest-NoBatchMaxMessageSize-") +
223+
(GetParam() ? "batch-" : "-no-batch-") + std::to_string(time(nullptr));
223224

224225
Consumer consumer;
225226
ASSERT_EQ(ResultOk, client.subscribe(topic, "sub", consumer));
@@ -247,7 +248,8 @@ TEST_P(ProducerTest, testMaxMessageSize) {
247248
TEST_P(ProducerTest, testChunkingMaxMessageSize) {
248249
Client client(serviceUrl);
249250

250-
const std::string topic = "ProducerTest-ChunkingMaxMessageSize-" + std::to_string(time(nullptr));
251+
const auto topic = std::string("ProducerTest-ChunkingMaxMessageSize-") +
252+
(GetParam() ? "batch-" : "no-batch-") + std::to_string(time(nullptr));
251253

252254
Consumer consumer;
253255
ASSERT_EQ(ResultOk, client.subscribe(topic, "sub", consumer));

tests/ShutdownTest.cc

+31-23
Original file line numberDiff line numberDiff line change
@@ -35,60 +35,66 @@ enum class EndToEndType : uint8_t
3535
REGEX_TOPICS
3636
};
3737

38-
class ShutdownTest : public ::testing::TestWithParam<EndToEndType> {
39-
public:
40-
void SetUp() override {
41-
topic_ = topic_ + std::to_string(id_++) + "-" + std::to_string(time(nullptr));
42-
if (GetParam() != EndToEndType::SINGLE_TOPIC) {
43-
int res = makePutRequest(
44-
"http://localhost:8080/admin/v2/persistent/public/default/" + topic_ + "/partitions", "2");
45-
ASSERT_TRUE(res == 204 || res == 409) << "res: " << res;
46-
}
38+
static std::string toString(EndToEndType endToEndType) {
39+
switch (endToEndType) {
40+
case EndToEndType::SINGLE_TOPIC:
41+
return "single-topic";
42+
case EndToEndType::MULTI_TOPICS:
43+
return "multi-topics";
44+
case EndToEndType::REGEX_TOPICS:
45+
return "regex-topics";
46+
default:
47+
return "???";
4748
}
49+
}
4850

51+
class ShutdownTest : public ::testing::TestWithParam<EndToEndType> {
4952
protected:
5053
Client client_{lookupUrl};
5154
decltype(PulsarFriend::getProducers(client_)) producers_{PulsarFriend::getProducers(client_)};
5255
decltype(PulsarFriend::getConsumers(client_)) consumers_{PulsarFriend::getConsumers(client_)};
53-
std::string topic_ = "shutdown-test-";
5456

55-
static std::atomic_int id_;
57+
void createPartitionedTopic(const std::string& topic) {
58+
if (GetParam() != EndToEndType::SINGLE_TOPIC) {
59+
int res = makePutRequest(
60+
"http://localhost:8080/admin/v2/persistent/public/default/" + topic + "/partitions", "2");
61+
ASSERT_TRUE(res == 204 || res == 409) << "res: " << res;
62+
}
63+
}
5664

57-
Result subscribe(Consumer &consumer) {
65+
Result subscribe(Consumer& consumer, const std::string& topic) {
5866
if (GetParam() == EndToEndType::REGEX_TOPICS) {
5967
// NOTE: Currently the regex subscription requires the complete namespace prefix
60-
return client_.subscribeWithRegex("persistent://public/default/" + topic_ + ".*", "sub",
61-
consumer);
68+
return client_.subscribeWithRegex("persistent://public/default/" + topic + ".*", "sub", consumer);
6269
} else {
63-
return client_.subscribe(topic_, "sub", consumer);
70+
return client_.subscribe(topic, "sub", consumer);
6471
}
6572
}
6673

6774
void assertConnectionsEmpty() {
6875
auto connections = PulsarFriend::getConnections(client_);
69-
for (const auto &cnx : PulsarFriend::getConnections(client_)) {
76+
for (const auto& cnx : PulsarFriend::getConnections(client_)) {
7077
EXPECT_TRUE(PulsarFriend::getProducers(*cnx).empty());
7178
EXPECT_TRUE(PulsarFriend::getConsumers(*cnx).empty());
7279
}
7380
}
7481
};
7582

76-
std::atomic_int ShutdownTest::id_{0};
77-
7883
TEST_P(ShutdownTest, testClose) {
84+
std::string topic = "shutdown-test-close-" + toString(GetParam()) + "-" + std::to_string(time(nullptr));
7985
Producer producer;
80-
ASSERT_EQ(ResultOk, client_.createProducer(topic_, producer));
86+
ASSERT_EQ(ResultOk, client_.createProducer(topic, producer));
8187
EXPECT_EQ(producers_.size(), 1);
8288
ASSERT_EQ(ResultOk, producer.close());
8389
EXPECT_EQ(producers_.size(), 0);
8490

8591
Consumer consumer;
86-
ASSERT_EQ(ResultOk, subscribe(consumer));
92+
ASSERT_EQ(ResultOk, subscribe(consumer, topic));
8793
EXPECT_EQ(consumers_.size(), 1);
8894
ASSERT_EQ(ResultOk, consumer.close());
8995
EXPECT_EQ(consumers_.size(), 0);
9096

91-
ASSERT_EQ(ResultOk, subscribe(consumer));
97+
ASSERT_EQ(ResultOk, subscribe(consumer, topic));
9298
EXPECT_EQ(consumers_.size(), 1);
9399
ASSERT_EQ(ResultOk, consumer.unsubscribe());
94100
EXPECT_EQ(consumers_.size(), 0);
@@ -98,16 +104,18 @@ TEST_P(ShutdownTest, testClose) {
98104
}
99105

100106
TEST_P(ShutdownTest, testDestructor) {
107+
std::string topic =
108+
"shutdown-test-destructor-" + toString(GetParam()) + "-" + std::to_string(time(nullptr));
101109
{
102110
Producer producer;
103-
ASSERT_EQ(ResultOk, client_.createProducer(topic_, producer));
111+
ASSERT_EQ(ResultOk, client_.createProducer(topic, producer));
104112
EXPECT_EQ(producers_.size(), 1);
105113
}
106114
EXPECT_EQ(producers_.size(), 0);
107115

108116
{
109117
Consumer consumer;
110-
ASSERT_EQ(ResultOk, subscribe(consumer));
118+
ASSERT_EQ(ResultOk, subscribe(consumer, topic));
111119
EXPECT_EQ(consumers_.size(), 1);
112120
}
113121
EXPECT_EQ(consumers_.size(), 0);

0 commit comments

Comments
 (0)