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

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

commit 3cecc3fc5d6ef392d8fe05c62f0a754e0a9f5bd6
Author: Pepijn Noltes <[email protected]>
AuthorDate: Sun May 14 22:20:32 2023 +0200

    Add error injection test suite for scheduled event
---
 libs/framework/gtest/CMakeLists.txt                |  2 +
 .../framework/gtest/src/ScheduledEventTestSuite.cc | 10 +++-
 .../ScheduledEventWithErrorInjectionTestSuite.cc   | 69 ++++++++++++++++++++++
 libs/framework/src/celix_scheduled_event.c         |  5 --
 libs/framework/src/framework.c                     |  3 -
 libs/framework/src/framework_private.h             | 11 ++--
 6 files changed, 87 insertions(+), 13 deletions(-)

diff --git a/libs/framework/gtest/CMakeLists.txt 
b/libs/framework/gtest/CMakeLists.txt
index b2844a1b..f2e1faa7 100644
--- a/libs/framework/gtest/CMakeLists.txt
+++ b/libs/framework/gtest/CMakeLists.txt
@@ -128,6 +128,7 @@ if (LINKER_WRAP_SUPPORTED)
             src/BundleArchiveWithErrorInjectionTestSuite.cc
             src/CelixFrameworkUtilsErrorInjectionTestSuite.cc
             src/CelixBundleContextBundlesWithErrorTestSuite.cc
+            src/ScheduledEventWithErrorInjectionTestSuite.cc
     )
     target_compile_definitions(test_framework_with_ei PRIVATE
             SIMPLE_TEST_BUNDLE1_LOCATION="${SIMPLE_TEST_BUNDLE1}"
@@ -143,6 +144,7 @@ if (LINKER_WRAP_SUPPORTED)
             Celix::utils_ei
             Celix::asprintf_ei
             Celix::dlfcn_ei
+            Celix::array_list_ei
             GTest::gtest GTest::gtest_main
     )
 
diff --git a/libs/framework/gtest/src/ScheduledEventTestSuite.cc 
b/libs/framework/gtest/src/ScheduledEventTestSuite.cc
index aab19a1d..160aab6c 100644
--- a/libs/framework/gtest/src/ScheduledEventTestSuite.cc
+++ b/libs/framework/gtest/src/ScheduledEventTestSuite.cc
@@ -22,7 +22,6 @@
 #include "celix/FrameworkFactory.h"
 #include "celix_bundle_context.h"
 #include "celix_scheduled_event.h"
