This is an automated email from the ASF dual-hosted git repository. pnoltes pushed a commit to branch feature/fix_lb_command in repository https://gitbox.apache.org/repos/asf/celix.git
commit 1db7b5386ff4c80ce44b14c8cc317eb02d9578c4 Author: Pepijn Noltes <[email protected]> AuthorDate: Sat Apr 15 11:57:57 2023 +0200 Update useBundle/useBundles call to work on installed bundles --- ...amework_test.cpp => CelixFrameworkTestSuite.cc} | 37 ++++++++++++++++++++++ libs/framework/include/celix_bundle_context.h | 18 +++++------ libs/framework/include/celix_framework.h | 9 +++--- libs/framework/src/bundle_context.c | 10 +++--- libs/framework/src/framework.c | 14 +++++--- 5 files changed, 65 insertions(+), 23 deletions(-) diff --git a/libs/framework/gtest/src/single_framework_test.cpp b/libs/framework/gtest/src/CelixFrameworkTestSuite.cc similarity index 86% rename from libs/framework/gtest/src/single_framework_test.cpp rename to libs/framework/gtest/src/CelixFrameworkTestSuite.cc index bdd3b078..1a3562c1 100644 --- a/libs/framework/gtest/src/single_framework_test.cpp +++ b/libs/framework/gtest/src/CelixFrameworkTestSuite.cc @@ -144,6 +144,43 @@ TEST_F(CelixFrameworkTestSuite, ListBundlesTest) { celix_arrayList_destroy(list); } +TEST_F(CelixFrameworkTestSuite, UseBundlesAndUseBundleTest) { + celix_framework_t* fw = framework.get(); + + long bndId1 = celix_framework_installBundle(fw, SIMPLE_TEST_BUNDLE1_LOCATION, true); + EXPECT_GT(bndId1, CELIX_FRAMEWORK_BUNDLE_ID); + long bndId2 = celix_framework_installBundle(fw, SIMPLE_TEST_BUNDLE2_LOCATION, false); + EXPECT_GT(bndId2, CELIX_FRAMEWORK_BUNDLE_ID); + + int callBackCount = 0; + auto callback = [](void* handle, const celix_bundle_t* bnd) { + auto* ct = static_cast<int*>(handle); + *ct += 1; + EXPECT_GE(celix_bundle_getId(bnd), CELIX_FRAMEWORK_BUNDLE_ID); + }; + + size_t useCount = celix_framework_useBundles(fw, true, &callBackCount, callback); + EXPECT_EQ(3, useCount); //2 bundles + framework + + auto nop = [](void* /*handle*/, const celix_bundle_t* /*bnd*/) {}; + + //test use framework bundle + bool called = celix_framework_useBundle(fw, true, CELIX_FRAMEWORK_BUNDLE_ID, nullptr, nop); + EXPECT_TRUE(called); + + //test use active bundle + called = celix_framework_useBundle(fw, true, bndId1, nullptr, nop); + EXPECT_TRUE(called); + called = celix_framework_useBundle(fw, false, bndId1, nullptr, nop); + EXPECT_TRUE(called); + + //test use inactive bundle + called = celix_framework_useBundle(fw, true, bndId2, nullptr, nop); + EXPECT_FALSE(called); //note bnd2 is not active + called = celix_framework_useBundle(fw, false, bndId2, nullptr, nop); + EXPECT_TRUE(called); +} + class FrameworkFactoryTestSuite : public ::testing::Test { public: FrameworkFactoryTestSuite() = default; diff --git a/libs/framework/include/celix_bundle_context.h b/libs/framework/include/celix_bundle_context.h index 00a39b15..8739dd0a 100644 --- a/libs/framework/include/celix_bundle_context.h +++ b/libs/framework/include/celix_bundle_context.h @@ -1125,9 +1125,10 @@ long celix_bundleContext_trackBundlesWithOptions( ); /** - * @brief Use the bundle with the provided bundle id if it is in the active (started) state. + * @brief Use the bundle with the provided bundle id. * - * The provided callback will be called if the bundle is found and in the active (started) state. + * The provided callback will be called if the bundle is found (installed). + * Call with CELIX_FRAMEWORK_BUNDLE_ID as bundleId to use the framework bundle. * * @param ctx The bundle context. * @param bundleId The bundle id. @@ -1140,24 +1141,23 @@ bool celix_bundleContext_useBundle( celix_bundle_context_t *ctx, long bundleId, void *callbackHandle, - void (*use)(void *handle, const celix_bundle_t *bundle) -); + void (*use)(void *handle, const celix_bundle_t *bundle)); /** - * @brief Use the currently active (started) bundles. + * @brief Use the currently installed bundles. * - * The provided callback will be called for all the currently started bundles (excluding the framework bundle). + * The provided callback will be called for all the currently installed bundles, expect the framework bundle. * * @param ctx The bundle context. * @param callbackHandle The data pointer, which will be used in the callbacks * @param use The callback which will be called for the currently started bundles. * The bundle pointers are only guaranteed to be valid during the callback. + * @return The number of times the use callback is called (nr of installed bundles). */ -void celix_bundleContext_useBundles( +size_t celix_bundleContext_useBundles( celix_bundle_context_t *ctx, void *callbackHandle, - void (*use)(void *handle, const celix_bundle_t *bundle) -); + void (*use)(void *handle, const celix_bundle_t *bundle)); /** * @brief Service Tracker Info provided to the service tracker tracker callbacks. diff --git a/libs/framework/include/celix_framework.h b/libs/framework/include/celix_framework.h index 5124bc44..9703190d 100644 --- a/libs/framework/include/celix_framework.h +++ b/libs/framework/include/celix_framework.h @@ -73,16 +73,17 @@ celix_bundle_context_t* celix_framework_getFrameworkContext(const celix_framewor celix_bundle_t* celix_framework_getFrameworkBundle(const celix_framework_t *fw); /** - * @brief Use the currently active (started) bundles. - * The provided callback will be called for all the currently started bundles. + * @brief * @brief Use the currently installed bundles. + * The provided callback will be called for all the currently installed bundles. * * @param ctx The bundle context. * @param includeFrameworkBundle If true the callback will also be triggered for the framework bundle. * @param callbackHandle The data pointer, which will be used in the callbacks - * @param use The callback which will be called for the currently started bundles. + * @param use The callback which will be called for the currently installed bundles. * The bundle pointers are only guaranteed to be valid during the callback. + * @return The number of times the use callback is called. */ -void celix_framework_useBundles(celix_framework_t *fw, bool includeFrameworkBundle, void *callbackHandle, void(*use)(void *handle, const celix_bundle_t *bnd)); +size_t celix_framework_useBundles(celix_framework_t *fw, bool includeFrameworkBundle, void *callbackHandle, void(*use)(void *handle, const celix_bundle_t *bnd)); /** * @brief Use the bundle with the provided bundle id diff --git a/libs/framework/src/bundle_context.c b/libs/framework/src/bundle_context.c index e41ddcc6..c50378f6 100644 --- a/libs/framework/src/bundle_context.c +++ b/libs/framework/src/bundle_context.c @@ -630,11 +630,11 @@ long celix_bundleContext_trackBundlesAsync( return celix_bundleContext_trackBundlesWithOptionsAsync(ctx, &opts); } -void celix_bundleContext_useBundles( +size_t celix_bundleContext_useBundles( bundle_context_t *ctx, void *callbackHandle, void (*use)(void *handle, const bundle_t *bundle)) { - celix_framework_useBundles(ctx->framework, false, callbackHandle, use); + return celix_framework_useBundles(ctx->framework, false, callbackHandle, use); } bool celix_bundleContext_useBundle( @@ -642,7 +642,7 @@ bool celix_bundleContext_useBundle( long bundleId, void *callbackHandle, void (*use)(void *handle, const bundle_t *bundle)) { - return celix_framework_useBundle(ctx->framework, true, bundleId, callbackHandle, use); + return celix_framework_useBundle(ctx->framework, false, bundleId, callbackHandle, use); } static void bundleContext_cleanupBundleTrackers(bundle_context_t *ctx) { @@ -1086,7 +1086,7 @@ bool celix_bundleContext_useServiceWithOptions( if(opts->flags & CELIX_SERVICE_USE_DIRECT) { if(opts->flags & CELIX_SERVICE_USE_SOD) { - // check CelixBundleContextServicesTests.UseServiceOnDemandDirectlyWithAsyncRegisterTest to see what is "service on demand". + // check CelixBundleContextServicesTestSuite.UseServiceOnDemandDirectlyWithAsyncRegisterTest to see what is "service on demand". celix_framework_waitUntilNoPendingRegistration(ctx->framework); } called = celix_serviceTracker_useHighestRankingService(data.svcTracker, NULL, opts->waitTimeoutInSeconds, opts->callbackHandle, opts->use, opts->useWithProperties, opts->useWithOwner); @@ -1143,7 +1143,7 @@ size_t celix_bundleContext_useServicesWithOptions( if (opts->flags & CELIX_SERVICE_USE_DIRECT) { if(opts->flags & CELIX_SERVICE_USE_SOD) { - // check CelixBundleContextServicesTests.UseServicesOnDemandDirectlyWithAsyncRegisterTest to see what is "service on demand". + // check CelixBundleContextServicesTestSuite.UseServicesOnDemandDirectlyWithAsyncRegisterTest to see what is "service on demand". celix_framework_waitUntilNoPendingRegistration(ctx->framework); } celix_bundleContext_useServicesWithOptions_2_UseServiceTracker(&data); diff --git a/libs/framework/src/framework.c b/libs/framework/src/framework.c index 17543f1b..3405a629 100644 --- a/libs/framework/src/framework.c +++ b/libs/framework/src/framework.c @@ -1553,7 +1553,8 @@ static celix_status_t frameworkActivator_destroy(void * userData, bundle_context **********************************************************************************************************************/ -void celix_framework_useBundles(framework_t *fw, bool includeFrameworkBundle, void *callbackHandle, void(*use)(void *handle, const bundle_t *bnd)) { +size_t celix_framework_useBundles(framework_t *fw, bool includeFrameworkBundle, void *callbackHandle, void(*use)(void *handle, const bundle_t *bnd)) { + size_t count = 0; celix_array_list_t *bundleIds = celix_arrayList_create(); celixThreadMutex_lock(&fw->installedBundles.mutex); @@ -1561,22 +1562,25 @@ void celix_framework_useBundles(framework_t *fw, bool includeFrameworkBundle, vo for (int i = 0; i < size; ++i) { celix_framework_bundle_entry_t *entry = celix_arrayList_get(fw->installedBundles.entries, i); if (entry->bndId > 0 || includeFrameworkBundle) { - //NOTE bundle state is checked in celix_framework_useBundles celix_arrayList_addLong(bundleIds, entry->bndId); } } celixThreadMutex_unlock(&fw->installedBundles.mutex); - //note that stored bundle ids can now already be invalid (race cond), - //but the celix_framework_useBundle function should be able to handle this safely. + // note that stored bundle ids can now already be invalid, but the celix_framework_useBundle function should be + // able to handle this safely. size = celix_arrayList_size(bundleIds); for (int i = 0; i < size; ++i) { long bndId = celix_arrayList_getLong(bundleIds, i); - celix_framework_useBundle(fw, true, bndId, callbackHandle, use); + bool called = celix_framework_useBundle(fw, false, bndId, callbackHandle, use); + if (called) { + ++count; + } } celix_arrayList_destroy(bundleIds); + return count; } bool celix_framework_useBundle(framework_t *fw, bool onlyActive, long bundleId, void *callbackHandle, void(*use)(void *handle, const bundle_t *bnd)) {
