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

pnoltes pushed a commit to branch 
feature/87-create-service-tracker-inside-useTrackedService-call
in repository https://gitbox.apache.org/repos/asf/celix.git

commit dbadfebcc4401b7f51a02c2f504d23863ca114e9
Author: Pepijn Noltes <pnol...@apache.org>
AuthorDate: Sat Mar 2 13:28:09 2024 +0100

    gh-87: Add test to call useTrackedService on the event thread.
---
 .../src/CelixBundleContextServicesTestSuite.cc     | 36 ++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/libs/framework/gtest/src/CelixBundleContextServicesTestSuite.cc 
b/libs/framework/gtest/src/CelixBundleContextServicesTestSuite.cc
index 8218ecf0..607cb208 100644
--- a/libs/framework/gtest/src/CelixBundleContextServicesTestSuite.cc
+++ b/libs/framework/gtest/src/CelixBundleContextServicesTestSuite.cc
@@ -2009,3 +2009,39 @@ TEST_F(CelixBundleContextServicesTestSuite, 
UseTrackedServiceDuringTrackerCreati
     serviceFound = celix_bundleContext_useTrackedService(ctx, trkId, nullptr, 
nullptr);
     EXPECT_TRUE(serviceFound);
 }
+
+TEST_F(CelixBundleContextServicesTestSuite, 
UseTrackedServiceOnTheCelixEventThread) {
+    //Given a registered service with a service registration guard
+    long svcId = celix_bundleContext_registerService(ctx, (void*)0x42, "test", 
nullptr);
+    celix_auto(celix_service_registration_guard_t) svcGuard = 
celix_serviceRegistrationGuard_init(ctx, svcId);
+
+    //And a  service tracker for the "test" service with a service tracker 
guard
+    long trkId = celix_bundleContext_trackServices(ctx, "test");
+    celix_auto(celix_tracker_guard_t) trkGuard = celix_trackerGuard_init(ctx, 
trkId);
+
+    //When all events are processed
+    celix_bundleContext_waitForEvents(ctx);
+
+    //Then I can use the tracked service on the Celix event thread
+    struct callback_data {
+        celix_bundle_context_t* ctx;
+        long trkId;
+    };
+    callback_data cbData{ctx, trkId};
+    auto eventCallback = [](void *data) {
+        auto d = static_cast<callback_data*>(data);
+        bool called = celix_bundleContext_useTrackedService(d->ctx, d->trkId, 
nullptr, nullptr);
+        EXPECT_TRUE(called);
+    };
+
+    long eventId = celix_framework_fireGenericEvent(
+            fw,
+            -1,
+            celix_bundle_getId(celix_framework_getFrameworkBundle(fw)),
+            "use tracked service",
+            (void*)&cbData,
+            eventCallback,
+            nullptr,
+            nullptr);
+    celix_framework_waitForGenericEvent(fw, eventId);
+}

Reply via email to