Skip to content

Commit

Permalink
Check if message has a "header" field with a stamp subfield of type b…
Browse files Browse the repository at this point in the history
…uiltin_interfaces::msg::Time (ros-tooling#54)

* Fix for Issue 51. Adds check to HasHeader template to check that the message
in question has a field called 'header' with a subfield 'stamp' of type builtin_interfaces::msg::Time.
Added unit test case and DummyCustomHeaderMessage for use with tests.

Signed-off-by: scott.mende@sslmda.com <scott.mende@sslmda.com>
Signed-off-by: Devin Bonnie <dbbonnie@amazon.com>
Signed-off-by: Emerson Knapp <emerson.b.knapp@gmail.com>
Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
Signed-off-by: Tim Clephas <tim.clephas@nobleo.nl>
  • Loading branch information
scottmendessl authored and Timple committed Jan 11, 2023
1 parent 15ec42e commit 0271aeb
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
10 changes: 7 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ endif()
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(ament_cmake_ros REQUIRED)
find_package(builtin_interfaces REQUIRED)
find_package(rcl REQUIRED)
find_package(rcpputils REQUIRED)
find_package(rosidl_default_generators REQUIRED)
find_package(statistics_msgs REQUIRED)
find_package(std_msgs REQUIRED)

add_library(${PROJECT_NAME}
src/libstatistics_collector/collector/collector.cpp
Expand All @@ -48,9 +48,11 @@ target_include_directories(${PROJECT_NAME} PUBLIC
"$<INSTALL_INTERFACE:include/${PROJECT_NAME}>")

ament_target_dependencies(${PROJECT_NAME}
"builtin_interfaces"
"rcl"
"rcpputils"
"statistics_msgs")
"statistics_msgs"
)

install(
TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}
Expand All @@ -66,12 +68,13 @@ ament_export_libraries(${PROJECT_NAME})
# Export modern CMake targets
ament_export_targets(${PROJECT_NAME})

ament_export_dependencies("rcl" "rcpputils" "rosidl_default_runtime" "statistics_msgs")
ament_export_dependencies("builtin_interfaces" "rcl" "rcpputils" "rosidl_default_runtime" "statistics_msgs")

if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
find_package(ament_cmake_gtest REQUIRED)
find_package(performance_test_fixture REQUIRED)
find_package(std_msgs REQUIRED)

ament_lint_auto_find_test_dependencies()

Expand All @@ -97,6 +100,7 @@ if(BUILD_TESTING)

rosidl_generate_interfaces(libstatistics_collector_test_msgs
"test/msg/DummyMessage.msg"
"test/msg/DummyCustomHeaderMessage.msg"
DEPENDENCIES "std_msgs"
SKIP_INSTALL)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "libstatistics_collector/topic_statistics_collector/topic_statistics_collector.hpp"

#include "builtin_interfaces/msg/time.hpp"
#include "rcl/time.h"
#include "rcutils/logging_macros.h"

Expand All @@ -41,11 +42,13 @@ template<typename M, typename = void>
struct HasHeader : public std::false_type {};

/**
* True if the message has a header
* True if the message has a field named 'header' with a subfield named 'stamp' of
* type builtin_interfaces::msg::Time
* @tparam M
*/
template<typename M>
struct HasHeader<M, decltype((void) M::header)>: std::true_type {};
struct HasHeader<M, typename std::enable_if<std::is_same<builtin_interfaces::msg::Time,
decltype(M().header.stamp)>::value>::type>: public std::true_type {};

/**
* Return a boolean flag indicating the timestamp is not set
Expand Down
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<build_depend>rosidl_default_generators</build_depend>
<build_depend>std_msgs</build_depend>

<depend>builtin_interfaces</depend>
<depend>rcl</depend>
<depend>rcpputils</depend>
<depend>statistics_msgs</depend>
Expand Down
4 changes: 4 additions & 0 deletions test/msg/DummyCustomHeaderMessage.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This is a dummy message type with a custom header field that is not type `Header`.
# It is intended for use in topic statistics tests.

std_msgs/ColorRGBA header
14 changes: 14 additions & 0 deletions test/topic_statistics_collector/test_received_message_age.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <string>

#include "libstatistics_collector/msg/dummy_message.hpp"
#include "libstatistics_collector/msg/dummy_custom_header_message.hpp"
#include "libstatistics_collector/topic_statistics_collector/constants.hpp"
#include "libstatistics_collector/topic_statistics_collector/received_message_age.hpp"

Expand All @@ -30,6 +31,9 @@ using ReceivedDummyMessageAgeCollector = libstatistics_collector::
topic_statistics_collector::ReceivedMessageAgeCollector<DummyMessage>;
using ReceivedIntMessageAgeCollector = libstatistics_collector::
topic_statistics_collector::ReceivedMessageAgeCollector<int>;
using DummyCustomHeaderMessage = libstatistics_collector::msg::DummyCustomHeaderMessage;
using ReceivedDummyCustomHeaderMessageAgeCollector = libstatistics_collector::
topic_statistics_collector::ReceivedMessageAgeCollector<DummyCustomHeaderMessage>;

constexpr const std::chrono::seconds kDefaultDurationSeconds{1};
constexpr const double kExpectedAverageMilliseconds{2000.0};
Expand Down Expand Up @@ -62,6 +66,16 @@ TEST(ReceivedMessageAgeTest, TestOnlyMessagesWithHeaderGetSampled) {
stats = dummy_msg_collector.GetStatisticsResults();
EXPECT_EQ(i + 1, stats.sample_count) << "Expect " << i + 1 << " samples to be collected";
}

ReceivedDummyCustomHeaderMessageAgeCollector dummy_custom_header_msg_collector{};
auto msg_custom_header = DummyCustomHeaderMessage{};
for (int i = 0; i < kDefaultTimesToTest; ++i) {
dummy_custom_header_msg_collector.OnMessageReceived(
msg_custom_header,
kDefaultTimeMessageReceived);
stats = dummy_custom_header_msg_collector.GetStatisticsResults();
EXPECT_EQ(0, stats.sample_count) << "Expect 0 samples to be collected";
}
}

TEST(ReceivedMessageAgeTest, TestMeasurementOnlyMadeForInitializedHeaderValue) {
Expand Down

0 comments on commit 0271aeb

Please sign in to comment.