Skip to content

Commit 7bb6402

Browse files
[flaky tests] Fix flaky ShutdownTest::testDestructor (#62)
Fixes #61 ### Motivation `testDestructor` is flaky because the destructor might not be called immediately after the `shared_ptr` object goes out of the scope. It's similar like the flaky `testReferenceCount` before in apache/pulsar#17645. ### Modifications Add back `WaitUtils.h`, which was removed in #55, add use `waitUntil` to wait until the assertion. ### Verifications Run the reproduce script in #61. Even if the loop count was increased to 100, it still never failed.
1 parent 1b701e3 commit 7bb6402

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

tests/ShutdownTest.cc

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "lib/ClientImpl.h"
2424
#include "HttpHelper.h"
2525
#include "PulsarFriend.h"
26+
#include "WaitUtils.h"
2627

2728
using namespace pulsar;
2829

@@ -111,13 +112,15 @@ TEST_P(ShutdownTest, testDestructor) {
111112
ASSERT_EQ(ResultOk, client_.createProducer(topic, producer));
112113
EXPECT_EQ(producers_.size(), 1);
113114
}
115+
waitUntil(std::chrono::seconds(2), [this] { return producers_.size() == 0; });
114116
EXPECT_EQ(producers_.size(), 0);
115117

116118
{
117119
Consumer consumer;
118120
ASSERT_EQ(ResultOk, subscribe(consumer, topic));
119121
EXPECT_EQ(consumers_.size(), 1);
120122
}
123+
waitUntil(std::chrono::seconds(2), [this] { return consumers_.size() == 0; });
121124
EXPECT_EQ(consumers_.size(), 0);
122125

123126
assertConnectionsEmpty();

tests/WaitUtils.h

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
#pragma once
20+
21+
#include <chrono>
22+
#include <functional>
23+
#include <thread>
24+
25+
namespace pulsar {
26+
27+
template <typename Rep, typename Period>
28+
inline void waitUntil(std::chrono::duration<Rep, Period> timeout, std::function<bool()> condition) {
29+
auto timeoutMs = std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count();
30+
while (timeoutMs > 0) {
31+
auto now = std::chrono::high_resolution_clock::now();
32+
if (condition()) {
33+
break;
34+
}
35+
std::this_thread::sleep_for(std::chrono::milliseconds(10));
36+
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
37+
std::chrono::high_resolution_clock::now() - now)
38+
.count();
39+
timeoutMs -= elapsed;
40+
}
41+
}
42+
43+
} // namespace pulsar

0 commit comments

Comments
 (0)