diff --git a/src/cpp/StatisticsBackendData.cpp b/src/cpp/StatisticsBackendData.cpp index 1a3ef8cca..caef3cd3f 100644 --- a/src/cpp/StatisticsBackendData.cpp +++ b/src/cpp/StatisticsBackendData.cpp @@ -367,7 +367,7 @@ void StatisticsBackendData::stop_monitor( // The monitor is inactive // NOTE: for test sake, this is not always set - if (database_->is_entity_present(monitor_id)) + if (database_ && database_->is_entity_present(monitor_id)) { database_->change_entity_status(monitor_id, false); } diff --git a/src/cpp/database/database.hpp b/src/cpp/database/database.hpp index 8bf7fc5b8..1467ea656 100644 --- a/src/cpp/database/database.hpp +++ b/src/cpp/database/database.hpp @@ -79,6 +79,13 @@ class Database { public: + /** + * @brief Destructor of Database. + * + * @note It requires a virtual dtor for test sake. + */ + virtual ~Database() = default; + /** * @brief Insert a new entity into the database. * @param entity The entity object to be inserted. diff --git a/test/TrafficInjector/MessageSerializer.h b/test/TrafficInjector/MessageSerializer.h index 08b0e581d..d4d9d380a 100644 --- a/test/TrafficInjector/MessageSerializer.h +++ b/test/TrafficInjector/MessageSerializer.h @@ -65,6 +65,8 @@ class MessageSerializer virtual void deserialize( void* data, const Message& message) = 0; + + virtual ~MessageSerializer() = default; }; class GuidMessageSerializer : public MessageSerializer diff --git a/test/TrafficInjector/TrafficInjector.h b/test/TrafficInjector/TrafficInjector.h index 0abd60de5..8fbb7d057 100644 --- a/test/TrafficInjector/TrafficInjector.h +++ b/test/TrafficInjector/TrafficInjector.h @@ -179,7 +179,7 @@ class TrafficInjector type.register_type(participant_); data_types_[kind] = type; - MessageSerializer* serializer = new WriterReaderDataMessageSerializer(); + std::shared_ptr serializer = std::make_shared(); serializers_[kind] = serializer; // Add the type to all other event kinds that use it @@ -197,7 +197,7 @@ class TrafficInjector type.register_type(participant_); data_types_[kind] = type; - MessageSerializer* serializer = new Locator2LocatorDataMessageSerializer(); + std::shared_ptr serializer = std::make_shared(); serializers_[kind] = serializer; // Add the type to all other event kinds that use it @@ -215,7 +215,7 @@ class TrafficInjector type.register_type(participant_); data_types_[kind] = type; - MessageSerializer* serializer = new EntityDataMessageSerializer(); + std::shared_ptr serializer = std::make_shared(); serializers_[kind] = serializer; // Add the type to all other event kinds that use it @@ -235,7 +235,7 @@ class TrafficInjector type.register_type(participant_); data_types_[kind] = type; - MessageSerializer* serializer = new EntityDataMessageSerializer(); + std::shared_ptr serializer = std::make_shared(); serializers_[kind] = serializer; // Add the type to all other event kinds that use it @@ -255,7 +255,7 @@ class TrafficInjector type.register_type(participant_); data_types_[kind] = type; - MessageSerializer* serializer = new Entity2LocatorTrafficMessageSerializer(); + std::shared_ptr serializer = std::make_shared(); serializers_[kind] = serializer; // Add the type to all other event kinds that use it @@ -275,7 +275,7 @@ class TrafficInjector type.register_type(participant_); data_types_[kind] = type; - MessageSerializer* serializer = new Entity2LocatorTrafficMessageSerializer(); + std::shared_ptr serializer = std::make_shared(); serializers_[kind] = serializer; // Add the type to all other event kinds that use it @@ -295,7 +295,7 @@ class TrafficInjector type.register_type(participant_); data_types_[kind] = type; - MessageSerializer* serializer = new EntityCountMessageSerializer(); + std::shared_ptr serializer = std::make_shared(); serializers_[kind] = serializer; // Add the type to all other event kinds that use it @@ -327,7 +327,7 @@ class TrafficInjector type.register_type(participant_); data_types_[kind] = type; - MessageSerializer* serializer = new EntityCountMessageSerializer(); + std::shared_ptr serializer = std::make_shared(); serializers_[kind] = serializer; // Add the type to all other event kinds that use it @@ -359,7 +359,7 @@ class TrafficInjector type.register_type(participant_); data_types_[kind] = type; - MessageSerializer* serializer = new EntityCountMessageSerializer(); + std::shared_ptr serializer = std::make_shared(); serializers_[kind] = serializer; // Add the type to all other event kinds that use it @@ -391,7 +391,7 @@ class TrafficInjector type.register_type(participant_); data_types_[kind] = type; - MessageSerializer* serializer = new EntityCountMessageSerializer(); + std::shared_ptr serializer = std::make_shared(); serializers_[kind] = serializer; // Add the type to all other event kinds that use it @@ -423,7 +423,7 @@ class TrafficInjector type.register_type(participant_); data_types_[kind] = type; - MessageSerializer* serializer = new EntityCountMessageSerializer(); + std::shared_ptr serializer = std::make_shared(); serializers_[kind] = serializer; // Add the type to all other event kinds that use it @@ -455,7 +455,7 @@ class TrafficInjector type.register_type(participant_); data_types_[kind] = type; - MessageSerializer* serializer = new EntityCountMessageSerializer(); + std::shared_ptr serializer = std::make_shared(); serializers_[kind] = serializer; // Add the type to all other event kinds that use it @@ -487,7 +487,7 @@ class TrafficInjector type.register_type(participant_); data_types_[kind] = type; - MessageSerializer* serializer = new EntityCountMessageSerializer(); + std::shared_ptr serializer = std::make_shared(); serializers_[kind] = serializer; // Add the type to all other event kinds that use it @@ -519,7 +519,7 @@ class TrafficInjector type.register_type(participant_); data_types_[kind] = type; - MessageSerializer* serializer = new EntityCountMessageSerializer(); + std::shared_ptr serializer = std::make_shared(); serializers_[kind] = serializer; // Add the type to all other event kinds that use it @@ -551,7 +551,7 @@ class TrafficInjector type.register_type(participant_); data_types_[kind] = type; - MessageSerializer* serializer = new DiscoveryTimeMessageSerializer(); + std::shared_ptr serializer = std::make_shared(); serializers_[kind] = serializer; // Add the type to all other event kinds that use it @@ -569,7 +569,7 @@ class TrafficInjector type.register_type(participant_); data_types_[kind] = type; - MessageSerializer* serializer = new SampleIdentityCountMessageSerializer(); + std::shared_ptr serializer = std::make_shared(); serializers_[kind] = serializer; // Add the type to all other event kinds that use it @@ -587,7 +587,7 @@ class TrafficInjector type.register_type(participant_); data_types_[kind] = type; - MessageSerializer* serializer = new PhysicalDataMessageSerializer(); + std::shared_ptr serializer = std::make_shared(); serializers_[kind] = serializer; // Add the type to all other event kinds that use it @@ -715,7 +715,7 @@ class TrafficInjector std::map topics_; std::map data_types_; - std::map serializers_; + std::map> serializers_; std::map event_names_; std::map event_kinds_; diff --git a/test/unittest/StatisticsBackend/IsActiveTests.cpp b/test/unittest/StatisticsBackend/IsActiveTests.cpp index 7ba99e1e3..16c1b67a2 100644 --- a/test/unittest/StatisticsBackend/IsActiveTests.cpp +++ b/test/unittest/StatisticsBackend/IsActiveTests.cpp @@ -25,6 +25,8 @@ using namespace eprosima::statistics_backend::subscriber; +bool StatisticsBackendTest::has_database_been_set_ = false; + /** * @brief Fixture for the is_active_tests * - Create a database loading it from a file. @@ -36,7 +38,7 @@ class is_active_tests : public ::testing::Test void SetUp() { - db = new DataBaseTest; + db = new DataBaseTest; // This will be deleted inside StatisticsBackendTest unset_database DatabaseDump dump; load_file(EMPTY_ENTITIES_DUMP_FILE, dump); db->load_database(dump); @@ -106,6 +108,11 @@ class is_active_tests : public ::testing::Test delete entity_queue; delete data_queue; delete participant_listener; + + if (!StatisticsBackendTest::unset_database()) + { + delete db; + } } std::shared_ptr host; @@ -121,13 +128,13 @@ class is_active_tests : public ::testing::Test DataBaseTest* db; // Entity queue, attached to the database - DatabaseEntityQueue* entity_queue; + DatabaseEntityQueue* entity_queue = nullptr; // Data queue, attached to the database - DatabaseDataQueue* data_queue; + DatabaseDataQueue* data_queue = nullptr; // Statistics participant_, that is supposed to receive the callbacks eprosima::fastdds::dds::DomainParticipant statistics_participant; // Listener under tests. Will receive a pointer to statistics_participant - StatisticsParticipantListener* participant_listener; + StatisticsParticipantListener* participant_listener = nullptr; }; // Windows dll do not export ParticipantProxyData class members (private APIs) diff --git a/test/unittest/StatisticsBackend/StatisticsBackendTests.cpp b/test/unittest/StatisticsBackend/StatisticsBackendTests.cpp index b6132168f..f46cc2054 100644 --- a/test/unittest/StatisticsBackend/StatisticsBackendTests.cpp +++ b/test/unittest/StatisticsBackend/StatisticsBackendTests.cpp @@ -34,6 +34,8 @@ using namespace eprosima::statistics_backend; using namespace eprosima::statistics_backend::database; +bool StatisticsBackendTest::has_database_been_set_ = false; + /** * @brief Fixture for the statistics_backend class tests * @@ -56,6 +58,14 @@ class statistics_backend_tests : public ::testing::TestWithParam> entities; }; diff --git a/test/unittest/TestUtils/DatabaseUtils.hpp b/test/unittest/TestUtils/DatabaseUtils.hpp index aa81a50c1..12cdf6263 100644 --- a/test/unittest/TestUtils/DatabaseUtils.hpp +++ b/test/unittest/TestUtils/DatabaseUtils.hpp @@ -634,8 +634,39 @@ class StatisticsBackendTest : public StatisticsBackend monitor->domain_listener = nullptr; details::StatisticsBackendData::get_instance()->monitors_by_entity_[domain->id] = monitor; } + has_database_been_set_ = true; } + /** + * @brief This method unset the internal database + * + * This method is required because the database is manually and hard set from the test, which makes + * ASAN cry. + * This will remove this instance in case it has been set, and do nothing otherwise. + * + * Be aware that in some cases where the database has not been set, it may be removed manually from + * the test. (This is why you should not create a smart ptr from a ptr initialized somewhere else.) + * + * @return true if the database has been reset + * @return false otherwise + */ + static bool unset_database() + { + if (has_database_been_set_) + { + details::StatisticsBackendData::get_instance()->database_.reset(); + return true; + } + else + { + return false; + } + } + +protected: + + static bool has_database_been_set_; + }; /**