Skip to content

Commit 0d62335

Browse files
authored
Properly delete builtin statistics writers upon delete_contained_entities() (#4891)
* Refs #20816: Add BB test Signed-off-by: Mario Dominguez <[email protected]> * Refs #20816: Fix Signed-off-by: Mario Dominguez <[email protected]> * Refs #20816: Apply Edu's suggestion Signed-off-by: Mario Dominguez <[email protected]> --------- Signed-off-by: Mario Dominguez <[email protected]>
1 parent a345ea1 commit 0d62335

File tree

3 files changed

+98
-9
lines changed

3 files changed

+98
-9
lines changed

src/cpp/statistics/fastdds/domain/DomainParticipantImpl.cpp

+2-9
Original file line numberDiff line numberDiff line change
@@ -287,15 +287,8 @@ void DomainParticipantImpl::disable()
287287

288288
ReturnCode_t DomainParticipantImpl::delete_contained_entities()
289289
{
290-
ReturnCode_t ret = efd::DomainParticipantImpl::delete_contained_entities();
291-
292-
if (ret == efd::RETCODE_OK)
293-
{
294-
builtin_publisher_impl_ = nullptr;
295-
builtin_publisher_ = nullptr;
296-
}
297-
298-
return ret;
290+
delete_statistics_builtin_entities();
291+
return efd::DomainParticipantImpl::delete_contained_entities();
299292
}
300293

301294
ReturnCode_t DomainParticipantImpl::enable_monitor_service()

src/cpp/statistics/fastdds/domain/DomainParticipantImpl.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ class DomainParticipantImpl : public efd::DomainParticipantImpl,
119119
* @brief This override calls the parent method and returns builtin publishers to nullptr
120120
*
121121
* @return RETCODE_OK if successful
122+
* @note This method is meant to be used followed by a deletion of the participant as it implies
123+
* the deletion of the builtin statistics publishers.
122124
*/
123125
efd::ReturnCode_t delete_contained_entities() override;
124126

test/blackbox/common/DDSBlackboxTestsStatistics.cpp

+94
Original file line numberDiff line numberDiff line change
@@ -684,3 +684,97 @@ TEST(DDSStatistics, discovery_topic_physical_data_delete_physical_properties)
684684
test_discovery_topic_physical_data(DiscoveryTopicPhysicalDataTest::NO_PHYSICAL_DATA);
685685
#endif // FASTDDS_STATISTICS
686686
}
687+
688+
class CustomStatisticsParticipantSubscriber : public PubSubReader<HelloWorldPubSubType>
689+
{
690+
public:
691+
692+
CustomStatisticsParticipantSubscriber(
693+
const std::string& topic_name)
694+
: PubSubReader<HelloWorldPubSubType>(topic_name)
695+
{
696+
}
697+
698+
void destroy() override
699+
{
700+
participant_->delete_contained_entities();
701+
DomainParticipantFactory::get_instance()->delete_participant(participant_);
702+
participant_ = nullptr;
703+
}
704+
705+
};
706+
707+
// Regression test for #20816. When an application is terminated with delete_contained_entities()
708+
// it has to properly finish. The test creates a number of participants with some of them sharing the same topic.
709+
// Each participant asynchronously sends and receive a number of samples. In the readers, when a minumm number of samples
710+
// is received the destroy() method is called (abruptly). The test checks that the application finishes successfully
711+
TEST(DDSStatistics, correct_deletion_upon_delete_contained_entities)
712+
{
713+
#ifdef FASTDDS_STATISTICS
714+
715+
//! Set environment variable and create participant using Qos set by code
716+
const char* value = "HISTORY_LATENCY_TOPIC;NETWORK_LATENCY_TOPIC;"
717+
"PUBLICATION_THROUGHPUT_TOPIC;SUBSCRIPTION_THROUGHPUT_TOPIC;RTPS_SENT_TOPIC;"
718+
"RTPS_LOST_TOPIC;HEARTBEAT_COUNT_TOPIC;ACKNACK_COUNT_TOPIC;NACKFRAG_COUNT_TOPIC;"
719+
"GAP_COUNT_TOPIC;DATA_COUNT_TOPIC;RESENT_DATAS_TOPIC;SAMPLE_DATAS_TOPIC;"
720+
"PDP_PACKETS_TOPIC;EDP_PACKETS_TOPIC;DISCOVERY_TOPIC;PHYSICAL_DATA_TOPIC;";
721+
722+
#ifdef _WIN32
723+
ASSERT_EQ(0, _putenv_s("FASTDDS_STATISTICS", value));
724+
#else
725+
ASSERT_EQ(0, setenv("FASTDDS_STATISTICS", value, 1));
726+
#endif // ifdef _WIN32
727+
728+
size_t n_participants = 5;
729+
size_t n_participants_same_topic = 2;
730+
731+
std::vector<std::shared_ptr<PubSubWriter<HelloWorldPubSubType>>> writers;
732+
std::vector<std::shared_ptr<CustomStatisticsParticipantSubscriber>> readers;
733+
734+
readers.reserve(n_participants);
735+
writers.reserve(n_participants);
736+
737+
std::vector<std::shared_ptr<std::thread>> threads;
738+
threads.reserve(2 * n_participants);
739+
740+
for (size_t i = 0; i < n_participants; ++i)
741+
{
742+
size_t topic_number = (i < n_participants_same_topic) ? 0 : i;
743+
744+
auto writer = std::make_shared<PubSubWriter<HelloWorldPubSubType>>(TEST_TOPIC_NAME + std::to_string(
745+
topic_number));
746+
auto reader =
747+
std::make_shared<CustomStatisticsParticipantSubscriber>(TEST_TOPIC_NAME + std::to_string(topic_number));
748+
749+
std::shared_ptr<std::list<HelloWorld>> data = std::make_shared<std::list<HelloWorld>>(default_helloworld_data_generator(
750+
10));
751+
752+
threads.emplace_back(std::make_shared<std::thread>([reader, data]()
753+
{
754+
reader->init();
755+
ASSERT_TRUE(reader->isInitialized());
756+
reader->startReception(data->size());
757+
reader->block_for_at_least(3);
758+
reader->destroy();
759+
}));
760+
761+
threads.emplace_back(std::make_shared<std::thread>([writer, data]()
762+
{
763+
writer->init();
764+
ASSERT_TRUE(writer->isInitialized());
765+
writer->wait_discovery();
766+
writer->send(*data, 10);
767+
writer->destroy();
768+
}));
769+
770+
writers.push_back(writer);
771+
readers.push_back(reader);
772+
}
773+
774+
for (auto& thread : threads)
775+
{
776+
thread->join();
777+
}
778+
779+
#endif // FASTDDS_STATISTICS
780+
}

0 commit comments

Comments
 (0)