-
Notifications
You must be signed in to change notification settings - Fork 27
Add fault injection macro unit tests #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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,101 @@ TEST(test_graph_cache, bad_arguments) | |||||
| rcutils_reset_error(); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| class TestGraphCache : public ::testing::Test | ||||||
| { | ||||||
| public: | ||||||
| void SetUp() | ||||||
| { | ||||||
| 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"}}); | ||||||
| } | ||||||
|
|
||||||
| 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( | ||||||
| "topic1", | ||||||
| identity_demangle, | ||||||
| &allocator, | ||||||
| &info); | ||||||
| if (RMW_RET_OK == ret) { | ||||||
| ret = rmw_topic_endpoint_info_array_fini(&info, &allocator); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @brawner should this be:
Suggested change
instead? Also, what if fault injection occurs here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, it will fail during a fault injection test because there will be at least one hook in there. I added a check on the return of fini just in case it needs be done a second time. |
||||||
| 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(); | ||||||
| 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) { | ||||||
| // rcutils_string_array_fini is not under test, so disable fault injection test here. | ||||||
| int64_t fault_injection_count = rcutils_fault_injection_get_count(); | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hidmic This is the only location here that would make sense to disable fault injection testing because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's fine as-is for the time being. If it starts showing up more often, we can add a macro. |
||||||
| 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(); | ||||||
| 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(); | ||||||
| } | ||||||
| }); | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brawner perhaps not an issue, but should the test fixture be setup outside the fault injection loop?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yah, created a test fixture class.