celix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pnol...@apache.org
Subject [celix] branch develop updated: Updates dependency manager is active check and adds initial test for dm
Date Tue, 10 Sep 2019 19:56:43 GMT
This is an automated email from the ASF dual-hosted git repository.

pnoltes pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/celix.git


The following commit(s) were added to refs/heads/develop by this push:
     new 53d0813  Updates dependency manager is active check and adds initial test for dm
53d0813 is described below

commit 53d0813fe5bd5c4da620e715837ebf418d723fb9
Author: Pepijn Noltes <pepijnnoltes@gmail.com>
AuthorDate: Tue Sep 10 21:56:15 2019 +0200

    Updates dependency manager is active check and adds initial test for dm
---
 libs/framework/include/celix_dependency_manager.h | 21 +++++--
 libs/framework/src/dm_component_impl.c            |  2 +-
 libs/framework/src/dm_dependency_manager_impl.c   | 34 ++++++++---
 libs/framework/tst/CMakeLists.txt                 |  1 +
 libs/framework/tst/dm_tests.cpp                   | 73 +++++++++++++++++++++++
 5 files changed, 118 insertions(+), 13 deletions(-)

diff --git a/libs/framework/include/celix_dependency_manager.h b/libs/framework/include/celix_dependency_manager.h
index 82ee3a1..a817ae4 100644
--- a/libs/framework/include/celix_dependency_manager.h
+++ b/libs/framework/include/celix_dependency_manager.h
@@ -70,11 +70,6 @@ celix_dependency_manager_info_t* celix_dependencyManager_createInfo(celix_depend
 celix_array_list_t * /*celix_dependency_manager_info_t entries*/ celix_dependencyManager_createInfos(celix_dependency_manager_t
*manager);
 
 /**
- * Check if all components - for all bundles - are active (all required dependencies resolved).
- */
-bool celix_dependencyManager_allComponentsActive(celix_dependency_manager_t *manager);
-
-/**
  * Destroys a DM info struct.
  */
 void celix_dependencyManager_destroyInfo(celix_dependency_manager_t *manager, celix_dependency_manager_info_t
*info);
@@ -84,6 +79,22 @@ void celix_dependencyManager_destroyInfo(celix_dependency_manager_t *manager,
ce
  */
 void celix_dependencyManager_destroyInfos(celix_dependency_manager_t *manager, celix_array_list_t
* infos /*entries celix_dependency_manager_info_t*/);
 
+
+/**
+ * Check if all components for the bundle of the dependency manager are active (all required
dependencies resolved).
+ */
+bool celix_dependencyManager_areComponentsActive(celix_dependency_manager_t *manager);
+
+/**
+ * Check if all components - for all bundles - are active (all required dependencies resolved).
+ */
+bool celix_dependencyManager_allComponentsActive(celix_dependency_manager_t *manager);
+
+/**
+ * Return the nr of components for this dependency manager
+ */
+size_t celix_dependencyManager_nrOfComponents(celix_dependency_manager_t *manager);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/libs/framework/src/dm_component_impl.c b/libs/framework/src/dm_component_impl.c
index 3897fc9..1187928 100644
--- a/libs/framework/src/dm_component_impl.c
+++ b/libs/framework/src/dm_component_impl.c
@@ -1536,7 +1536,7 @@ void celix_dmComponent_destroyComponentInfo(dm_component_info_pt info)
{
 
 bool celix_dmComponent_isActive(celix_dm_component_t *component) {
     pthread_mutex_lock(&component->mutex);
-    bool active = component->active;
+    bool active = component->state == DM_CMP_STATE_TRACKING_OPTIONAL;
     pthread_mutex_unlock(&component->mutex);
     return active;
 }
diff --git a/libs/framework/src/dm_dependency_manager_impl.c b/libs/framework/src/dm_dependency_manager_impl.c
index 3b0aa0f..f63eaf3 100644
--- a/libs/framework/src/dm_dependency_manager_impl.c
+++ b/libs/framework/src/dm_dependency_manager_impl.c
@@ -169,13 +169,11 @@ static void celix_dm_allComponentsActiveCallback(void *handle, const
celix_bundl
 	celix_bundle_context_t *context = NULL;
 	bundle_getContext((celix_bundle_t*)bnd, &context);
 	celix_dependency_manager_t *mng = celix_bundleContext_getDependencyManager(context);
-	int size = celix_arrayList_size(mng->components);
-	for (int i = 0; i < size; i += 1) {
-		celix_dm_component_t *cmp = celix_arrayList_get(mng->components, i);
-		if (!celix_dmComponent_isActive(cmp)) {
-			*allActivePtr = false;
-		}
-	}
+    bool allActive = celix_dependencyManager_areComponentsActive(mng);
+    if (!allActive) {
+        *allActivePtr = false;
+    }
+    celixThreadMutex_lock(&mng->mutex);
 }
 
 bool celix_dependencyManager_allComponentsActive(celix_dependency_manager_t *manager) {
@@ -184,6 +182,28 @@ bool celix_dependencyManager_allComponentsActive(celix_dependency_manager_t
*man
 	return allActive;
 }
 
+size_t celix_dependencyManager_nrOfComponents(celix_dependency_manager_t *mng) {
+    celixThreadMutex_lock(&mng->mutex);
+    size_t nr = (size_t)celix_arrayList_size(mng->components);
+    celixThreadMutex_unlock(&mng->mutex);
+    return nr;
+}
+
+bool celix_dependencyManager_areComponentsActive(celix_dependency_manager_t *mng) {
+    bool allActive = true;
+    celixThreadMutex_lock(&mng->mutex);
+    int size = celix_arrayList_size(mng->components);
+    for (int i = 0; i < size; i += 1) {
+        celix_dm_component_t *cmp = celix_arrayList_get(mng->components, i);
+        if (!celix_dmComponent_isActive(cmp)) {
+            allActive = false;
+            break;
+        }
+    }
+    celixThreadMutex_unlock(&mng->mutex);
+    return allActive;
+}
+
 void celix_dependencyManager_destroyInfo(celix_dependency_manager_t *manager __attribute__((unused)),
celix_dependency_manager_info_t *info) {
 	for (int i = 0; i < celix_arrayList_size(info->components); i += 1) {
 		celix_dm_component_info_t *cmpinfo = (dm_component_info_pt)arrayList_get(info->components,
i);
diff --git a/libs/framework/tst/CMakeLists.txt b/libs/framework/tst/CMakeLists.txt
index 76d0990..7bf8af6 100644
--- a/libs/framework/tst/CMakeLists.txt
+++ b/libs/framework/tst/CMakeLists.txt
@@ -27,6 +27,7 @@ add_executable(test_framework
     multiple_frameworks_test.cpp
     bundle_context_bundles_tests.cpp
     bundle_context_services_test.cpp
+    dm_tests.cpp
 )
 
 target_link_libraries(test_framework Celix::framework ${CURL_LIBRARIES} ${CPPUTEST_LIBRARY})
diff --git a/libs/framework/tst/dm_tests.cpp b/libs/framework/tst/dm_tests.cpp
new file mode 100644
index 0000000..4470aa4
--- /dev/null
+++ b/libs/framework/tst/dm_tests.cpp
@@ -0,0 +1,73 @@
+/**
+ *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 "celix_api.h"
+
+#include <CppUTest/TestHarness.h>
+#include <CppUTest/CommandLineTestRunner.h>
+
+
+TEST_GROUP(DepenencyManagerTests) {
+    framework_t* fw = nullptr;
+    bundle_context_t *ctx = nullptr;
+    properties_t *properties = nullptr;
+
+    void setup() {
+        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);
+    }
+
+    void teardown() {
+        celix_frameworkFactory_destroyFramework(fw);
+    }
+};
+
+TEST(DepenencyManagerTests, DmCreateComponent) {
+    auto *mng = celix_bundleContext_getDependencyManager(ctx);
+    auto *cmp = celix_dmComponent_create(ctx, "test1");
+    celix_dependencyManager_add(mng, cmp);
+
+    CHECK_EQUAL(1, celix_dependencyManager_nrOfComponents(mng));
+    CHECK_TRUE(celix_dependencyManager_allComponentsActive(mng));
+
+    cmp = celix_dmComponent_create(ctx, "test2");
+    celix_dependencyManager_add(mng, cmp);
+
+    CHECK_EQUAL(2, celix_dependencyManager_nrOfComponents(mng));
+    CHECK_TRUE(celix_dependencyManager_allComponentsActive(mng));
+}
+
+TEST(DepenencyManagerTests, 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);
+    CHECK_FALSE(celix_dependencyManager_areComponentsActive(mng));
+}


Mime
View raw message