Skip to content

Commit ec93d1d

Browse files
authored
Merge pull request #839 from xuzhenbao/add_remote_service_ranking
Add remote service ranking
2 parents b1bb0d6 + 7eef3c4 commit ec93d1d

7 files changed

Lines changed: 787 additions & 165 deletions

File tree

bundles/remote_services/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ The Remote Service Admin Service subproject contains an adapted implementation o
2727

2828
The topology manager decides which services should be imported and exported according to a defined policy. Currently, only one policy is implemented in Celix, the *promiscuous* policy, which simply imports and exports all services.
2929

30-
| **Bundle** | `Celix::rsa_topology_manager` |
31-
|--|-----------------------------------------|
32-
| **Configuration** | *None* |
30+
| **Bundle** | `Celix::rsa_topology_manager` |
31+
|--|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
32+
| **Configuration** | `CELIX_RSA_IMPORTED_SERVICE_RANKING_OFFSETS`: defines the ranking offsets for imported services. The value of this property is a comma-separated list of `<config type>=<ranking offset>`. The `<config type>` is the value of the `service.exported.configs` property of the service registration, and the `<ranking offset>` is an integer that is added to the ranking of the imported service. This allows you to influence the ranking of imported services based on their configuration type. For example, if you want to give a higher ranking to remote services with configuration type "shm" than remote services with configuration type "http", you can set this property to `http=-2,shm=-1`. |
3333

3434
### Remote Service Admin
3535

bundles/remote_services/topology_manager/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ add_celix_bundle(rsa_topology_manager
2222
src/topology_manager.c
2323
src/scope.c
2424
src/activator.c
25-
VERSION 0.9.0
25+
VERSION 0.10.0
2626
SYMBOLIC_NAME "apache_celix_rs_topology_manager"
2727
GROUP "Celix/RSA"
2828
NAME "Apache Celix RS Topology Manager"

bundles/remote_services/topology_manager/gtest/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ if (EI_TESTS)
4949
Celix::framework
5050
Celix::threads_ei
5151
# Celix::bundle_ctx_ei
52-
# Celix::string_hash_map_ei
52+
Celix::string_hash_map_ei
5353
Celix::long_hash_map_ei
5454
Celix::array_list_ei
5555
Celix::properties_ei
5656
Celix::utils_ei
5757
Celix::malloc_ei
58+
Celix::filter_ei
59+
Celix::asprintf_ei
5860
GTest::gtest
5961
GTest::gtest_main
6062
)

bundles/remote_services/topology_manager/gtest/src/TopologyManagerErrorInjectionTestSuite.cc

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
#include "celix_properties_ei.h"
2727
#include "celix_utils_ei.h"
2828
#include "celix_threads_ei.h"
29+
#include "celix_string_hash_map_ei.h"
30+
#include "celix_filter_ei.h"
31+
#include "asprintf_ei.h"
2932

3033
class TopologyManagerCreatingErrorInjectionTestSuite : public ::testing::Test {
3134
public:
@@ -45,6 +48,10 @@ class TopologyManagerCreatingErrorInjectionTestSuite : public ::testing::Test {
4548
celix_ei_expect_calloc(nullptr, 0, nullptr);
4649
celix_ei_expect_celixThreadMutex_create(nullptr, 0, 0);
4750
celix_ei_expect_celix_longHashMap_create(nullptr, 0, nullptr);
51+
celix_ei_expect_celix_stringHashMap_create(nullptr, 0, nullptr);
52+
celix_ei_expect_celix_stringHashMap_putLong(nullptr, 1, 0);
53+
celix_ei_expect_celix_utils_strdup(nullptr, 0, nullptr);
54+
celix_ei_expect_celix_stringHashMap_createWithOptions(nullptr, 0, nullptr);
4855
}
4956

5057
std::shared_ptr<celix_framework_t> fw{};
@@ -60,6 +67,42 @@ TEST_F(TopologyManagerCreatingErrorInjectionTestSuite, AllocingMemoryErrorTest)
6067
EXPECT_EQ(CELIX_ENOMEM, status);
6168
}
6269

