From 9a7010718b176c1d4b926fee4efd12906765c5e9 Mon Sep 17 00:00:00 2001 From: Stephen Brawner Date: Tue, 28 Jul 2020 16:52:11 -0700 Subject: [PATCH 1/3] Add fault injection macro unit tests Signed-off-by: Stephen Brawner --- rmw_dds_common/test/test_graph_cache.cpp | 62 ++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/rmw_dds_common/test/test_graph_cache.cpp b/rmw_dds_common/test/test_graph_cache.cpp index 9d388b4..d0a7de0 100644 --- a/rmw_dds_common/test/test_graph_cache.cpp +++ b/rmw_dds_common/test/test_graph_cache.cpp @@ -23,6 +23,7 @@ #include "osrf_testing_tools_cpp/scope_exit.hpp" +#include "rcutils/testing/fault_injection.h" #include "rmw/qos_profiles.h" #include "rmw/topic_endpoint_info.h" #include "rmw/topic_endpoint_info_array.h" @@ -1550,3 +1551,64 @@ TEST(test_graph_cache, bad_arguments) rcutils_reset_error(); } } + +TEST(test_graph_cache, get_writers_info_by_topic_maybe_fail) +{ + RCUTILS_FAULT_INJECTION_TEST( + { + GraphCache graph_cache; + add_participants(graph_cache, {"participant1"}); + add_entities( + graph_cache, + { + // topic1 + {"reader1", "participant1", "topic1", "Str", true}, + {"writer1", "participant1", "topic1", "Str", false}, + }); + add_nodes( + graph_cache, { + {"participant1", "ns1", "node1"}, + {"participant1", "ns1", "node2"}, + {"participant1", "ns2", "node1"}}); + + rmw_topic_endpoint_info_array_t info = rmw_get_zero_initialized_topic_endpoint_info_array(); + + rcutils_allocator_t allocator = rcutils_get_default_allocator(); + rmw_ret_t ret = graph_cache.get_writers_info_by_topic( + "topic1", + identity_demangle, + &allocator, + &info); + if (RMW_RET_OK == ret) { + ret = rmw_topic_endpoint_info_array_fini(&info, &allocator); + } else { + rcutils_reset_error(); + } + + rcutils_string_array_t names = rcutils_get_zero_initialized_string_array(); + rcutils_string_array_t namespaces = rcutils_get_zero_initialized_string_array(); + rcutils_string_array_t enclaves = rcutils_get_zero_initialized_string_array(); + ret = graph_cache.get_node_names(&names, &namespaces, &enclaves, &allocator); + if (RMW_RET_OK == ret) { + ret = rcutils_string_array_fini(&names); + ret = rcutils_string_array_fini(&namespaces); + ret = rcutils_string_array_fini(&enclaves); + } else { + rcutils_reset_error(); + } + + DemangleFunctionT demangle_topic = identity_demangle; + DemangleFunctionT demangle_type = identity_demangle; + rmw_names_and_types_t names_and_types = rmw_get_zero_initialized_names_and_types(); + ret = graph_cache.get_names_and_types( + demangle_topic, + demangle_type, + &allocator, + &names_and_types); + if (RMW_RET_OK == ret) { + ret = rmw_names_and_types_fini(&names_and_types); + } else { + rcutils_reset_error(); + } + }); +} From 5d490c1d882462b7e3829ac71991ae6753499fb7 Mon Sep 17 00:00:00 2001 From: Stephen Brawner Date: Fri, 7 Aug 2020 15:38:09 -0700 Subject: [PATCH 2/3] target definitions Signed-off-by: Stephen Brawner --- rmw_dds_common/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/rmw_dds_common/CMakeLists.txt b/rmw_dds_common/CMakeLists.txt index 5f64096..5c33072 100644 --- a/rmw_dds_common/CMakeLists.txt +++ b/rmw_dds_common/CMakeLists.txt @@ -78,6 +78,7 @@ if(BUILD_TESTING) ament_add_gmock(test_graph_cache test/test_graph_cache.cpp) if(TARGET test_graph_cache) target_link_libraries(test_graph_cache ${PROJECT_NAME}_library osrf_testing_tools_cpp::memory_tools) + target_compile_definitions(test_graph_cache PUBLIC RCUTILS_ENABLE_FAULT_INJECTION) endif() ament_add_gmock(test_gid_utils test/test_gid_utils.cpp) From 71433979eb9c9325d0ee0fab25e03df949887e74 Mon Sep 17 00:00:00 2001 From: Stephen Brawner Date: Tue, 25 Aug 2020 14:17:15 -0700 Subject: [PATCH 3/3] PR Fixup Signed-off-by: Stephen Brawner --- rmw_dds_common/test/test_graph_cache.cpp | 63 +++++++++++++++++++----- 1 file changed, 50 insertions(+), 13 deletions(-) diff --git a/rmw_dds_common/test/test_graph_cache.cpp b/rmw_dds_common/test/test_graph_cache.cpp index d0a7de0..c856d0d 100644 --- a/rmw_dds_common/test/test_graph_cache.cpp +++ b/rmw_dds_common/test/test_graph_cache.cpp @@ -1552,61 +1552,98 @@ TEST(test_graph_cache, bad_arguments) } } -TEST(test_graph_cache, get_writers_info_by_topic_maybe_fail) +class TestGraphCache : public ::testing::Test { - RCUTILS_FAULT_INJECTION_TEST( +public: + void SetUp() { - GraphCache graph_cache; - add_participants(graph_cache, {"participant1"}); + add_participants(graph_cache_, {"participant1"}); add_entities( - graph_cache, + graph_cache_, { // topic1 {"reader1", "participant1", "topic1", "Str", true}, {"writer1", "participant1", "topic1", "Str", false}, }); add_nodes( - graph_cache, { + graph_cache_, { {"participant1", "ns1", "node1"}, {"participant1", "ns1", "node2"}, {"participant1", "ns2", "node1"}}); + } - rmw_topic_endpoint_info_array_t info = rmw_get_zero_initialized_topic_endpoint_info_array(); +protected: + GraphCache graph_cache_; +}; +TEST_F(TestGraphCache, get_writers_info_by_topic_maybe_fail) +{ + RCUTILS_FAULT_INJECTION_TEST( + { + rmw_topic_endpoint_info_array_t info = rmw_get_zero_initialized_topic_endpoint_info_array(); rcutils_allocator_t allocator = rcutils_get_default_allocator(); - rmw_ret_t ret = graph_cache.get_writers_info_by_topic( + + rmw_ret_t ret = graph_cache_.get_writers_info_by_topic( "topic1", identity_demangle, &allocator, &info); if (RMW_RET_OK == ret) { ret = rmw_topic_endpoint_info_array_fini(&info, &allocator); + if (RMW_RET_OK != ret) { + // If fault injection causes fini to fail, it should work the second time. + EXPECT_EQ(RMW_RET_OK, rmw_topic_endpoint_info_array_fini(&info, &allocator)); + } } else { rcutils_reset_error(); } + }); +} +TEST_F(TestGraphCache, get_node_names_maybe_fail) +{ + RCUTILS_FAULT_INJECTION_TEST( + { rcutils_string_array_t names = rcutils_get_zero_initialized_string_array(); rcutils_string_array_t namespaces = rcutils_get_zero_initialized_string_array(); rcutils_string_array_t enclaves = rcutils_get_zero_initialized_string_array(); - ret = graph_cache.get_node_names(&names, &namespaces, &enclaves, &allocator); + rcutils_allocator_t allocator = rcutils_get_default_allocator(); + rmw_ret_t ret = graph_cache_.get_node_names(&names, &namespaces, &enclaves, &allocator); if (RMW_RET_OK == ret) { - ret = rcutils_string_array_fini(&names); - ret = rcutils_string_array_fini(&namespaces); - ret = rcutils_string_array_fini(&enclaves); + // rcutils_string_array_fini is not under test, so disable fault injection test here. + int64_t fault_injection_count = rcutils_fault_injection_get_count(); + rcutils_fault_injection_set_count(RCUTILS_FAULT_INJECTION_NEVER_FAIL); + + EXPECT_EQ(RCUTILS_RET_OK, rcutils_string_array_fini(&names)); + EXPECT_EQ(RCUTILS_RET_OK, rcutils_string_array_fini(&namespaces)); + EXPECT_EQ(RCUTILS_RET_OK, rcutils_string_array_fini(&enclaves)); + + rcutils_fault_injection_set_count(fault_injection_count); } else { rcutils_reset_error(); } + }); +} +TEST_F(TestGraphCache, get_names_and_types_maybe_fail) +{ + RCUTILS_FAULT_INJECTION_TEST( + { DemangleFunctionT demangle_topic = identity_demangle; DemangleFunctionT demangle_type = identity_demangle; + rcutils_allocator_t allocator = rcutils_get_default_allocator(); rmw_names_and_types_t names_and_types = rmw_get_zero_initialized_names_and_types(); - ret = graph_cache.get_names_and_types( + rmw_ret_t ret = graph_cache_.get_names_and_types( demangle_topic, demangle_type, &allocator, &names_and_types); if (RMW_RET_OK == ret) { ret = rmw_names_and_types_fini(&names_and_types); + if (RMW_RET_OK != ret) { + // If fault injection causes fini to fail, it should work the second time. + EXPECT_EQ(RMW_RET_OK, rmw_names_and_types_fini(&names_and_types)); + } } else { rcutils_reset_error(); }