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/17 07:47:56 UTC
(celix) 01/01: Use future/promise to replace the shaky sleep.
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 82b5012d190b8ac7b7ca522c60a3ee9d63e231c3
Author: PengZheng <ho...@gmail.com>
AuthorDate: Fri Oct 17 15:46:38 2025 +0800
Use future/promise to replace the shaky sleep.
---
.../framework/gtest/src/CelixFrameworkTestSuite.cc | 30 ++++++++++++++--------
1 file changed, 20 insertions(+), 10 deletions(-)
diff --git a/libs/framework/gtest/src/CelixFrameworkTestSuite.cc b/libs/framework/gtest/src/CelixFrameworkTestSuite.cc
index b428dc8b4..82a002bdc 100644
--- a/libs/framework/gtest/src/CelixFrameworkTestSuite.cc
+++ b/libs/framework/gtest/src/CelixFrameworkTestSuite.cc
@@ -28,6 +28,7 @@
#include "celix_framework.h"
#include "framework_private.h"
#include "celix_constants.h"
+#include "celix_log.h"
#include "celix_utils.h"
class CelixFrameworkTestSuite : public ::testing::Test {
@@ -99,25 +100,34 @@ TEST_F(CelixFrameworkTestSuite, GenericEventTimeoutPropertyTest) {
ASSERT_TRUE(fw != nullptr);
// Start capturing stdout
- ::testing::internal::CaptureStderr();
+ std::promise<std::string> p;
+ std::future<std::string> f = p.get_future();
+ celix_frameworkLogger_setLogCallback(fw->logger, &p,
+ [](void* handle, celix_log_level_e, const char*, const char *, int, const char *format, va_list formatArgs) {
+ auto* p = static_cast<std::promise<std::string>*>(handle);
+ // format std::string from format and formatArgs
+ char buffer[1024];
+ vsnprintf(buffer, sizeof(buffer), format, formatArgs);
+ 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);
+ }
+ });
- //When there is a emtpy event queue
celix_framework_waitForEmptyEventQueue(fw);
//And a generic event is fired, that block the queue for 20ms
- auto callback = [](void* /*data*/) {
- std::this_thread::sleep_for(std::chrono::milliseconds{20});
+ auto callback = [](void* data) {
+ auto* f = static_cast<std::future<std::string>*>(data);
+ f->wait();
};
- long eventId = celix_framework_fireGenericEvent(fw, -1L, -1L, "test", nullptr, callback, nullptr, nullptr);
+ long eventId = celix_framework_fireGenericEvent(fw, 100L, -1L, "test", &f, callback, nullptr, nullptr);
+ EXPECT_EQ(100L, eventId);
//Then waiting for the event queue will have logged errors
celix_framework_waitForGenericEvent(fw, eventId);
- // And the log will contain a printed warning
- auto log = ::testing::internal::GetCapturedStderr();
- auto expected = "Generic event 'test' (id=" + std::to_string(eventId) + ")";
- EXPECT_TRUE(log.find(expected) != std::string::npos) << "Got " << log;
-
// Cleanup framework
celix_frameworkFactory_destroyFramework(fw);
}