This is an automated email from the ASF dual-hosted git repository.

pnoltes pushed a commit to branch feature/585-celix-conditions
in repository https://gitbox.apache.org/repos/asf/celix.git

commit aab28be1979a8467bc0490d38dfd26d0a16a104c
Author: Pepijn Noltes <[email protected]>
AuthorDate: Thu Jul 27 20:28:42 2023 +0200

    Refactor WaitForScheduledEventTest into 2 tests
---
 .../framework/gtest/src/ScheduledEventTestSuite.cc | 65 +++++++++++++++++-----
 1 file changed, 52 insertions(+), 13 deletions(-)

diff --git a/libs/framework/gtest/src/ScheduledEventTestSuite.cc 
b/libs/framework/gtest/src/ScheduledEventTestSuite.cc
index 90646a4b..1e0d07f3 100644
--- a/libs/framework/gtest/src/ScheduledEventTestSuite.cc
+++ b/libs/framework/gtest/src/ScheduledEventTestSuite.cc
@@ -537,7 +537,7 @@ TEST_F(ScheduledEventTestSuite, 
CxxCancelOneShotEventBeforeFiredTest) {
     // Then the event is not fired and does not leak
 }
 
-TEST_F(ScheduledEventTestSuite, RemoveScheduledEventAsync) {
+TEST_F(ScheduledEventTestSuite, RemoveScheduledEventAsyncTest) {
     std::atomic<int> count{0};
     auto callback = [](void* data) {
         auto* count = static_cast<std::atomic<int>*>(data);
@@ -562,14 +562,14 @@ TEST_F(ScheduledEventTestSuite, 
RemoveScheduledEventAsync) {
     EXPECT_EQ(0, count.load());
 }
 
-TEST_F(ScheduledEventTestSuite, WaitForScheduledEvent) {
+TEST_F(ScheduledEventTestSuite, WaitForScheduledEventTest) {
     std::atomic<int> count{0};
     auto callback = [](void* data) {
         auto* count = static_cast<std::atomic<int>*>(data);
         count->fetch_add(1);
     };
 
-    // Given a scheduled event with an initial delay of 1ms and an interval of 
1ms
+    // Given a scheduled event with an initial delay of 10ms and an interval 
of 10ms
     celix_scheduled_event_options_t opts{};
     opts.initialDelayInSeconds = 0.01;
     opts.intervalInSeconds = 0.01;
@@ -580,35 +580,74 @@ TEST_F(ScheduledEventTestSuite, WaitForScheduledEvent) {
 
     // When waiting for the event with a timeout longer than the initial delay
     auto status =
-        
celix_bundleContext_waitForScheduledEvent(fw->getFrameworkBundleContext()->getCBundleContext(),
 eventId, 1);
+        
celix_bundleContext_waitForScheduledEvent(fw->getFrameworkBundleContext()->getCBundleContext(),
 eventId, 2);
 
     // Then the return status is success
-    EXPECT_EQ(CELIX_SUCCESS, status);
+    EXPECT_EQ(CELIX_SUCCESS, status) << "Unexpected status" << 
celix_strerror(status) << std::endl;
 
     // And the event is fired
     EXPECT_EQ(1, count.load());
 
+    // When waiting for the event with a timeout longer than the interval
+    status =
+        
celix_bundleContext_waitForScheduledEvent(fw->getFrameworkBundleContext()->getCBundleContext(),
 eventId, 2);
+
+    // Then the return status is success
+    EXPECT_EQ(CELIX_SUCCESS, status);
+
+    // And the event is fired again
+    EXPECT_EQ(2, count.load());
+
+    
celix_bundleContext_removeScheduledEvent(fw->getFrameworkBundleContext()->getCBundleContext(),
 eventId);
+}
+
+TEST_F(ScheduledEventTestSuite, WaitTooShortForScheduledEventTest) {
+    std::atomic<int> count{0};
+    auto callback = [](void *data) {
+        auto *count = static_cast<std::atomic<int> *>(data);
+        count->fetch_add(1);
+    };
+
+    // Given a scheduled event with an initial delay of 1s and an interval of 
1s
+    celix_scheduled_event_options_t opts{};
+    opts.initialDelayInSeconds = 1;
+    opts.intervalInSeconds = 1;
+    opts.callbackData = &count;
+    opts.callback = callback;
+    long eventId = 
celix_bundleContext_scheduleEvent(fw->getFrameworkBundleContext()->getCBundleContext(),
 &opts);
+    EXPECT_GE(eventId, 0);
+
     // When waiting too short for the event
-    status = celix_bundleContext_waitForScheduledEvent(
-        fw->getFrameworkBundleContext()->getCBundleContext(), eventId, 0.0001);
+    celix_status_t status = celix_bundleContext_waitForScheduledEvent(
+            fw->getFrameworkBundleContext()->getCBundleContext(), eventId, 
0.0001);
 
     // Then the return status is timeout
     EXPECT_EQ(ETIMEDOUT, status);
 
-    // When waiting for the event with a timeout longer than the interval
-    status =
-        
celix_bundleContext_waitForScheduledEvent(fw->getFrameworkBundleContext()->getCBundleContext(),
 eventId, 1);
+    // And th event is not fired
+    EXPECT_EQ(0, count.load());
+
+    // When event is woken up
+    status = 
celix_bundleContext_wakeupScheduledEvent(fw->getFrameworkBundleContext()->getCBundleContext(),
 eventId);
 
     // Then the return status is success
     EXPECT_EQ(CELIX_SUCCESS, status);
 
-    // And the event is fired again
-    EXPECT_EQ(2, count.load());
+    // And the event will be fired
+    waitFor([&count]() { return count.load() == 1; }, 
std::chrono::milliseconds{ALLOWED_ERROR_MARGIN_IN_MS});
+    EXPECT_EQ(1, count.load());
+
+    // When waiting too short for the next (interval based) event
+    status = celix_bundleContext_waitForScheduledEvent(
+            fw->getFrameworkBundleContext()->getCBundleContext(), eventId, 
0.0001);
+
+    // Then the return status is timeout
+    EXPECT_EQ(ETIMEDOUT, status);
 
     
celix_bundleContext_removeScheduledEvent(fw->getFrameworkBundleContext()->getCBundleContext(),
 eventId);
 }
 
-TEST_F(ScheduledEventTestSuite, CxxWaitForScheduledEvent) {
+TEST_F(ScheduledEventTestSuite, CxxWaitForScheduledEventTest) {
     std::atomic<int> count{0};
     auto callback = [&count]() { count.fetch_add(1); };
 

Reply via email to