70+
TEST_F(TopologyManagerCreatingErrorInjectionTestSuite, CreatingImportedServiceRankingOffsetMapErrorTest) {
71+
celix_ei_expect_celix_stringHashMap_create((void*)topologyManager_create, 0, nullptr);
72+
void *scope{};
73+
topology_manager_t *tmPtr{};
74+
auto status = topologyManager_create(ctx.get(), logHelper.get(), &tmPtr, &scope);
75+
EXPECT_EQ(CELIX_ENOMEM, status);
76+
}
77+
78+
TEST_F(TopologyManagerCreatingErrorInjectionTestSuite, ImportedServiceRankingOffsetStringDuplicationErrorTest) {
79+
setenv("CELIX_RSA_IMPORTED_SERVICE_RANKING_OFFSETS", "configType1=10,configType2=20", 1);
80+
celix_ei_expect_celix_utils_strdup((void*)topologyManager_create, 1, nullptr);
81+
void *scope{};
82+
topology_manager_t *tmPtr{};
83+
auto status = topologyManager_create(ctx.get(), logHelper.get(), &tmPtr, &scope);
84+
EXPECT_EQ(CELIX_ENOMEM, status);
85+
unsetenv("CELIX_RSA_IMPORTED_SERVICE_RANKING_OFFSETS");
86+
}
87+
88+
TEST_F(TopologyManagerCreatingErrorInjectionTestSuite, AddingImportedServiceRankingOffsetErrorTest) {
89+
setenv("CELIX_RSA_IMPORTED_SERVICE_RANKING_OFFSETS", "configType1=10,configType2=20", 1);
90+
celix_ei_expect_celix_stringHashMap_putLong((void*)topologyManager_create, 1, ENOMEM);
91+
void *scope{};
92+
topology_manager_t *tmPtr{};
93+
auto status = topologyManager_create(ctx.get(), logHelper.get(), &tmPtr, &scope);
94+
EXPECT_EQ(ENOMEM, status);
95+
unsetenv("CELIX_RSA_IMPORTED_SERVICE_RANKING_OFFSETS");
96+
}
97+
98+
TEST_F(TopologyManagerCreatingErrorInjectionTestSuite, CreatingImportedServiceMapErrorTest) {
99+
celix_ei_expect_celix_stringHashMap_createWithOptions((void*)topologyManager_create, 0, nullptr);
100+
void *scope{};
101+
topology_manager_t *tmPtr{};
102+
auto status = topologyManager_create(ctx.get(), logHelper.get(), &tmPtr, &scope);
103+
EXPECT_EQ(CELIX_ENOMEM, status);
104+
}
105+
63106
TEST_F(TopologyManagerCreatingErrorInjectionTestSuite, CreatingMutexErrorTest) {
64107
celix_ei_expect_celixThreadMutex_create((void*)topologyManager_create, 0, CELIX_ENOMEM);
65108
void *scope{};
@@ -121,6 +164,12 @@ class TopologyManagerErrorInjectionTestSuite : public TopologyManagerTestSuiteBa
121164
celix_ei_expect_celix_properties_set(nullptr, 0, 0);
122165
celix_ei_expect_celix_utils_strdup(nullptr, 0, nullptr);
123166
celix_ei_expect_celix_arrayList_add(nullptr, 0, 0);
167+
celix_ei_expect_celix_filter_create(nullptr, 0, nullptr);
168+
celix_ei_expect_celix_properties_getAsStringArrayList(nullptr, 0, 0);
169+
celix_ei_expect_celix_properties_setLong(nullptr, 0, 0);
170+
celix_ei_expect_asprintf(nullptr, 0, 0);
171+
celix_ei_expect_celix_arrayList_createWithOptions(nullptr, 0, nullptr);
172+
celix_ei_expect_celix_stringHashMap_put(nullptr, 0, 0);
124173
}
125174

126175
void TestExportServiceFailure(void (*errorInject)(void)) {
@@ -263,5 +312,118 @@ TEST_F(TopologyManagerErrorInjectionTestSuite, AddDynamicIpEndpointToListErrorTe
263312
});
264313
}
265314

