diff --git a/tests/ShutdownTest.cc b/tests/ShutdownTest.cc index d9a9c232..39513474 100644 --- a/tests/ShutdownTest.cc +++ b/tests/ShutdownTest.cc @@ -23,6 +23,7 @@ #include "lib/ClientImpl.h" #include "HttpHelper.h" #include "PulsarFriend.h" +#include "WaitUtils.h" using namespace pulsar; @@ -111,6 +112,7 @@ TEST_P(ShutdownTest, testDestructor) { ASSERT_EQ(ResultOk, client_.createProducer(topic, producer)); EXPECT_EQ(producers_.size(), 1); } + waitUntil(std::chrono::seconds(2), [this] { return producers_.size() == 0; }); EXPECT_EQ(producers_.size(), 0); { @@ -118,6 +120,7 @@ TEST_P(ShutdownTest, testDestructor) { ASSERT_EQ(ResultOk, subscribe(consumer, topic)); EXPECT_EQ(consumers_.size(), 1); } + waitUntil(std::chrono::seconds(2), [this] { return consumers_.size() == 0; }); EXPECT_EQ(consumers_.size(), 0); assertConnectionsEmpty(); diff --git a/tests/WaitUtils.h b/tests/WaitUtils.h new file mode 100644 index 00000000..abe3efcc --- /dev/null +++ b/tests/WaitUtils.h @@ -0,0 +1,43 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +#pragma once + +#include +#include +#include + +namespace pulsar { + +template +inline void waitUntil(std::chrono::duration timeout, std::function condition) { + auto timeoutMs = std::chrono::duration_cast(timeout).count(); + while (timeoutMs > 0) { + auto now = std::chrono::high_resolution_clock::now(); + if (condition()) { + break; + } + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + auto elapsed = std::chrono::duration_cast( + std::chrono::high_resolution_clock::now() - now) + .count(); + timeoutMs -= elapsed; + } +} + +} // namespace pulsar