celix-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [celix] Oipo commented on a change in pull request #293: Feature/add build to svc dep creation
Date Tue, 15 Dec 2020 22:30:23 GMT

Oipo commented on a change in pull request #293:
URL: https://github.com/apache/celix/pull/293#discussion_r543713034



##########
File path: libs/framework/gtest/src/DependencyManagerTestSuite.cc
##########
@@ -0,0 +1,272 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <gtest/gtest.h>
+#include <atomic>
+
+#include "celix_api.h"
+
+class DependencyManagerTestSuite : public ::testing::Test {
+public:
+    celix_framework_t* fw = nullptr;
+    celix_bundle_context_t *ctx = nullptr;
+    celix_properties_t *properties = nullptr;
+
+    DependencyManagerTestSuite() {
+        properties = properties_create();
+        properties_set(properties, "LOGHELPER_ENABLE_STDOUT_FALLBACK", "true");
+        properties_set(properties, "org.osgi.framework.storage.clean", "onFirstInit");
+        properties_set(properties, "org.osgi.framework.storage", ".cacheBundleContextTestFramework");
+
+        fw = celix_frameworkFactory_createFramework(properties);
+        ctx = framework_getContext(fw);
+    }
+
+    ~DependencyManagerTestSuite() override {
+        celix_frameworkFactory_destroyFramework(fw);
+    }
+
+    DependencyManagerTestSuite(DependencyManagerTestSuite&&) = delete;
+    DependencyManagerTestSuite(const DependencyManagerTestSuite&) = delete;
+    DependencyManagerTestSuite& operator=(DependencyManagerTestSuite&&) = delete;
+    DependencyManagerTestSuite& operator=(const DependencyManagerTestSuite&) = delete;
+};
+
+TEST_F(DependencyManagerTestSuite, DmCreateComponent) {
+    auto *mng = celix_bundleContext_getDependencyManager(ctx);
+    auto *cmp = celix_dmComponent_create(ctx, "test1");
+    celix_dependencyManager_add(mng, cmp);
+
+    ASSERT_EQ(1, celix_dependencyManager_nrOfComponents(mng));
+    ASSERT_TRUE(celix_dependencyManager_allComponentsActive(mng));
+
+    cmp = celix_dmComponent_create(ctx, "test2");
+    celix_dependencyManager_add(mng, cmp);
+
+    ASSERT_EQ(2, celix_dependencyManager_nrOfComponents(mng));
+    ASSERT_TRUE(celix_dependencyManager_allComponentsActive(mng));
+}
+
+TEST_F(DependencyManagerTestSuite, TestCheckActive) {
+    auto *mng = celix_bundleContext_getDependencyManager(ctx);
+    auto *cmp = celix_dmComponent_create(ctx, "test1");
+
+    auto *dep = celix_dmServiceDependency_create();
+    celix_dmServiceDependency_setService(dep, "svcname", nullptr, nullptr);
+    celix_dmServiceDependency_setRequired(dep, true);
+    celix_dmComponent_addServiceDependency(cmp, dep); //required dep -> cmp not active
+
+
+    celix_dependencyManager_add(mng, cmp);
+    ASSERT_FALSE(celix_dependencyManager_areComponentsActive(mng));
+}
+
+class TestComponent {
+
+};
+
+TEST_F(DependencyManagerTestSuite, OnlyActiveAfterBuildCheck) {
+    celix::dm::DependencyManager dm{ctx};
+    EXPECT_EQ(0, dm.getNrOfComponents());
+
+    auto& cmp = dm.createComponent<TestComponent>(std::make_shared<TestComponent>(),
"test1");
+    EXPECT_EQ(0, dm.getNrOfComponents()); //dm not started yet / comp not build yet
+    EXPECT_TRUE(cmp.isValid());
+
+    cmp.build();
+    cmp.build(); //should be ok to call twice
+    EXPECT_EQ(1, dm.getNrOfComponents()); //cmp "build", so active
+
+    dm.clear();
+    dm.clear(); //should be ok to call twice
+    EXPECT_EQ(0, dm.getNrOfComponents()); //dm cleared so no components
+}
+
+TEST_F(DependencyManagerTestSuite, StartDmWillBuildCmp) {
+    celix::dm::DependencyManager dm{ctx};
+    EXPECT_EQ(0, dm.getNrOfComponents());
+
+    auto& cmp = dm.createComponent<TestComponent>(std::make_shared<TestComponent>(),
"test1");
+    EXPECT_EQ(0, dm.getNrOfComponents()); //dm not started yet / comp not build yet
+    EXPECT_TRUE(cmp.isValid());
+
+    dm.start();
+    EXPECT_EQ(1, dm.getNrOfComponents()); //cmp "build", so active
+
+    dm.stop();
+    EXPECT_EQ(0, dm.getNrOfComponents()); //dm cleared so no components
+}
+
+struct TestService {
+    void *handle;
+};
+
+class Cmp1 : public TestService {
+
+};
+
+class Cmp2 : public TestService {
+public:
+    explicit Cmp2(const std::string& name) {
+        std::cout << "usage arg: " << name;
+    }
+};
+
+TEST_F(DependencyManagerTestSuite, CreateComponentVariant) {
+    celix::dm::DependencyManager dm{ctx};
+
+    dm.createComponent<Cmp1>().addInterface<TestService>(); //lazy
+    dm.createComponent(std::unique_ptr<Cmp1>{new Cmp1}).addInterface<TestService>();
//with unique ptr
+    dm.createComponent(std::make_shared<Cmp1>()).addInterface<TestService>();
//with shared ptr
+    dm.createComponent(Cmp1{}).addInterface<TestService>(); //with value
+
+    //dm.createComponent<Cmp2>(); //Does not compile ->  no default ctor
+    dm.createComponent(std::unique_ptr<Cmp2>{new Cmp2{"a"}}).addInterface<TestService>();
//with unique ptr
+    dm.createComponent(std::make_shared<Cmp2>("b")).addInterface<TestService>();;
//with shared ptr
+    dm.createComponent(Cmp2{"c"}).addInterface<TestService>();; //with value
+
+    dm.start();
+}
+
+TEST_F(DependencyManagerTestSuite, AddSvcProvideAfterBuild) {
+    celix::dm::DependencyManager dm{ctx};
+    EXPECT_EQ(0, dm.getNrOfComponents());
+
+    auto& cmp = dm.createComponent<TestComponent>(std::make_shared<TestComponent>(),
"test1");
+    EXPECT_EQ(0, dm.getNrOfComponents()); //dm not started yet / comp not build yet
+    EXPECT_TRUE(cmp.isValid());
+
+    cmp.build();
+    EXPECT_EQ(1, dm.getNrOfComponents()); //cmp "build", so active
+
+    TestService svc{nullptr};
+    cmp.addCInterface(&svc, "TestService");
+
+    long svcId = celix_bundleContext_findService(ctx, "TestService");
+    EXPECT_EQ(-1, svcId); //not build -> not found
+
+    cmp.build();
+    cmp.build(); //should be ok to call twice
+    svcId = celix_bundleContext_findService(ctx, "TestService");
+    EXPECT_GT(svcId, -1); //(re)build -> found
+
+    dm.clear();
+    EXPECT_EQ(0, dm.getNrOfComponents()); //dm cleared so no components
+    svcId = celix_bundleContext_findService(ctx, "TestService");
+    EXPECT_EQ(svcId, -1); //cleared -> not found
+}
+
+TEST_F(DependencyManagerTestSuite, BuildSvcProvide) {
+    celix::dm::DependencyManager dm{ctx};
+    EXPECT_EQ(0, dm.getNrOfComponents());
+
+    auto& cmp = dm.createComponent<Cmp1>(std::make_shared<Cmp1>(), "test2");
+    EXPECT_EQ(0, dm.getNrOfComponents()); //dm not started yet / comp not build yet
+    EXPECT_TRUE(cmp.isValid());
+
+    cmp.build();
+    EXPECT_EQ(1, dm.getNrOfComponents()); //cmp "build", so active
+
+    TestService svc{nullptr};
+    cmp.createProvidedCService(&svc, "CTestService").addProperty("key1", "val1").addProperty("key2",
3);
+
+    long svcId = celix_bundleContext_findService(ctx, "CTestService");
+    EXPECT_EQ(-1, svcId); //not build -> not found
+
+    cmp.build();
+    cmp.build(); //should be ok to call twice
+    svcId = celix_bundleContext_findService(ctx, "CTestService");
+    EXPECT_GT(svcId, -1); //(re)build -> found
+
+    celix_service_filter_options_t opts{};
+    opts.serviceName = "CTestService";
+    opts.filter = "(&(key1=val1)(key2=3))";
+    svcId = celix_bundleContext_findServiceWithOptions(ctx, &opts);
+    EXPECT_GT(svcId, -1); //found, so properties present
+
+    celix::dm::Properties props{};
+    props["key1"] = "value";
+    cmp.createProvidedService<TestService>().setProperties(props).setVersion("1.0.0").build();
+
+    opts.serviceName = "TestService";
+    opts.filter = "(key1=value)";
+    opts.ignoreServiceLanguage = true;
+    svcId = celix_bundleContext_findServiceWithOptions(ctx, &opts);
+    EXPECT_GT(svcId, -1); //found, so properties present
+
+    dm.clear();
+    EXPECT_EQ(0, dm.getNrOfComponents()); //dm cleared so no components
+    svcId = celix_bundleContext_findService(ctx, "CTestService");
+    EXPECT_EQ(svcId, -1); //cleared -> not found
+}
+
+TEST_F(DependencyManagerTestSuite, AddSvcDepAfterBuild) {
+    celix::dm::DependencyManager dm{ctx};
+    EXPECT_EQ(0, dm.getNrOfComponents());
+
+    auto& cmp = dm.createComponent<TestComponent>(std::make_shared<TestComponent>(),
"test1");
+    EXPECT_EQ(0, dm.getNrOfComponents()); //dm not started yet / comp not build yet
+    EXPECT_TRUE(cmp.isValid());
+
+    cmp.build();
+    cmp.build(); //should be ok to call twice
+    EXPECT_EQ(1, dm.getNrOfComponents()); //cmp "build", so active
+
+    std::atomic<int> count{0};
+    auto& dep = cmp.createCServiceDependency<TestService>("TestService")
+            .setCallbacks([&count](const TestService*, celix::dm::Properties&&)
{
+                count++;
+            });
+
+    TestService svc{nullptr};
+    long svcId = celix_bundleContext_registerService(ctx, &svc, "TestService", nullptr);
+    long svcId2 = celix_bundleContext_registerService(ctx, &svc, "AnotherService", nullptr);
//note should not be found.
+
+    ASSERT_EQ(0, count); //service dep not yet build -> so no set call
+
+    dep.build();
+    dep.build(); //should be ok to call twice
+    ASSERT_EQ(1, count); //service dep build -> so count is 1;
+
+    //create another service dep
+    cmp.createCServiceDependency<TestService>("TestService")
+            .setCallbacks([&count](const TestService*, celix::dm::Properties&&)
{
+                count++;
+            });
+    ASSERT_EQ(1, count); //new service dep not yet build -> so count still 1
+
+    cmp.build(); //cmp build, which will build svc dep
+    ASSERT_EQ(2, count); //new service dep build -> so count is 2
+
+    celix_bundleContext_unregisterService(ctx, svcId);
+    celix_bundleContext_unregisterService(ctx, svcId2);
+}
+
+TEST_F(DependencyManagerTestSuite, InCompleteBuildShouldNotLeak) {
+    celix::dm::DependencyManager dm{ctx};
+    dm.createComponent<TestComponent>(std::make_shared<TestComponent>(), "test1");
//note not build
+
+    auto& cmp2 = dm.createComponent<Cmp1>(std::make_shared<Cmp1>(), "test2").build();
+    cmp2.createCServiceDependency<TestService>("TestService").setFilter("(key=value");
//note not build
+    cmp2.createServiceDependency<TestService>().setFilter("(key=value)"); //note not
build
+
+    TestService svc{nullptr};
+    cmp2.createProvidedCService(&svc, "CTestService").addProperty("key1", "val1"); //note
not build
+    cmp2.createProvidedService<TestService>().setVersion("1.0.0").build(); //note not
build

Review comment:
       "note not build".
   
   ... but this lines builds :laughing: 

##########
File path: libs/framework/include/celix/dm/ServiceDependency.h
##########
@@ -59,20 +61,20 @@ namespace celix { namespace dm {
             }
         }
     public:
-        BaseServiceDependency(bool v)  : valid{v} {
+        BaseServiceDependency(celix_dm_component_t* c, bool v)  : cCmp{c}, valid{v} {
             if (this->valid) {
                 this->cServiceDep = celix_dmServiceDependency_create();
                 //NOTE using suspend as default strategy
                 celix_dmServiceDependency_setStrategy(this->cServiceDep,  DM_SERVICE_DEPENDENCY_STRATEGY_SUSPEND);
             }
         }
 
-        virtual ~BaseServiceDependency() = default;
+        virtual ~BaseServiceDependency() noexcept;

Review comment:
       Fun fact: implicit noexcept on destructors behave differently than on functions. For
normal functions, not specifying noexcept results in the compiler adding `noexcept(false)`,
which means that the compiler should assume exceptions can potentially be thrown and some
optimizations should not be done (though I would not be surprised if compilers check if a
function throws and optimizes anyway.)
   
   Noexcept on destructors are defined differently: if it is missing, the compiler should
check if all its parent class and all member variables have noexcept destructors. If so, mark
destructor implicitly as `noexcept(true)` otherwise `noexcept(false)`.
   
   Something similar happens for default defined move/copy/regular constructors/operators.
If you happen to declare a copy constructor without explicitly telling the compiler if it
is noexcept or not, it will try to figure it out for you.
   
   Given that `BaseServiceDependency` has no throwing parents or member variables, it was
already marked noexcept. Though I like explicit noexcept. So just wanted to share this tidbit
of knowledge.

##########
File path: libs/framework/include/celix/dm/Component_Impl.h
##########
@@ -163,17 +181,33 @@ bool Component<T>::isValid() const {
     return this->bundleContext() != nullptr;
 }
 
+
+template<typename T>
+static
+typename std::enable_if<std::is_default_constructible<T>::value, T*>::type
+createInstance() {
+    return new T{};
+}
+
+template<typename T>
+static
+typename std::enable_if<!std::is_default_constructible<T>::value, T*>::type

Review comment:
       :+1: 

##########
File path: libs/framework/include/celix/dm/ServiceDependency_Impl.h
##########
@@ -22,11 +22,26 @@
 #include <cstring>
 #include "celix_constants.h"
 #include "celix_properties.h"
+#include "ServiceDependency.h"
+
 
 using namespace celix::dm;
 
+inline void BaseServiceDependency::runBuild() {
+    bool alreadyAdded = depAddedToCmp.exchange(true);
+    if (!alreadyAdded) {
+        celix_dmComponent_addServiceDependency(cCmp, cServiceDep);
+    }
+}
+
+inline BaseServiceDependency::~BaseServiceDependency() noexcept {
+    if (!depAddedToCmp) {

Review comment:
       Can you double check if the `!` is correct here?

##########
File path: libs/framework/include/celix/dm/ProvidedService.h
##########
@@ -0,0 +1,97 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#pragma once
+
+#include <string>
+#include <memory>
+
+#include "celix_api.h"
+#include "celix/dm/Properties.h"
+
+namespace celix { namespace dm {
+
+    class BaseProvidedService {
+    public:
+        BaseProvidedService(celix_dm_component_t* _cmp, std::string svcName, void* svc, bool
_cppService);
+
+        BaseProvidedService(BaseProvidedService&&) = delete;
+        BaseProvidedService& operator=(BaseProvidedService&&) = delete;
+        BaseProvidedService(const BaseProvidedService&) = delete;
+        BaseProvidedService& operator=(const BaseProvidedService&) = delete;
+
+        const std::string& getName() const;
+        const std::string& getVersion() const;
+        bool isCppService() const;
+        void* getService() const;
+        const celix::dm::Properties& getProperties() const;
+
+        void runBuild();
+    protected:
+        celix_dm_component_t* cCmp;
+        std::string svcName;
+        void* svcPointer;
+        bool cppService;
+        std::string svcVersion{};
+        celix::dm::Properties properties{};
+        bool provideAddedToCmp{false};
+    };
+
+    template<typename T, typename I>
+    class ProvidedService : public BaseProvidedService {
+    public:
+        ProvidedService(celix_dm_component_t* _cmp, std::string svcName, I* svc, bool _cppService);
+
+        /**
+         * Set the version of the interface
+         */
+        ProvidedService<T,I>& setVersion(std::string v);
+
+        /**
+         * Set the properties of the interface. Note this will reset the already set / added
properties
+         */
+        ProvidedService<T,I>& setProperties(celix::dm::Properties);
+
+        /**
+         * Add a single property to the service properties.

Review comment:
       I think changing all the addProperty comments to `Add/override ...` makes it more clear.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



Mime
View raw message