Skip to content
Permalink
Browse files
Use flags instead of bool to accommodate future API extension.
  • Loading branch information
PengZheng committed Feb 25, 2022
1 parent c407c40 commit 98fddd504b7144b56580b11075c3310cbee48eb6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
@@ -76,7 +76,9 @@ class CelixBundleContextServicesTests : public ::testing::Test {
celix_service_use_options_t use_opts{};
use_opts.filter.serviceName = "calc";
use_opts.filter.versionRange = "[1,2)";
use_opts.direct = direct;
if(direct) {
use_opts.flags = CELIX_SERVICE_USE_DIRECT;
}

celix_service_registration_options_t reg_opts{};
reg_opts.serviceName = calcName;
@@ -114,7 +116,9 @@ class CelixBundleContextServicesTests : public ::testing::Test {
celix_service_use_options_t use_opts{};
use_opts.filter.serviceName = "calc";
use_opts.filter.versionRange = "[2,3)";
use_opts.direct = direct;
if(direct) {
use_opts.flags = CELIX_SERVICE_USE_DIRECT;
}

celix_service_registration_options_t reg_opts{};
reg_opts.serviceName = calcName;
@@ -153,7 +157,9 @@ class CelixBundleContextServicesTests : public ::testing::Test {

celix_service_use_options_t opts{};
opts.filter.serviceName = "calc";
opts.direct = direct;
if(direct) {
opts.flags = CELIX_SERVICE_USE_DIRECT;
}

bool called = celix_bundleContext_useServiceWithOptions(ctx, &opts);
EXPECT_FALSE(called); //service not avail.
@@ -202,7 +208,9 @@ class CelixBundleContextServicesTests : public ::testing::Test {

celix_service_use_options_t opts{};
opts.filter.serviceName = "calc";
opts.direct = direct;
if(direct) {
opts.flags = CELIX_SERVICE_USE_DIRECT;
}

bool called = celix_bundleContext_useServiceWithOptions(ctx, &opts);
EXPECT_FALSE(called); //service not avail.
@@ -380,13 +388,13 @@ TEST_F(CelixBundleContextServicesTests, registerMultipleAndUseServices) {
use_opts.filter.versionRange = nullptr;
use_opts.callbackHandle = &total;
use_opts.use = use;
use_opts.direct = false;
use_opts.flags = 0;
total = 0;
count = celix_bundleContext_useServicesWithOptions(ctx, &use_opts);
EXPECT_EQ(3, count);
EXPECT_EQ(42 * 3, total);

use_opts.direct = true;
use_opts.flags = CELIX_SERVICE_USE_DIRECT;
total = 0;
count = celix_bundleContext_useServicesWithOptions(ctx, &use_opts);
EXPECT_EQ(3, count);
@@ -398,13 +406,13 @@ TEST_F(CelixBundleContextServicesTests, registerMultipleAndUseServices) {
EXPECT_EQ(2, count);
EXPECT_EQ(42 * 2, total);

use_opts.direct = false;
use_opts.flags = 0;
total = 0;
count = celix_bundleContext_useServicesWithOptions(ctx, &use_opts);
EXPECT_EQ(2, count);
EXPECT_EQ(42 * 2, total);

use_opts.direct = true;
use_opts.flags = CELIX_SERVICE_USE_DIRECT;
total = 0;
count = celix_bundleContext_useServicesWithOptions(ctx, &use_opts);
EXPECT_EQ(2, count);
@@ -457,7 +465,7 @@ TEST_F(CelixBundleContextServicesTests, useServiceInUseCallback) {
opts.filter.serviceName = calcName;
opts.callbackHandle = ctx;
opts.use = use;
opts.direct = false;
opts.flags = 0;
bool called = celix_bundleContext_useServiceWithOptions(ctx, &opts);
EXPECT_TRUE(called);

@@ -1434,7 +1442,7 @@ TEST_F(CelixBundleContextServicesTests, UseServiceOnDemandDirectlyWithAsyncRegis
}, nullptr);
celix_service_use_options_t opts{};
opts.filter.serviceName = "test";
opts.direct = true;
opts.flags = CELIX_SERVICE_USE_DIRECT;
called = celix_bundleContext_useServiceWithOptions(ctx, &opts);
EXPECT_TRUE(called); //service created on demand.

@@ -1472,7 +1480,7 @@ TEST_F(CelixBundleContextServicesTests, UseServicesOnDemandDirectlyWithAsyncRegi

celix_service_use_options_t opts{};
opts.filter.serviceName = "test";
opts.direct = true;
opts.flags = CELIX_SERVICE_USE_DIRECT;
size_t count = celix_bundleContext_useServicesWithOptions(ctx, &opts);
EXPECT_EQ(2, count);

@@ -772,11 +772,11 @@ typedef struct celix_service_use_options {
*/
void (*useWithOwner)(void *handle, void *svc, const celix_properties_t *props, const celix_bundle_t *svcOwner) OPTS_INIT;
/**
* @brief Whether to call the provided callbacks from the caller thread directly, if false the callbacks will be called from the Celix event loop (most likely indirectly).
* @brief Call the provided callbacks from the caller thread directly if set, otherwise the callbacks will be called from the Celix event loop (most likely indirectly).
* Note that using blocking service in the Celix event loop is generally a bad idea, which should be avoided if possible.
* Default (false)
*/
bool direct;
#define CELIX_SERVICE_USE_DIRECT (1)
int flags;
} celix_service_use_options_t;

/**
@@ -792,7 +792,7 @@ typedef struct celix_service_use_options {
.use = NULL, \
.useWithProperties = NULL, \
.useWithOwner = NULL, \
.direct=false}
.flags=0}
#endif

/**
@@ -1209,7 +1209,7 @@ bool celix_bundleContext_useServiceWithOptions(
celix_framework_waitForGenericEvent(ctx->framework, eventId);


if(opts->direct) {
if(opts->flags & CELIX_SERVICE_USE_DIRECT) {
celix_framework_waitUntilNoPendingRegistration(ctx->framework); // TL;DR make "service on demand" pattern work. Try comment it out and run CelixBundleContextServicesTests.UseServiceOnDemandDirectlyWithAsyncRegisterTest
called = celix_serviceTracker_useHighestRankingService(data.svcTracker, NULL, opts->waitTimeoutInSeconds, opts->callbackHandle, opts->use, opts->useWithProperties, opts->useWithOwner);
} else {
@@ -1263,7 +1263,7 @@ size_t celix_bundleContext_useServicesWithOptions(
long eventId = celix_framework_fireGenericEvent(ctx->framework, -1, celix_bundle_getId(ctx->bundle), "create service tracker for celix_bundleContext_useServicesWithOptions", &data, celix_bundleContext_useServiceWithOptions_1_CreateServiceTracker, NULL, NULL);
celix_framework_waitForGenericEvent(ctx->framework, eventId);

if (opts->direct) {
if (opts->flags & CELIX_SERVICE_USE_DIRECT) {
celix_framework_waitUntilNoPendingRegistration(ctx->framework); // TL;DR make "service on demand" pattern work. Try comment it out and run CelixBundleContextServicesTests.UseServicesOnDemandDirectlyWithAsyncRegisterTest
celix_bundleContext_useServicesWithOptions_2_UseServiceTracker(&data);
} else {

0 comments on commit 98fddd5

Please sign in to comment.