|
19 | 19 | #include <gtest/gtest.h> |
20 | 20 |
|
21 | 21 | #include "rcl_lifecycle/rcl_lifecycle.h" |
| 22 | + |
22 | 23 | #include "osrf_testing_tools_cpp/memory_tools/memory_tools.hpp" |
23 | 24 | #include "osrf_testing_tools_cpp/scope_exit.hpp" |
24 | 25 | #include "rcl/error_handling.h" |
| 26 | +#include "rcutils/testing/fault_injection.h" |
| 27 | + |
25 | 28 | #include "lifecycle_msgs/msg/transition_event.h" |
26 | 29 | #include "lifecycle_msgs/srv/change_state.h" |
27 | 30 | #include "lifecycle_msgs/srv/get_available_states.h" |
@@ -337,7 +340,6 @@ TEST(TestRclLifecycle, state_machine) { |
337 | 340 | ret = rcl_lifecycle_state_machine_fini(&state_machine, nullptr, &allocator); |
338 | 341 | EXPECT_EQ(ret, RCL_RET_ERROR); |
339 | 342 | rcutils_reset_error(); |
340 | | - std::cout << "state_machine: " << __LINE__ << std::endl; |
341 | 343 | } |
342 | 344 |
|
343 | 345 | TEST(TestRclLifecycle, state_transitions) { |
@@ -437,3 +439,50 @@ TEST(TestRclLifecycle, state_transitions) { |
437 | 439 | ret = rcl_lifecycle_state_machine_fini(&state_machine, &node, &allocator); |
438 | 440 | EXPECT_EQ(ret, RCL_RET_OK) << rcl_get_error_string().str; |
439 | 441 | } |
| 442 | + |
| 443 | +TEST(TestRclLifecycle, init_fini_maybe_fail) { |
| 444 | + rcl_node_t node = rcl_get_zero_initialized_node(); |
| 445 | + rcl_allocator_t allocator = rcl_get_default_allocator(); |
| 446 | + rcl_context_t context = rcl_get_zero_initialized_context(); |
| 447 | + rcl_node_options_t options = rcl_node_get_default_options(); |
| 448 | + rcl_init_options_t init_options = rcl_get_zero_initialized_init_options(); |
| 449 | + rcl_ret_t ret = rcl_init_options_init(&init_options, allocator); |
| 450 | + EXPECT_EQ(ret, RCL_RET_OK) << rcl_get_error_string().str; |
| 451 | + |
| 452 | + ret = rcl_init(0, nullptr, &init_options, &context); |
| 453 | + EXPECT_EQ(ret, RCL_RET_OK) << rcl_get_error_string().str; |
| 454 | + |
| 455 | + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT( |
| 456 | + { |
| 457 | + ASSERT_EQ(RCL_RET_OK, rcl_shutdown(&context)); |
| 458 | + ASSERT_EQ(RCL_RET_OK, rcl_context_fini(&context)); |
| 459 | + }); |
| 460 | + |
| 461 | + ret = rcl_node_init(&node, "node", "namespace", &context, &options); |
| 462 | + EXPECT_EQ(ret, RCL_RET_OK) << rcl_get_error_string().str; |
| 463 | + |
| 464 | + const rosidl_message_type_support_t * pn = |
| 465 | + ROSIDL_GET_MSG_TYPE_SUPPORT(lifecycle_msgs, msg, TransitionEvent); |
| 466 | + const rosidl_service_type_support_t * cs = |
| 467 | + ROSIDL_GET_SRV_TYPE_SUPPORT(lifecycle_msgs, srv, ChangeState); |
| 468 | + const rosidl_service_type_support_t * gs = |
| 469 | + ROSIDL_GET_SRV_TYPE_SUPPORT(lifecycle_msgs, srv, GetState); |
| 470 | + const rosidl_service_type_support_t * gas = |
| 471 | + ROSIDL_GET_SRV_TYPE_SUPPORT(lifecycle_msgs, srv, GetAvailableStates); |
| 472 | + const rosidl_service_type_support_t * gat = |
| 473 | + ROSIDL_GET_SRV_TYPE_SUPPORT(lifecycle_msgs, srv, GetAvailableTransitions); |
| 474 | + const rosidl_service_type_support_t * gtg = |
| 475 | + ROSIDL_GET_SRV_TYPE_SUPPORT(lifecycle_msgs, srv, GetAvailableTransitions); |
| 476 | + |
| 477 | + RCUTILS_FAULT_INJECTION_TEST( |
| 478 | + { |
| 479 | + // Init segfaults if this is not zero initialized |
| 480 | + rcl_lifecycle_state_machine_t sm = rcl_lifecycle_get_zero_initialized_state_machine(); |
| 481 | + |
| 482 | + ret = rcl_lifecycle_state_machine_init( |
| 483 | + &sm, &node, pn, cs, gs, gas, gat, gtg, true, &allocator); |
| 484 | + if (RCL_RET_OK == ret) { |
| 485 | + ret = rcl_lifecycle_state_machine_fini(&sm, &node, &allocator); |
| 486 | + } |
| 487 | + }); |
| 488 | +} |
0 commit comments