diff --git a/libs/framework/gtest/src/ScheduledEventTestSuite.cc b/libs/framework/gtest/src/ScheduledEventTestSuite.cc index 9edbbb282..f2dd53f55 100644 --- a/libs/framework/gtest/src/ScheduledEventTestSuite.cc +++ b/libs/framework/gtest/src/ScheduledEventTestSuite.cc @@ -21,6 +21,7 @@ #include "celix/FrameworkFactory.h" #include "celix_bundle_context.h" +#include "celix_framework.h" #include "celix_scheduled_event.h" class ScheduledEventTestSuite : public ::testing::Test { @@ -761,3 +762,25 @@ TEST_F(ScheduledEventTestSuite, ScheduledEventTimeoutLogTest) { EXPECT_GE(logCount.load(), 2); } #endif + +TEST_F(ScheduledEventTestSuite, ScheduledEventForInvactiveFramework) { + // Given a framework that is stopped + celix_framework_stopBundle(fw->getCFramework(), CELIX_FRAMEWORK_BUNDLE_ID); + celix_framework_waitForStop(fw->getCFramework()); + // When a scheduled event is added + std::atomic count{0}; + auto callback = [](void* data) { + auto* count = static_cast*>(data); + count->fetch_add(1); + }; + + celix_scheduled_event_options_t opts{}; + opts.initialDelayInSeconds = 0.01; + opts.callbackData = &count; + opts.callback = callback; + long eventId = celix_bundleContext_scheduleEvent(fw->getFrameworkBundleContext()->getCBundleContext(), &opts); + EXPECT_LT(eventId, 0); + + // Then the event is not added + EXPECT_EQ(0, count.load()); +} diff --git a/libs/framework/src/framework.c b/libs/framework/src/framework.c index 0b317c91a..4706a4ae6 100644 --- a/libs/framework/src/framework.c +++ b/libs/framework/src/framework.c @@ -2583,8 +2583,13 @@ long celix_framework_scheduleEvent(celix_framework_t* fw, celix_bundleEntry_decreaseUseCount(bndEntry); celixThreadMutex_lock(&fw->dispatcher.mutex); - celix_longHashMap_put(fw->dispatcher.scheduledEvents, id, event); - celixThreadCondition_broadcast(&fw->dispatcher.cond); //notify dispatcher thread for newly added scheduled event + if (fw->dispatcher.active) { + celix_longHashMap_put(fw->dispatcher.scheduledEvents, id, event); + celixThreadCondition_broadcast(&fw->dispatcher.cond); //notify dispatcher thread for newly added scheduled event + } else { + celix_scheduledEvent_release(event); + id = -1L; + } celixThreadMutex_unlock(&fw->dispatcher.mutex); return id;