Skip to content

Commit 14e666d

Browse files
authored
Add fault injection macro unit tests (#27)
* Add fault injection macro unit tests Signed-off-by: Stephen Brawner <brawner@gmail.com> * target definitions Signed-off-by: Stephen Brawner <brawner@gmail.com> * PR Fixup Signed-off-by: Stephen Brawner <brawner@gmail.com>
1 parent 0c7fe8f commit 14e666d

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

rmw_dds_common/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ if(BUILD_TESTING)
7878
ament_add_gmock(test_graph_cache test/test_graph_cache.cpp)
7979
if(TARGET test_graph_cache)
8080
target_link_libraries(test_graph_cache ${PROJECT_NAME}_library osrf_testing_tools_cpp::memory_tools)
81+
target_compile_definitions(test_graph_cache PUBLIC RCUTILS_ENABLE_FAULT_INJECTION)
8182
endif()
8283

8384
ament_add_gmock(test_gid_utils test/test_gid_utils.cpp)

rmw_dds_common/test/test_graph_cache.cpp

+99
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "osrf_testing_tools_cpp/scope_exit.hpp"
2525

26+
#include "rcutils/testing/fault_injection.h"
2627
#include "rmw/qos_profiles.h"
2728
#include "rmw/topic_endpoint_info.h"
2829
#include "rmw/topic_endpoint_info_array.h"
@@ -1550,3 +1551,101 @@ TEST(test_graph_cache, bad_arguments)
15501551
rcutils_reset_error();
15511552
}
15521553
}
1554+
1555+
class TestGraphCache : public ::testing::Test
1556+
{
1557+
public:
1558+
void SetUp()
1559+
{
1560+
add_participants(graph_cache_, {"participant1"});
1561+
add_entities(
1562+
graph_cache_,
1563+
{
1564+
// topic1
1565+
{"reader1", "participant1", "topic1", "Str", true},
1566+
{"writer1", "participant1", "topic1", "Str", false},
1567+
});
1568+
add_nodes(
1569+
graph_cache_, {
1570+
{"participant1", "ns1", "node1"},
1571+
{"participant1", "ns1", "node2"},
1572+
{"participant1", "ns2", "node1"}});
1573+
}
1574+
1575+
protected:
1576+
GraphCache graph_cache_;
1577+
};
1578+
1579+
TEST_F(TestGraphCache, get_writers_info_by_topic_maybe_fail)
1580+
{
1581+
RCUTILS_FAULT_INJECTION_TEST(
1582+
{
1583+
rmw_topic_endpoint_info_array_t info = rmw_get_zero_initialized_topic_endpoint_info_array();
1584+
rcutils_allocator_t allocator = rcutils_get_default_allocator();
1585+
1586+
rmw_ret_t ret = graph_cache_.get_writers_info_by_topic(
1587+
"topic1",
1588+
identity_demangle,
1589+
&allocator,
1590+
&info);
1591+
if (RMW_RET_OK == ret) {
1592+
ret = rmw_topic_endpoint_info_array_fini(&info, &allocator);
1593+
if (RMW_RET_OK != ret) {
1594+
// If fault injection causes fini to fail, it should work the second time.
1595+
EXPECT_EQ(RMW_RET_OK, rmw_topic_endpoint_info_array_fini(&info, &allocator));
1596+
}
1597+
} else {
1598+
rcutils_reset_error();
1599+
}
1600+
});
1601+
}
1602+
1603+
TEST_F(TestGraphCache, get_node_names_maybe_fail)
1604+
{
1605+
RCUTILS_FAULT_INJECTION_TEST(
1606+
{
1607+
rcutils_string_array_t names = rcutils_get_zero_initialized_string_array();
1608+
rcutils_string_array_t namespaces = rcutils_get_zero_initialized_string_array();
1609+
rcutils_string_array_t enclaves = rcutils_get_zero_initialized_string_array();
1610+
rcutils_allocator_t allocator = rcutils_get_default_allocator();
1611+
rmw_ret_t ret = graph_cache_.get_node_names(&names, &namespaces, &enclaves, &allocator);
1612+
if (RMW_RET_OK == ret) {
1613+
// rcutils_string_array_fini is not under test, so disable fault injection test here.
1614+
int64_t fault_injection_count = rcutils_fault_injection_get_count();
1615+
rcutils_fault_injection_set_count(RCUTILS_FAULT_INJECTION_NEVER_FAIL);
1616+
1617+
EXPECT_EQ(RCUTILS_RET_OK, rcutils_string_array_fini(&names));
1618+
EXPECT_EQ(RCUTILS_RET_OK, rcutils_string_array_fini(&namespaces));
1619+
EXPECT_EQ(RCUTILS_RET_OK, rcutils_string_array_fini(&enclaves));
1620+
1621+
rcutils_fault_injection_set_count(fault_injection_count);
1622+
} else {
1623+
rcutils_reset_error();
1624+
}
1625+
});
1626+
}
1627+
1628+
TEST_F(TestGraphCache, get_names_and_types_maybe_fail)
1629+
{
1630+
RCUTILS_FAULT_INJECTION_TEST(
1631+
{
1632+
DemangleFunctionT demangle_topic = identity_demangle;
1633+
DemangleFunctionT demangle_type = identity_demangle;
1634+
rcutils_allocator_t allocator = rcutils_get_default_allocator();
1635+
rmw_names_and_types_t names_and_types = rmw_get_zero_initialized_names_and_types();
1636+
rmw_ret_t ret = graph_cache_.get_names_and_types(
1637+
demangle_topic,
1638+
demangle_type,
1639+
&allocator,
1640+
&names_and_types);
1641+
if (RMW_RET_OK == ret) {
1642+
ret = rmw_names_and_types_fini(&names_and_types);
1643+
if (RMW_RET_OK != ret) {
1644+
// If fault injection causes fini to fail, it should work the second time.
1645+
EXPECT_EQ(RMW_RET_OK, rmw_names_and_types_fini(&names_and_types));
1646+
}
1647+
} else {
1648+
rcutils_reset_error();
1649+
}
1650+
});
1651+
}

0 commit comments

Comments
 (0)