You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@celix.apache.org by pe...@apache.org on 2025/10/18 07:47:48 UTC
(celix) 01/01: Use future/promise to replace sleep-based event queue handling for improved test reliability.
This is an automated email from the ASF dual-hosted git repository.
pengzheng pushed a commit to branch hotfix/shaky-test-timeout
in repository https://gitbox.apache.org/repos/asf/celix.git
commit 94fda8a210bc2fae229a8a02aa6ad46d242831f9
Author: PengZheng <ho...@gmail.com>
AuthorDate: Sat Oct 18 15:47:35 2025 +0800
Use future/promise to replace sleep-based event queue handling for improved test reliability.
---
libs/framework/gtest/src/CelixFrameworkTestSuite.cc | 20 ++++++++++++++------
1 file changed, 14 insertions(+), 6 deletions(-)
diff --git a/libs/framework/gtest/src/CelixFrameworkTestSuite.cc b/libs/framework/gtest/src/CelixFrameworkTestSuite.cc
index 82a002bdc..86c625021 100644
--- a/libs/framework/gtest/src/CelixFrameworkTestSuite.cc
+++ b/libs/framework/gtest/src/CelixFrameworkTestSuite.cc
@@ -74,16 +74,21 @@ TEST_F(CelixFrameworkTestSuite, TimedWaitEventQueueTest) {
//When there is a emtpy event queue
celix_framework_waitForEmptyEventQueue(framework.get());
- //And a generic event is fired, that block the queue for 20ms
- auto callback = [](void* /*data*/) {
- std::this_thread::sleep_for(std::chrono::milliseconds{200});
+ std::promise<int> p;
+ std::future<int> f = p.get_future();
+ //And a generic event is fired, that block the queue until timeout
+ auto callback = [](void* data) {
+ auto* f = static_cast<std::future<std::string>*>(data);
+ f->wait();
};
- celix_framework_fireGenericEvent(framework.get(), -1L, -1L, "test", nullptr, callback, nullptr, nullptr);
+ celix_framework_fireGenericEvent(framework.get(), -1L, -1L, "test", &f, callback, nullptr, nullptr);
//Then a wait for empty event queue for max 5ms will return a timeout
celix_status_t status = celix_framework_waitForEmptyEventQueueFor(framework.get(), 0.005);
EXPECT_EQ(ETIMEDOUT, status) << "Expected timeout, but got " << celix_strerror(status);
+ p.set_value(1);
+
//And a wait for empty event queue for max 1s will return success
status = celix_framework_waitForEmptyEventQueueFor(framework.get(), 1);
EXPECT_EQ(CELIX_SUCCESS, status);
@@ -99,7 +104,6 @@ TEST_F(CelixFrameworkTestSuite, GenericEventTimeoutPropertyTest) {
framework_t* fw = celix_frameworkFactory_createFramework(config);
ASSERT_TRUE(fw != nullptr);
- // Start capturing stdout
std::promise<std::string> p;
std::future<std::string> f = p.get_future();
celix_frameworkLogger_setLogCallback(fw->logger, &p,
@@ -111,7 +115,11 @@ TEST_F(CelixFrameworkTestSuite, GenericEventTimeoutPropertyTest) {
auto log = std::string(buffer);
auto expected = "Generic event 'test' (id=" + std::to_string(100) + ")";
if (log.find(expected) != std::string::npos) {
- p->set_value(log);
+ try {
+ p->set_value(log);
+ } catch (std::future_error& e) {
+ EXPECT_EQ(std::future_errc::promise_already_satisfied, e.code());
+ }
}
});