315+
TEST_F(TopologyManagerErrorInjectionTestSuite, CreatingFilterErrorWhenImportScopeChangedTest) {
316+
celix_ei_expect_celix_filter_create(CELIX_EI_UNKNOWN_CALLER, 0, nullptr, 2);
317+
auto status = tm_addImportScope(tms.get(), (char*)"(service.imported.configs=tm_test_config_type)");
318+
EXPECT_EQ(CELIX_ILLEGAL_ARGUMENT, status);
319+
}
320+
321+
TEST_F(TopologyManagerErrorInjectionTestSuite, AllocingMemoryForImportedServiceEntryErrorTest) {
322+
TestImportService([](topology_manager_t* tm, service_reference_pt, void*, endpoint_description_t *importEndpoint) {
323+
celix_ei_expect_calloc(CELIX_EI_UNKNOWN_CALLER, 0, nullptr);
324+
auto status = topologyManager_addImportedService(tm, importEndpoint, nullptr);
325+
EXPECT_EQ(CELIX_ENOMEM, status);
326+
327+
status = topologyManager_removeImportedService(tm, importEndpoint, nullptr);
328+
EXPECT_EQ(CELIX_SUCCESS, status);
329+
});
330+
}
331+
332+
TEST_F(TopologyManagerErrorInjectionTestSuite, GettingServiceImportedConfigsErrorTest) {
333+
TestImportService([](topology_manager_t* tm, service_reference_pt, void*, endpoint_description_t *importEndpoint) {
334+
celix_ei_expect_celix_properties_getAsStringArrayList(CELIX_EI_UNKNOWN_CALLER, 0, ENOMEM);
335+
auto status = topologyManager_addImportedService(tm, importEndpoint, nullptr);
336+
EXPECT_EQ(ENOMEM, status);
337+
338+
celix_properties_unset(importEndpoint->properties, CELIX_RSA_SERVICE_IMPORTED_CONFIGS);
339+
status = topologyManager_addImportedService(tm, importEndpoint, nullptr);
340+
EXPECT_EQ(CELIX_ENOMEM, status);
341+
});
342+
}
343+
344+
TEST_F(TopologyManagerErrorInjectionTestSuite, CopyImportedServicePropertiesErrorTest) {
345+
TestImportService([](topology_manager_t* tm, service_reference_pt, void*, endpoint_description_t *importEndpoint) {
346+
celix_ei_expect_celix_properties_copy(CELIX_EI_UNKNOWN_CALLER, 0, nullptr);//called in endpointDescription_clone
347+
auto status = topologyManager_addImportedService(tm, importEndpoint, nullptr);
348+
EXPECT_EQ(ENOMEM, status);
349+
});
350+
}
351+
352+
TEST_F(TopologyManagerErrorInjectionTestSuite, SettingImportedServiceRankingErrorTest) {
353+
setenv("CELIX_RSA_IMPORTED_SERVICE_RANKING_OFFSETS", "tm_test_config_type=10", 1);
354+
tm.reset();
355+
void* scope = nullptr;
356+
topology_manager_t* tmPtr{};
357+
auto status = topologyManager_create(ctx.get(), logHelper.get(), &tmPtr, &scope);
358+
EXPECT_EQ(status, CELIX_SUCCESS);
359+
tm = std::shared_ptr<topology_manager_t>{tmPtr, [](auto t) {topologyManager_destroy(t);}};
360+
361+
TestImportService([](topology_manager_t* tm, service_reference_pt, void*, endpoint_description_t *importEndpoint) {
362+
celix_ei_expect_celix_properties_setLong(CELIX_EI_UNKNOWN_CALLER, 0, ENOMEM);//called in endpointDescription_clone
363+
auto status = topologyManager_addImportedService(tm, importEndpoint, nullptr);
364+
EXPECT_EQ(ENOMEM, status);
365+
});
366+
367+
unsetenv("CELIX_RSA_IMPORTED_SERVICE_RANKING_OFFSETS");
368+
}
369+
370+
371+
TEST_F(TopologyManagerErrorInjectionTestSuite, CreatingImportsMapFailureTest) {
372+
TestImportService([](topology_manager_t* tm, service_reference_pt, void*, endpoint_description_t *importEndpoint) {
373+
celix_ei_expect_celix_longHashMap_create(CELIX_EI_UNKNOWN_CALLER, 0, nullptr);
374+
auto status = topologyManager_addImportedService(tm, importEndpoint, nullptr);
375+
EXPECT_EQ(ENOMEM, status);
376+
377+
status = topologyManager_removeImportedService(tm, importEndpoint, nullptr);
378+
EXPECT_EQ(CELIX_SUCCESS, status);
379+
});
380+
}
381+
382+
TEST_F(TopologyManagerErrorInjectionTestSuite, AddingImportedServiceToMapFailureTest) {
383+
TestImportService([](topology_manager_t* tm, service_reference_pt, void*, endpoint_description_t *importEndpoint) {
384+
celix_ei_expect_celix_stringHashMap_put(CELIX_EI_UNKNOWN_CALLER, 0, ENOMEM);
385+
auto status = topologyManager_addImportedService(tm, importEndpoint, nullptr);
386+
EXPECT_EQ(ENOMEM, status);
387+
388+
status = topologyManager_removeImportedService(tm, importEndpoint, nullptr);
389+
EXPECT_EQ(CELIX_SUCCESS, status);
390+
});
391+
}
392+
393+
TEST_F(TopologyManagerErrorInjectionTestSuite, AddingImportedRegistrationToMapFailureTest) {
394+
TestImportService([this](topology_manager_t* tm, service_reference_pt rsaSvcRef, void* rsaSvc, endpoint_description_t *importEndpoint) {
395+
auto status = topologyManager_rsaAdded(tm, rsaSvcRef, rsaSvc);
396+
EXPECT_EQ(CELIX_SUCCESS, status);
397+
celix_ei_expect_celix_longHashMap_put(CELIX_EI_UNKNOWN_CALLER, 0, ENOMEM);
398+
status = topologyManager_addImportedService(tm, importEndpoint, nullptr);
399+
EXPECT_EQ(CELIX_SUCCESS, status);
400+
celix_service_filter_options_t opts{};
401+
opts.filter = (char *)"(service.imported.configs=tm_test_config_type)";
402+
auto svcId = celix_bundleContext_findServiceWithOptions(ctx.get(), &opts);
403+
EXPECT_EQ(svcId, -1);
404+
405+
status = topologyManager_removeImportedService(tm, importEndpoint, nullptr);
406+
EXPECT_EQ(CELIX_SUCCESS, status);
407+
status = topologyManager_rsaRemoved(tm, rsaSvcRef, rsaSvc);
408+
EXPECT_EQ(CELIX_SUCCESS, status);
409+
});
410+
}
266411

