Skip to content

Commit

Permalink
Merge pull request #380 from PengZheng/fix-tracker-assertion
Browse files Browse the repository at this point in the history
Fix assertion failure when using C++ ServiceTracker in the event loop.
  • Loading branch information
pnoltes committed Nov 21, 2021
2 parents 82dab97 + 64b7293 commit 71fac23
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
16 changes: 15 additions & 1 deletion libs/framework/gtest/src/CxxBundleContextTestSuite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,20 @@ TEST_F(CxxBundleContextTestSuite, UnregisterServiceWhileRegistering) {
ctx->waitForEvents();
}

TEST_F(CxxBundleContextTestSuite, GetServiceInEventLoop) {
auto context = ctx;
ctx->getFramework()->fireGenericEvent(
ctx->getBundleId(),
"register/unregister in Celix event thread",
[context]() {
auto tracker = context->trackServices<TestInterface>().build();
auto svc = tracker->getHighestRankingService();
EXPECT_TRUE(svc.get() == nullptr);
}
);
ctx->waitForEvents();
}

TEST_F(CxxBundleContextTestSuite, KeepSharedPtrActiveWhileDeregistering) {
auto svcReg = ctx->registerService<TestInterface>(std::make_shared<TestImplementation>())
.build();
Expand Down Expand Up @@ -653,4 +667,4 @@ TEST_F(CxxBundleContextTestSuite, CheckStandardServiceProperties) {
EXPECT_TRUE(called);

celix_bundleContext_unregisterService(ctx->getCBundleContext(), svcId);
}
}
4 changes: 2 additions & 2 deletions libs/framework/include/celix/Trackers.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ namespace celix {
*/
void waitIfAble() const {
auto* fw = celix_bundleContext_getFramework(cCtx.get());
if (celix_framework_isCurrentThreadTheEventLoop(fw)) {
if (!celix_framework_isCurrentThreadTheEventLoop(fw)) {
wait();
}
}
Expand Down Expand Up @@ -765,4 +765,4 @@ namespace celix {
const std::vector<std::function<void(const ServiceTrackerInfo&)>> onTrackerDestroyed;
};

}
}

0 comments on commit 71fac23

Please sign in to comment.