-#include "framework_private.h"
 
 class ScheduledEventTestSuite : public ::testing::Test {
 public:
@@ -184,4 +183,13 @@ TEST_F(ScheduledEventTestSuite, 
InvalidOptionsAndArgumentsTest) {
     //celix_scheduleEvent_destroy and celix_scheduledEvent_waitAndDestroy can 
be called with NULL
     celix_scheduledEvent_destroy(nullptr);
     celix_scheduledEvent_waitAndDestroy(nullptr);
+
+    //celix_bundleContext_removeScheduledEvent can handle invalid eventIds
+    celix_bundleContext_removeScheduledEvent(ctx->getCBundleContext(), -1);
+    celix_bundleContext_removeScheduledEvent(ctx->getCBundleContext(), 404);
+
+    //celix_framework_addScheduledEvent with an invalid bndId should return -1
+    scheduledEventId = celix_framework_addScheduledEvent(
+        ctx->getFramework()->getCFramework(), 404, nullptr, 0.0, 0.0, nullptr, 
[](void*) { /*nop*/ });
+    EXPECT_EQ(scheduledEventId, -1);
 }
diff --git 
a/libs/framework/gtest/src/ScheduledEventWithErrorInjectionTestSuite.cc 
b/libs/framework/gtest/src/ScheduledEventWithErrorInjectionTestSuite.cc
new file mode 100644
index 00000000..d3bb2ef2
--- /dev/null
+++ b/libs/framework/gtest/src/ScheduledEventWithErrorInjectionTestSuite.cc
@@ -0,0 +1,69 @@
+/*
+ * 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.
+ */
+
+#include <gtest/gtest.h>
+
+#include "celix/FrameworkFactory.h"
+#include "celix_bundle_context.h"
+#include "celix_scheduled_event.h"
+#include "framework_private.h"
+
+#include "celix_array_list_ei.h"
+#include "malloc_ei.h"
+
+class ScheduledEventWithErrorInjectionTestSuite : public ::testing::Test {
+public:
+    ScheduledEventWithErrorInjectionTestSuite() {
+        fw = 
celix::createFramework({{"CELIX_LOGGING_DEFAULT_ACTIVE_LOG_LEVEL", "info"}});
+    }
+
+    ~ScheduledEventWithErrorInjectionTestSuite() noexcept override {
+        celix_ei_expect_celix_arrayList_add(nullptr, 0, CELIX_SUCCESS);
+        celix_ei_expect_malloc(nullptr, 0, nullptr);
+    }
+
+    std::shared_ptr<celix::Framework> fw{};
+};
+
+TEST_F(ScheduledEventWithErrorInjectionTestSuite, ArrayListAddFailsTest) {
+    //Given celix_arrayList_add is primed to fail on the first call from 
addScheduledEvent (whitebox knowledge)
+    
celix_ei_expect_celix_arrayList_add((void*)celix_bundleContext_addScheduledEvent,
 1, CELIX_ENOMEM);
+
+    //When a scheduled event is added
+    celix_scheduled_event_options_t opts{};
+    opts.eventCallback = [](void*){/*nop*/};
+    long scheduledEventId = 
celix_bundleContext_addScheduledEvent(fw->getFrameworkBundleContext()->getCBundleContext(),
 &opts);
+
+    //Then the scheduled event id is -1 (error)
+    EXPECT_EQ(-1L, scheduledEventId);
+}
+
+TEST_F(ScheduledEventWithErrorInjectionTestSuite, MallocFailsTest) {
+    //Given malloc is primed to fail on the first call from 
celix_scheduledEvent_create (whitebox knowledge)
+    celix_ei_expect_malloc((void*)celix_scheduledEvent_create, 0, nullptr);
+
+    //When a scheduled event is added
+    celix_scheduled_event_options_t opts{};
+    opts.eventName = "malloc fail test";
+    opts.eventCallback = [](void*){/*nop*/};
+    long scheduledEventId = 
celix_bundleContext_addScheduledEvent(fw->getFrameworkBundleContext()->getCBundleContext(),
 &opts);
+
+    //Then the scheduled event id is -1 (error)
+    EXPECT_EQ(-1L, scheduledEventId);
+}
diff --git a/libs/framework/src/celix_scheduled_event.c 
b/libs/framework/src/celix_scheduled_event.c
index 66c02920..4c7c687e 100644
--- a/libs/framework/src/celix_scheduled_event.c
+++ b/libs/framework/src/celix_scheduled_event.c
@@ -30,11 +30,6 @@ celix_scheduled_event_t* 
celix_scheduledEvent_create(celix_framework_logger_t* l
                                                      double intervalInSeconds,
                                                      void* eventData,
                                                      void 
(*eventCallback)(void* eventData)) {
-    if (eventCallback == NULL) {
-        fw_log(logger, CELIX_LOG_LEVEL_ERROR, "Cannot add scheduled event for 
bundle id %li. No event callback provided", bndEntry->bndId);
-        return  NULL;
-    }
-
     celix_scheduled_event_t* event = malloc(sizeof(*event));
     char* eventName = providedEventName == NULL ? 
(char*)CELIX_SCHEDULED_EVENT_DEFAULT_NAME
                                                  : 
celix_utils_strdup(providedEventName);
diff --git a/libs/framework/src/framework.c b/libs/framework/src/framework.c
index b8252c01..b617438c 100644
--- a/libs/framework/src/framework.c
+++ b/libs/framework/src/framework.c
@@ -2697,6 +2697,3 @@ void celix_framework_waitForStop(celix_framework_t 
*framework) {
     celixThreadMutex_unlock(&framework->shutdown.mutex);
 }
 
-celix_framework_logger_t* celix_framework_getLogger(celix_framework_t* fw) {
-    return fw->logger;
-}
diff --git a/libs/framework/src/framework_private.h 
b/libs/framework/src/framework_private.h
index 04b4b700..672a0a8e 100644
--- a/libs/framework/src/framework_private.h
+++ b/libs/framework/src/framework_private.h
@@ -41,6 +41,10 @@
 #include "celix_threads.h"
 #include "service_registry.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 #ifndef CELIX_FRAMEWORK_DEFAULT_STATIC_EVENT_QUEUE_SIZE
 #define CELIX_FRAMEWORK_DEFAULT_STATIC_EVENT_QUEUE_SIZE 1024
 #endif
@@ -487,9 +491,8 @@ bool 
celix_framework_removeScheduledEvent(celix_framework_t* fw, long scheduledE
  */
 void celix_framework_cleanupScheduledEvents(celix_framework_t* fw, long bndId);
 
-/**
- * @brief Return the framework logger. Note logger lifetime is the same as the 
framework.
- */
-celix_framework_logger_t* celix_framework_getLogger(celix_framework_t* fw);
+#ifdef __cplusplus
+}
+#endif
 
 #endif /* FRAMEWORK_PRIVATE_H_ */

Reply via email to