412+
TEST_F(TopologyManagerErrorInjectionTestSuite, AddingImportedRegistrationToMapFailureWhenAddRsaTest) {
413+
TestImportService([this](topology_manager_t* tm, service_reference_pt rsaSvcRef, void* rsaSvc, endpoint_description_t *importEndpoint) {
414+
auto status = topologyManager_addImportedService(tm, importEndpoint, nullptr);
415+
EXPECT_EQ(CELIX_SUCCESS, status);
416+
celix_ei_expect_celix_longHashMap_put(CELIX_EI_UNKNOWN_CALLER, 0, ENOMEM, 2);
417+
status = topologyManager_rsaAdded(tm, rsaSvcRef, rsaSvc);
418+
EXPECT_EQ(CELIX_SUCCESS, status);
419+
celix_service_filter_options_t opts{};
420+
opts.filter = (char *)"(service.imported.configs=tm_test_config_type)";
421+
auto svcId = celix_bundleContext_findServiceWithOptions(ctx.get(), &opts);
422+
EXPECT_EQ(svcId, -1);
267423

424+
status = topologyManager_rsaRemoved(tm, rsaSvcRef, rsaSvc);
425+
EXPECT_EQ(CELIX_SUCCESS, status);
426+
status = topologyManager_removeImportedService(tm, importEndpoint, nullptr);
427+
EXPECT_EQ(CELIX_SUCCESS, status);
428+
});
429+
}

0 commit comments

Comments
 (0)