From 46d630a03b01c26b0464c32e0522aa9a0d22f66b Mon Sep 17 00:00:00 2001 From: rex-schilasky <49162693+rex-schilasky@users.noreply.github.com> Date: Mon, 15 Apr 2024 16:57:23 +0200 Subject: [PATCH] cut descgate interfaces to config, registration receiver and registration provider (to make it testable) --- ecal/core/src/ecal_descgate.cpp | 36 +++++-------------- ecal/core/src/ecal_descgate.h | 16 ++++----- ecal/core/src/ecal_globals.cpp | 21 +++++++++-- .../cpp/util_test/src/util_getclients.cpp | 14 +++++++- .../cpp/util_test/src/util_getservices.cpp | 14 +++++++- .../cpp/util_test/src/util_gettopics.cpp | 9 +++-- 6 files changed, 66 insertions(+), 44 deletions(-) diff --git a/ecal/core/src/ecal_descgate.cpp b/ecal/core/src/ecal_descgate.cpp index b4ded553ac..5f6004ea83 100644 --- a/ecal/core/src/ecal_descgate.cpp +++ b/ecal/core/src/ecal_descgate.cpp @@ -21,12 +21,10 @@ * @brief eCAL description gateway class **/ -#include -#include - -#include "ecal_globals.h" #include "ecal_descgate.h" +#include + namespace { eCAL::Util::DescQualityFlags GetDataTypeInfoQuality(const eCAL::SDataTypeInformation& data_type_info_, bool is_producer_) @@ -45,33 +43,15 @@ namespace namespace eCAL { - CDescGate::CDescGate() : - m_publisher_info_map (std::chrono::milliseconds(Config::GetMonitoringTimeoutMs())), - m_subscriber_info_map (std::chrono::milliseconds(Config::GetMonitoringTimeoutMs())), - m_service_info_map (std::chrono::milliseconds(Config::GetMonitoringTimeoutMs())), - m_client_info_map (std::chrono::milliseconds(Config::GetMonitoringTimeoutMs())) + CDescGate::CDescGate(const std::chrono::milliseconds& exp_timeout_) : + m_publisher_info_map (exp_timeout_), + m_subscriber_info_map (exp_timeout_), + m_service_info_map (exp_timeout_), + m_client_info_map (exp_timeout_) { } CDescGate::~CDescGate() = default; - void CDescGate::Create() - { -#if ECAL_CORE_REGISTRATION - // utilize registration provider and receiver to get descriptions - g_registration_provider()->SetCustomApplySampleCallback("descgate", [this](const auto& sample_) {this->ApplySample(sample_, tl_none); }); - g_registration_receiver()->SetCustomApplySampleCallback("descgate", [this](const auto& sample_) {this->ApplySample(sample_, tl_none); }); -#endif - } - - void CDescGate::Destroy() - { -#if ECAL_CORE_REGISTRATION - // stop registration provider and receiver utilization to get descriptions - g_registration_provider()->RemCustomApplySampleCallback("descgate"); - g_registration_receiver()->RemCustomApplySampleCallback("descgate"); -#endif - } - QualityTopicIdMap CDescGate::GetPublisher() { return GetTopics(m_publisher_info_map); @@ -179,7 +159,7 @@ namespace eCAL break; default: { - Logging::Log(log_level_debug1, "CDescGate::ApplySample : unknown sample type"); + std::cerr << "CDescGate::ApplySample : unknown sample type" << '\n'; } break; } diff --git a/ecal/core/src/ecal_descgate.h b/ecal/core/src/ecal_descgate.h index 45c4d022f1..9e663762aa 100644 --- a/ecal/core/src/ecal_descgate.h +++ b/ecal/core/src/ecal_descgate.h @@ -26,13 +26,13 @@ #include #include -#include "serialization/ecal_serialize_sample_registration.h" +#include "serialization/ecal_struct_sample_registration.h" #include "util/ecal_expmap.h" +#include #include #include #include -#include namespace eCAL { @@ -65,21 +65,23 @@ namespace eCAL } }; - using QualityTopicIdMap = std::map; + using QualityTopicIdMap = std::map; using QualityServiceIdMap = std::map; class CDescGate { public: - CDescGate(); + CDescGate(const std::chrono::milliseconds& exp_timeout_); ~CDescGate(); - void Create(); - void Destroy(); + // apply samples to description gate + void ApplySample(const Registration::Sample& sample_, eTLayerType layer_); + // get publisher/subscriber maps QualityTopicIdMap GetPublisher(); QualityTopicIdMap GetSubscriber(); + // get service/clients maps QualityServiceIdMap GetServices(); QualityServiceIdMap GetClients(); @@ -122,8 +124,6 @@ namespace eCAL QualityTopicIdMap GetTopics(const SQualityTopicIdMap& topic_map_); QualityServiceIdMap GetServices(const SQualityServiceIdMap& service_method_info_map_); - void ApplySample(const Registration::Sample& sample_, eTLayerType layer_); - void ApplyTopicDescription(SQualityTopicIdMap& topic_info_map_, const std::string& topic_name_, const std::string& topic_id_, diff --git a/ecal/core/src/ecal_globals.cpp b/ecal/core/src/ecal_globals.cpp index 634fdfc3b2..5270b2e2d3 100644 --- a/ecal/core/src/ecal_globals.cpp +++ b/ecal/core/src/ecal_globals.cpp @@ -105,7 +105,8 @@ namespace eCAL ///////////////////// if (descgate_instance == nullptr) { - descgate_instance = std::make_unique(); + // create description gate with configured expiration timeout + descgate_instance = std::make_unique(std::chrono::milliseconds(Config::GetMonitoringTimeoutMs())); new_initialization = true; } @@ -232,7 +233,14 @@ namespace eCAL if (registration_provider_instance) registration_provider_instance->Create(); if (registration_receiver_instance) registration_receiver_instance->Create(); #endif - if (descgate_instance) descgate_instance->Create(); + if (descgate_instance) + { +#if ECAL_CORE_REGISTRATION + // utilize registration provider and receiver to get descriptions + g_registration_provider()->SetCustomApplySampleCallback("descgate", [](const auto& sample_) {g_descgate()->ApplySample(sample_, tl_none); }); + g_registration_receiver()->SetCustomApplySampleCallback("descgate", [](const auto& sample_) {g_descgate()->ApplySample(sample_, tl_none); }); +#endif + } #if defined(ECAL_CORE_REGISTRATION_SHM) || defined(ECAL_CORE_TRANSPORT_SHM) if (memfile_pool_instance) memfile_pool_instance->Create(); #endif @@ -324,7 +332,14 @@ namespace eCAL #if ECAL_CORE_SUBSCRIBER if (subgate_instance) subgate_instance->Destroy(); #endif - if (descgate_instance) descgate_instance->Destroy(); + if (descgate_instance) + { +#if ECAL_CORE_REGISTRATION + // stop registration provider and receiver utilization to get descriptions + g_registration_provider()->RemCustomApplySampleCallback("descgate"); + g_registration_receiver()->RemCustomApplySampleCallback("descgate"); +#endif + } #if ECAL_CORE_REGISTRATION if (registration_receiver_instance) registration_receiver_instance->Destroy(); if (registration_provider_instance) registration_provider_instance->Destroy(); diff --git a/ecal/tests/cpp/util_test/src/util_getclients.cpp b/ecal/tests/cpp/util_test/src/util_getclients.cpp index b21b5af25f..3a7543d7e6 100644 --- a/ecal/tests/cpp/util_test/src/util_getclients.cpp +++ b/ecal/tests/cpp/util_test/src/util_getclients.cpp @@ -22,7 +22,10 @@ #include -enum { CMN_MONITORING_TIMEOUT = 5000}; +enum { + CMN_MONITORING_TIMEOUT = 5000, + CMN_REGISTRATION_REFRESH = 1000 +}; TEST(core_cpp_util, ClientExpiration) { @@ -67,6 +70,9 @@ TEST(core_cpp_util, ClientExpiration) EXPECT_EQ(client_info_map.size(), 1); } + // let's unregister + eCAL::Process::SleepMS(CMN_REGISTRATION_REFRESH); + // get all clients again, all clients // should be removed from the map eCAL::Util::GetClients(client_info_map); @@ -153,6 +159,9 @@ TEST(core_cpp_util, ClientEqualQualities) EXPECT_EQ(resp_desc, "foo::resp_desc2"); } + // let's unregister + eCAL::Process::SleepMS(CMN_REGISTRATION_REFRESH); + // get all clients again, all clients // should be removed from the map eCAL::Util::GetClients(client_info_map); @@ -218,6 +227,9 @@ TEST(core_cpp_util, ClientDifferentQualities) EXPECT_EQ(client_info_map.size(), 1); } + // let's unregister + eCAL::Process::SleepMS(CMN_REGISTRATION_REFRESH); + // get all clients again, all clients // should be removed from the map eCAL::Util::GetClients(client_info_map); diff --git a/ecal/tests/cpp/util_test/src/util_getservices.cpp b/ecal/tests/cpp/util_test/src/util_getservices.cpp index b5faaf4659..03a325589c 100644 --- a/ecal/tests/cpp/util_test/src/util_getservices.cpp +++ b/ecal/tests/cpp/util_test/src/util_getservices.cpp @@ -22,7 +22,10 @@ #include -enum { CMN_MONITORING_TIMEOUT = 5000 }; +enum { + CMN_MONITORING_TIMEOUT = 5000, + CMN_REGISTRATION_REFRESH = 1000 +}; TEST(core_cpp_util, ServiceExpiration) { @@ -63,6 +66,9 @@ TEST(core_cpp_util, ServiceExpiration) EXPECT_EQ(service_info_map.size(), 1); } + // let's unregister + eCAL::Process::SleepMS(CMN_REGISTRATION_REFRESH); + // get all services again, all services // should be removed from the map eCAL::Util::GetServices(service_info_map); @@ -141,6 +147,9 @@ TEST(core_cpp_util, ServiceEqualQualities) EXPECT_EQ(resp_desc, "foo::resp_desc2"); } + // let's unregister + eCAL::Process::SleepMS(CMN_REGISTRATION_REFRESH); + // get all services again, all services // should be removed from the map eCAL::Util::GetServices(service_info_map); @@ -198,6 +207,9 @@ TEST(core_cpp_util, ServiceDifferentQualities) EXPECT_EQ(service_info_map.size(), 1); } + // let's unregister + eCAL::Process::SleepMS(CMN_REGISTRATION_REFRESH); + // get all services again, all services // should be removed from the map eCAL::Util::GetServices(service_info_map); diff --git a/ecal/tests/cpp/util_test/src/util_gettopics.cpp b/ecal/tests/cpp/util_test/src/util_gettopics.cpp index 363cc72688..7e6af479ad 100644 --- a/ecal/tests/cpp/util_test/src/util_gettopics.cpp +++ b/ecal/tests/cpp/util_test/src/util_gettopics.cpp @@ -25,7 +25,10 @@ #include -enum { CMN_MONITORING_TIMEOUT = 5000 }; +enum { + CMN_MONITORING_TIMEOUT = 5000, + CMN_REGISTRATION_REFRESH = 1000 +}; TEST(core_cpp_util, GetTopics) { @@ -108,8 +111,8 @@ TEST(core_cpp_util, GetTopics) } } - // let's unregister them - eCAL::Process::SleepMS(CMN_MONITORING_TIMEOUT + 2000); + // let's unregister + eCAL::Process::SleepMS(CMN_REGISTRATION_REFRESH); // get all topics again, now all topics // should be removed from the map