Skip to content

Commit

Permalink
fix topic stats test, wait for more messages, only check the ones wit…
Browse files Browse the repository at this point in the history
…h samples (#1274)

Signed-off-by: Dirk Thomas <dirk-thomas@users.noreply.github.com>
  • Loading branch information
dirk-thomas authored and brawner committed Oct 19, 2020
1 parent b17c739 commit 8be1e76
Showing 1 changed file with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ TEST_F(TestSubscriptionTopicStatisticsFixture, test_receive_stats_for_message_no
auto statistics_listener = std::make_shared<rclcpp::topic_statistics::MetricsMessageSubscriber>(
"test_receive_single_empty_stats_message_listener",
"/statistics",
2);
4);

auto empty_subscriber = std::make_shared<EmptySubscriber>(
kTestSubNodeName,
Expand All @@ -301,12 +301,12 @@ TEST_F(TestSubscriptionTopicStatisticsFixture, test_receive_stats_for_message_no
kTestDuration);

// Compare message counts, sample count should be the same as published and received count
EXPECT_EQ(2, statistics_listener->GetNumberOfMessagesReceived());
EXPECT_EQ(4, statistics_listener->GetNumberOfMessagesReceived());

// Check the received message and the data types
const auto received_messages = statistics_listener->GetReceivedMessages();

EXPECT_EQ(2u, received_messages.size());
EXPECT_EQ(4u, received_messages.size());

std::set<std::string> received_metrics;
for (const auto & msg : received_messages) {
Expand All @@ -318,10 +318,27 @@ TEST_F(TestSubscriptionTopicStatisticsFixture, test_receive_stats_for_message_no
// Check the collected statistics for message period.
// Message age statistics will not be calculated because Empty messages
// don't have a `header` with timestamp.
bool any_samples = false;
for (const auto & msg : received_messages) {
if (msg.metrics_source != "message_period") {
continue;
}
// skip messages without samples
bool has_samples = false;
for (const auto & stats_point : msg.statistics) {
const auto type = stats_point.data_type;
if (
StatisticDataType::STATISTICS_DATA_TYPE_SAMPLE_COUNT == type &&
stats_point.data > 0)
{
has_samples = true;
break;
}
}
if (!has_samples) {
continue;
}
any_samples = true;
for (const auto & stats_point : msg.statistics) {
const auto type = stats_point.data_type;
switch (type) {
Expand All @@ -346,6 +363,7 @@ TEST_F(TestSubscriptionTopicStatisticsFixture, test_receive_stats_for_message_no
}
}
}
EXPECT_TRUE(any_samples) << "All received metrics messages had zero samples";
}

TEST_F(TestSubscriptionTopicStatisticsFixture, test_receive_stats_for_message_with_header)
Expand All @@ -361,7 +379,7 @@ TEST_F(TestSubscriptionTopicStatisticsFixture, test_receive_stats_for_message_wi
auto statistics_listener = std::make_shared<rclcpp::topic_statistics::MetricsMessageSubscriber>(
"test_receive_stats_for_message_with_header",
"/statistics",
2);
4);

auto msg_with_header_subscriber = std::make_shared<MessageWithHeaderSubscriber>(
kTestSubNodeName,
Expand All @@ -378,12 +396,12 @@ TEST_F(TestSubscriptionTopicStatisticsFixture, test_receive_stats_for_message_wi
kTestDuration);

// Compare message counts, sample count should be the same as published and received count
EXPECT_EQ(2, statistics_listener->GetNumberOfMessagesReceived());
EXPECT_EQ(4, statistics_listener->GetNumberOfMessagesReceived());

// Check the received message and the data types
const auto received_messages = statistics_listener->GetReceivedMessages();

EXPECT_EQ(2u, received_messages.size());
EXPECT_EQ(4u, received_messages.size());

std::set<std::string> received_metrics;
for (const auto & msg : received_messages) {
Expand All @@ -393,7 +411,24 @@ TEST_F(TestSubscriptionTopicStatisticsFixture, test_receive_stats_for_message_wi
EXPECT_TRUE(received_metrics.find("message_period") != received_metrics.end());

// Check the collected statistics for message period.
bool any_samples = false;
for (const auto & msg : received_messages) {
// skip messages without samples
bool has_samples = false;
for (const auto & stats_point : msg.statistics) {
const auto type = stats_point.data_type;
if (
StatisticDataType::STATISTICS_DATA_TYPE_SAMPLE_COUNT == type &&
stats_point.data > 0)
{
has_samples = true;
break;
}
}
if (!has_samples) {
continue;
}
any_samples = true;
for (const auto & stats_point : msg.statistics) {
const auto type = stats_point.data_type;
switch (type) {
Expand All @@ -418,4 +453,5 @@ TEST_F(TestSubscriptionTopicStatisticsFixture, test_receive_stats_for_message_wi
}
}
}
EXPECT_TRUE(any_samples) << "All received metrics messages had zero samples";
}

0 comments on commit 8be1e76

Please sign in to comment.