This is an automated email from the ASF dual-hosted git repository. pnoltes pushed a commit to branch feature/cxx_rsa_update in repository https://gitbox.apache.org/repos/asf/celix.git
commit 10c92193006fddb333e210ab5766893770e142ca Author: Pepijn Noltes <[email protected]> AuthorDate: Thu Apr 15 21:04:34 2021 +0200 Refactors test case to use components instead of svc registrations. --- .../admin/gtest/src/RemoteServiceAdminTestSuite.cc | 116 ++++++++++++++------- .../async_remote_services/admin/include/admin.h | 8 +- bundles/async_remote_services/admin/src/admin.cc | 26 ++--- .../include/celix/rsa/IExportServiceFactory.h | 12 +-- .../include/celix/rsa/IImportServiceFactory.h | 10 +- 5 files changed, 102 insertions(+), 70 deletions(-) diff --git a/bundles/async_remote_services/admin/gtest/src/RemoteServiceAdminTestSuite.cc b/bundles/async_remote_services/admin/gtest/src/RemoteServiceAdminTestSuite.cc index 20ff87e..22ff43e 100644 --- a/bundles/async_remote_services/admin/gtest/src/RemoteServiceAdminTestSuite.cc +++ b/bundles/async_remote_services/admin/gtest/src/RemoteServiceAdminTestSuite.cc @@ -59,48 +59,62 @@ private: std::shared_ptr<celix::rsa::Endpoint> endpoint = std::make_shared<celix::rsa::Endpoint>(celix::Properties{}); //can be empty for stub }; -class StubExportedServiceGuard { + +class StubExportedServiceEntry { public: - explicit StubExportedServiceGuard(std::shared_ptr<celix::ServiceRegistration> _reg) : reg{std::move(_reg)} {} + explicit StubExportedServiceEntry(std::shared_ptr<celix::BundleContext> _ctx) : ctx{std::move(_ctx)} { + auto& cmp = ctx->getDependencyManager()->createComponent<StubExportedService>(); + cmp.createProvidedService<celix::rsa::IExportedService>(); + //NOTE normally add svc dep to the service to be exported publisher + //NOTE normally also provide subscriber + cmp.buildAsync(); + cmpUUID = cmp.getUUID(); + } - void close() { - reg->unregister(); + ~StubExportedServiceEntry() noexcept { + close(); + } + + void close() noexcept { + if (!cmpUUID.empty()) { + ctx->getDependencyManager()->removeComponentAsync(cmpUUID); + } + cmpUUID = ""; } private: - const std::shared_ptr<celix::ServiceRegistration> reg; + const std::shared_ptr<celix::BundleContext> ctx; + std::string cmpUUID{}; }; -class StubExportServiceRegistration : public celix::rsa::IExportServiceRegistration { +class StubExportServiceGuard : public celix::rsa::IExportServiceGuard { public: - explicit StubExportServiceRegistration(std::weak_ptr<StubExportedServiceGuard> _guard) : guard{std::move(_guard)} {} - ~StubExportServiceRegistration() noexcept override { - auto g = guard.lock(); - if (g) { - g->close(); + explicit StubExportServiceGuard(std::weak_ptr<StubExportedServiceEntry> _entry) : entry{std::move(_entry)} {} + ~StubExportServiceGuard() noexcept override { + auto e = entry.lock(); + if (e) { + e->close(); } } private: - const std::weak_ptr<StubExportedServiceGuard> guard; + const std::weak_ptr<StubExportedServiceEntry> entry; }; class StubExportServiceFactory : public celix::rsa::IExportServiceFactory { public: explicit StubExportServiceFactory(std::shared_ptr<celix::BundleContext> _ctx) : ctx{std::move(_ctx)} {} - std::unique_ptr<celix::rsa::IExportServiceRegistration> exportService(const celix::Properties& /*serviceProperties*/) override { - auto svcReg= ctx->registerService<celix::rsa::IExportedService>(std::make_shared<StubExportedService>()).build(); - auto guard = std::make_shared<StubExportedServiceGuard>(std::move(svcReg)); - + std::unique_ptr<celix::rsa::IExportServiceGuard> exportService(const celix::Properties& /*serviceProperties*/) override { + auto entry = std::make_shared<StubExportedServiceEntry>(ctx); std::lock_guard<std::mutex> lock{mutex}; - guards.emplace_back(guard); - return std::make_unique<StubExportServiceRegistration>(guard); + entries.emplace_back(entry); + return std::make_unique<StubExportServiceGuard>(entry); } private: const std::shared_ptr<celix::BundleContext> ctx; std::mutex mutex{}; - std::vector<std::shared_ptr<StubExportedServiceGuard>> guards{}; + std::vector<std::shared_ptr<StubExportedServiceEntry>> entries{}; }; class IDummyService { @@ -143,6 +157,9 @@ TEST_F(RemoteServiceAdminTestSuite, exportService) { EXPECT_EQ(1, count); + /** + * When I remove the export service factory, the IExportService will be removed. + */ reg1->unregister(); //removed factory -> removed exported interface @@ -189,44 +206,60 @@ TEST_F(RemoteServiceAdminTestSuite, exportServiceDifferentOrder) { * the RemoteServiceAdmin. */ -class StubImportedServiceGuard { +class StubImportedService : public IDummyService { public: - explicit StubImportedServiceGuard(std::shared_ptr<celix::ServiceRegistration> _reg) : reg{std::move(_reg)} {} + ~StubImportedService() noexcept override = default; +}; - void close() { - reg->unregister(); +class StubImportedServiceEntry { +public: + explicit StubImportedServiceEntry(std::shared_ptr<celix::BundleContext> _ctx) : ctx{std::move(_ctx)} { + auto& cmp = ctx->getDependencyManager()->createComponent<StubImportedService>(); + cmp.createProvidedService<IDummyService>() + .addProperty(celix::rsa::REMOTE_SERVICE_IMPORTED_PROPERTY_NAME, true); + cmp.buildAsync(); + cmpUUID = cmp.getUUID(); + } + + ~StubImportedServiceEntry() noexcept { + close(); + } + + void close() noexcept { + if (!cmpUUID.empty()) { + ctx->getDependencyManager()->removeComponentAsync(cmpUUID); + } + cmpUUID = ""; } private: - const std::shared_ptr<celix::ServiceRegistration> reg; + const std::shared_ptr<celix::BundleContext> ctx; + std::string cmpUUID{}; }; -class StubImportServiceRegistration : public celix::rsa::IImportServiceRegistration { +class StubImportServiceGuard : public celix::rsa::IImportServiceGuard { public: - explicit StubImportServiceRegistration(std::weak_ptr<StubImportedServiceGuard> _guard) : guard{std::move(_guard)} {} - ~StubImportServiceRegistration() noexcept override { - auto g = guard.lock(); - if (g) { - g->close(); + explicit StubImportServiceGuard(std::weak_ptr<StubImportedServiceEntry> _entry) : entry{std::move(_entry)} {} + ~StubImportServiceGuard() noexcept override { + auto e = entry.lock(); + if (e) { + e->close(); } } private: - const std::weak_ptr<StubImportedServiceGuard> guard; + const std::weak_ptr<StubImportedServiceEntry> entry; }; class StubImportServiceFactory : public celix::rsa::IImportServiceFactory { public: explicit StubImportServiceFactory(std::shared_ptr<celix::BundleContext> _ctx) : ctx{std::move(_ctx)} {} - virtual std::unique_ptr<celix::rsa::IImportServiceRegistration> importService(const celix::rsa::Endpoint& endpoint) { + virtual std::unique_ptr<celix::rsa::IImportServiceGuard> importService(const celix::rsa::Endpoint& endpoint) { if (endpoint.getExportedInterfaces() == celix::typeName<IDummyService>()) { - auto reg = ctx->registerService<IDummyService>(std::make_shared<DummyServiceImpl>()) - .addProperty(celix::rsa::REMOTE_SERVICE_IMPORTED_PROPERTY_NAME, true) - .build(); std::lock_guard<std::mutex> lock{mutex}; - auto guard = std::make_shared<StubImportedServiceGuard>(std::move(reg)); - guards.emplace_back(guard); - return std::make_unique<StubImportServiceRegistration>(guard); + auto entry = std::make_shared<StubImportedServiceEntry>(ctx); + entries.emplace_back(entry); + return std::make_unique<StubImportServiceGuard>(entry); } else { return {}; } @@ -235,7 +268,7 @@ public: private: const std::shared_ptr<celix::BundleContext> ctx; std::mutex mutex{}; - std::vector<std::shared_ptr<StubImportedServiceGuard>> guards{}; + std::vector<std::shared_ptr<StubImportedServiceEntry>> entries{}; }; TEST_F(RemoteServiceAdminTestSuite, importService) { @@ -244,7 +277,7 @@ TEST_F(RemoteServiceAdminTestSuite, importService) { /** * When I add a import service factory and register a Endpoint/ - * RemoteServiceAdmin will create a imported service (proxy for remote service described by the endpoint). + * RemoteServiceAdmin will create a imported service through the factory (proxy for remote service described by the endpoint). */ auto count = ctx->useService<IDummyService>() .build(); @@ -270,6 +303,9 @@ TEST_F(RemoteServiceAdminTestSuite, importService) { EXPECT_EQ(1, count); + /** + * When I remove the import service factory, the imported service (proxy) will be removed. + */ reg1->unregister(); //removed factory -> removed imported interface diff --git a/bundles/async_remote_services/admin/include/admin.h b/bundles/async_remote_services/admin/include/admin.h index 80be3ad..4d1bd1f 100644 --- a/bundles/async_remote_services/admin/include/admin.h +++ b/bundles/async_remote_services/admin/include/admin.h @@ -67,13 +67,13 @@ namespace celix::async_rsa { std::pmr::unordered_map<std::string, std::shared_ptr<celix::rsa::IExportServiceFactory>> _exportServiceFactories{&_memResource}; //key = service name std::pmr::unordered_map<std::string, std::shared_ptr<celix::rsa::IImportServiceFactory>> _importServiceFactories{&_memResource}; //key = service name - std::pmr::unordered_map<std::string, std::unique_ptr<celix::rsa::IImportServiceRegistration>> _importedServices{&_memResource}; //key = endpoint id - std::pmr::unordered_map<long, std::unique_ptr<celix::rsa::IExportServiceRegistration>> _exportedServices{&_memResource}; //key = service id + std::pmr::unordered_map<std::string, std::unique_ptr<celix::rsa::IImportServiceGuard>> _importedServices{&_memResource}; //key = endpoint id + std::pmr::unordered_map<long, std::unique_ptr<celix::rsa::IExportServiceGuard>> _exportedServices{&_memResource}; //key = service id #else std::unordered_map<std::string, std::shared_ptr<celix::rsa::IExportServiceFactory>> _exportServiceFactories{}; //key = service name std::unordered_map<std::string, std::shared_ptr<celix::rsa::IImportServiceFactory>> _importServiceFactories{}; //key = service name - std::unordered_map<std::string, std::unique_ptr<celix::rsa::IImportServiceRegistration>> _importedServices{}; //key = endpoint id - std::unordered_map<long, std::unique_ptr<celix::rsa::IExportServiceRegistration>> _exportedServices{}; //key = service id + std::unordered_map<std::string, std::unique_ptr<celix::rsa::IImportServiceGuard>> _importedServices{}; //key = endpoint id + std::unordered_map<long, std::unique_ptr<celix::rsa::IExportServiceGuard>> _exportedServices{}; //key = service id #endif std::vector<std::shared_ptr<celix::rsa::Endpoint>> _toBeImportedServices{}; std::vector<std::shared_ptr<const celix::Properties>> _toBeExportedServices{}; diff --git a/bundles/async_remote_services/admin/src/admin.cc b/bundles/async_remote_services/admin/src/admin.cc index 3b92867..ee90ccb 100644 --- a/bundles/async_remote_services/admin/src/admin.cc +++ b/bundles/async_remote_services/admin/src/admin.cc @@ -66,7 +66,7 @@ void celix::async_rsa::AsyncAdmin::removeEndpoint(const std::shared_ptr<celix::r return; } - std::shared_ptr<celix::rsa::IImportServiceRegistration> tmpStore{}; //to ensure destruction outside of lock + std::shared_ptr<celix::rsa::IImportServiceGuard> tmpStore{}; //to ensure destruction outside of lock { std::lock_guard l(_m); @@ -167,22 +167,18 @@ void celix::async_rsa::AsyncAdmin::removeService(const std::shared_ptr<void>& /* return; } - std::unique_ptr<celix::rsa::IExportServiceRegistration> tmpStore{}; //to ensure destruction outside of lock - { - std::lock_guard l(_m); + std::lock_guard l(_m); - auto instanceIt = _exportedServices.find(svcId); - if (instanceIt != end(_exportedServices)) { - tmpStore = std::move(instanceIt->second); - _exportedServices.erase(instanceIt); - } + auto instanceIt = _exportedServices.find(svcId); + if (instanceIt != end(_exportedServices)) { + _exportedServices.erase(instanceIt); + } - //remove to be exported endpoint (if present) - for (auto it = _toBeExportedServices.begin(); it != _toBeExportedServices.end(); ++it) { - if ((*it)->getAsBool(celix::SERVICE_ID, -1) == svcId) { - _toBeExportedServices.erase(it); - break; - } + //remove to be exported endpoint (if present) + for (auto it = _toBeExportedServices.begin(); it != _toBeExportedServices.end(); ++it) { + if ((*it)->getAsBool(celix::SERVICE_ID, -1) == svcId) { + _toBeExportedServices.erase(it); + break; } } } diff --git a/bundles/async_remote_services/rsa_spi/include/celix/rsa/IExportServiceFactory.h b/bundles/async_remote_services/rsa_spi/include/celix/rsa/IExportServiceFactory.h index fa5508c..7b386fe 100644 --- a/bundles/async_remote_services/rsa_spi/include/celix/rsa/IExportServiceFactory.h +++ b/bundles/async_remote_services/rsa_spi/include/celix/rsa/IExportServiceFactory.h @@ -24,12 +24,12 @@ namespace celix::rsa { /** - * @brief ExportServiceRegistration class which represent a (opaque) exported service. - * The lifetime of this object should be coupled with the lifetime of the exported service. - */ - class IExportServiceRegistration { + * @brief IExportServiceGuard class which represent a (opaque) exported service. + * If lifetime of this object expires it should remove the underlining exported service. + * */ + class IExportServiceGuard { public: - virtual ~IExportServiceRegistration() noexcept = default; + virtual ~IExportServiceGuard() noexcept = default; }; /** @@ -54,6 +54,6 @@ namespace celix::rsa { * @return A ExportService. * @throws celix::rsa::RemoteServicesException if the export failed. */ - virtual std::unique_ptr<celix::rsa::IExportServiceRegistration> exportService(const celix::Properties& serviceProperties) = 0; + virtual std::unique_ptr<celix::rsa::IExportServiceGuard> exportService(const celix::Properties& serviceProperties) = 0; }; } \ No newline at end of file diff --git a/bundles/async_remote_services/rsa_spi/include/celix/rsa/IImportServiceFactory.h b/bundles/async_remote_services/rsa_spi/include/celix/rsa/IImportServiceFactory.h index f62cf23..e3c15c2 100644 --- a/bundles/async_remote_services/rsa_spi/include/celix/rsa/IImportServiceFactory.h +++ b/bundles/async_remote_services/rsa_spi/include/celix/rsa/IImportServiceFactory.h @@ -24,12 +24,12 @@ namespace celix::rsa { /** - * @brief ImportServiceRegistration class which represent a (opaque) imported service. - * The lifetime of this object should be coupled with the lifetime of the imported service. + * @brief IImportServiceGuard class which represent a (opaque) imported service. + * If lifetime of this object expires it should remove the underlining imported service. */ - class IImportServiceRegistration { + class IImportServiceGuard { public: - virtual ~IImportServiceRegistration() noexcept = default; + virtual ~IImportServiceGuard() noexcept = default; }; /** @@ -54,6 +54,6 @@ namespace celix::rsa { * @return A ImportService. * @throws celix::rsa::RemoteServicesException if the import failed. */ - virtual std::unique_ptr<celix::rsa::IImportServiceRegistration> importService(const celix::rsa::Endpoint& endpoint) = 0; + virtual std::unique_ptr<celix::rsa::IImportServiceGuard> importService(const celix::rsa::Endpoint& endpoint) = 0; }; } \ No newline at end of file
