diff --git a/rcl/test/rcl/test_wait.cpp b/rcl/test/rcl/test_wait.cpp index d61c02cd44..ae9c56eae4 100644 --- a/rcl/test/rcl/test_wait.cpp +++ b/rcl/test/rcl/test_wait.cpp @@ -49,7 +49,8 @@ class CLASSNAME (WaitSetTestFixture, RMW_IMPLEMENTATION) : public ::testing::Tes ret = rcl_init_options_init(&init_options, rcl_get_default_allocator()); ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ - EXPECT_EQ(RCL_RET_OK, rcl_init_options_fini(&init_options)) << rcl_get_error_string().str; + ret = rcl_init_options_fini(&init_options); + EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; }); this->context_ptr = new rcl_context_t; *this->context_ptr = rcl_get_zero_initialized_context(); @@ -66,11 +67,15 @@ class CLASSNAME (WaitSetTestFixture, RMW_IMPLEMENTATION) : public ::testing::Tes }; TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), test_resize_to_zero) { + rcl_ret_t ret; // Initialize a wait set with a subscription and then resize it to zero. rcl_wait_set_t wait_set = rcl_get_zero_initialized_wait_set(); - rcl_ret_t ret = - rcl_wait_set_init(&wait_set, 1, 1, 1, 1, 1, 0, context_ptr, rcl_get_default_allocator()); + ret = rcl_wait_set_init(&wait_set, 1, 1, 1, 1, 1, 0, context_ptr, rcl_get_default_allocator()); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + ret = rcl_wait_set_fini(&wait_set); + EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + }); ret = rcl_wait_set_resize(&wait_set, 0u, 0u, 0u, 0u, 0u, 0u); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; @@ -80,17 +85,18 @@ TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), test_resize_to_zero) { EXPECT_EQ(wait_set.size_of_clients, 0ull); EXPECT_EQ(wait_set.size_of_services, 0ull); EXPECT_EQ(wait_set.size_of_timers, 0ull); - - ret = rcl_wait_set_fini(&wait_set); - ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; } // Test rcl_wait with a positive finite timeout value (1ms) TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), finite_timeout) { + rcl_ret_t ret; rcl_wait_set_t wait_set = rcl_get_zero_initialized_wait_set(); - rcl_ret_t ret = - rcl_wait_set_init(&wait_set, 0, 0, 1, 0, 0, 0, context_ptr, rcl_get_default_allocator()); + ret = rcl_wait_set_init(&wait_set, 0, 0, 1, 0, 0, 0, context_ptr, rcl_get_default_allocator()); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + ret = rcl_wait_set_fini(&wait_set); + EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + }); int64_t timeout = RCL_MS_TO_NS(10); // nanoseconds std::chrono::steady_clock::time_point before_sc = std::chrono::steady_clock::now(); @@ -100,23 +106,28 @@ TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), finite_timeout) { // Check time int64_t diff = std::chrono::duration_cast(after_sc - before_sc).count(); EXPECT_LE(diff, timeout + TOLERANCE); - - ret = rcl_wait_set_fini(&wait_set); - ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; } // Check that a timer overrides a negative timeout value (blocking forever) TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), negative_timeout) { + rcl_ret_t ret; rcl_wait_set_t wait_set = rcl_get_zero_initialized_wait_set(); - rcl_ret_t ret = - rcl_wait_set_init(&wait_set, 0, 1, 1, 0, 0, 0, context_ptr, rcl_get_default_allocator()); + ret = rcl_wait_set_init(&wait_set, 0, 1, 1, 0, 0, 0, context_ptr, rcl_get_default_allocator()); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + ret = rcl_wait_set_fini(&wait_set); + EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + }); // Add a dummy guard condition to avoid an error rcl_guard_condition_t guard_cond = rcl_get_zero_initialized_guard_condition(); ret = rcl_guard_condition_init( &guard_cond, this->context_ptr, rcl_guard_condition_get_default_options()); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + ret = rcl_guard_condition_fini(&guard_cond); + EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + }); ret = rcl_wait_set_add_guard_condition(&wait_set, &guard_cond, NULL); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; @@ -124,22 +135,21 @@ TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), negative_timeout) { rcl_allocator_t allocator = rcl_get_default_allocator(); ret = rcl_clock_init(RCL_STEADY_TIME, &clock, &allocator); ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + ret = rcl_clock_fini(&clock); + EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + }); rcl_timer_t timer = rcl_get_zero_initialized_timer(); ret = rcl_timer_init( &timer, &clock, this->context_ptr, RCL_MS_TO_NS(10), nullptr, rcl_get_default_allocator()); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; - ret = rcl_wait_set_add_timer(&wait_set, &timer, NULL); - EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; - OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ - rcl_ret_t ret = rcl_guard_condition_fini(&guard_cond); - EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; - ret = rcl_wait_set_fini(&wait_set); - EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; ret = rcl_timer_fini(&timer); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; }); + ret = rcl_wait_set_add_timer(&wait_set, &timer, NULL); + EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; int64_t timeout = -1; std::chrono::steady_clock::time_point before_sc = std::chrono::steady_clock::now(); @@ -154,25 +164,26 @@ TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), negative_timeout) { // Test rcl_wait with a timeout value of 0 (non-blocking) TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), zero_timeout) { + rcl_ret_t ret; rcl_wait_set_t wait_set = rcl_get_zero_initialized_wait_set(); - rcl_ret_t ret = - rcl_wait_set_init(&wait_set, 0, 1, 1, 0, 0, 0, context_ptr, rcl_get_default_allocator()); + ret = rcl_wait_set_init(&wait_set, 0, 1, 1, 0, 0, 0, context_ptr, rcl_get_default_allocator()); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + ret = rcl_wait_set_fini(&wait_set); + EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + }); // Add a dummy guard condition to avoid an error rcl_guard_condition_t guard_cond = rcl_get_zero_initialized_guard_condition(); ret = rcl_guard_condition_init( &guard_cond, this->context_ptr, rcl_guard_condition_get_default_options()); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; - ret = rcl_wait_set_add_guard_condition(&wait_set, &guard_cond, NULL); - EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; - OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ - rcl_ret_t ret = rcl_guard_condition_fini(&guard_cond); - EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; - ret = rcl_wait_set_fini(&wait_set); + ret = rcl_guard_condition_fini(&guard_cond); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; }); + ret = rcl_wait_set_add_guard_condition(&wait_set, &guard_cond, NULL); + EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; // Time spent during wait should be negligible. int64_t timeout = 0; @@ -187,27 +198,28 @@ TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), zero_timeout) { // Test rcl_wait with a timeout value of 0 (non-blocking) and an already triggered guard condition TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), zero_timeout_triggered_guard_condition) { + rcl_ret_t ret; rcl_wait_set_t wait_set = rcl_get_zero_initialized_wait_set(); - rcl_ret_t ret = - rcl_wait_set_init(&wait_set, 0, 1, 0, 0, 0, 0, context_ptr, rcl_get_default_allocator()); + ret = rcl_wait_set_init(&wait_set, 0, 1, 0, 0, 0, 0, context_ptr, rcl_get_default_allocator()); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + ret = rcl_wait_set_fini(&wait_set); + EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + }); rcl_guard_condition_t guard_cond = rcl_get_zero_initialized_guard_condition(); ret = rcl_guard_condition_init( &guard_cond, this->context_ptr, rcl_guard_condition_get_default_options()); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + ret = rcl_guard_condition_fini(&guard_cond); + EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + }); ret = rcl_wait_set_add_guard_condition(&wait_set, &guard_cond, NULL); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; ret = rcl_trigger_guard_condition(&guard_cond); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; - OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ - rcl_ret_t ret = rcl_guard_condition_fini(&guard_cond); - EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; - ret = rcl_wait_set_fini(&wait_set); - EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; - }); - // Time spent during wait should be negligible. int64_t timeout = 0; std::chrono::steady_clock::time_point before_sc = std::chrono::steady_clock::now(); @@ -221,16 +233,24 @@ TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), zero_timeout_triggered // Check that a canceled timer doesn't wake up rcl_wait TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), canceled_timer) { + rcl_ret_t ret; rcl_wait_set_t wait_set = rcl_get_zero_initialized_wait_set(); - rcl_ret_t ret = - rcl_wait_set_init(&wait_set, 0, 1, 1, 0, 0, 0, context_ptr, rcl_get_default_allocator()); + ret = rcl_wait_set_init(&wait_set, 0, 1, 1, 0, 0, 0, context_ptr, rcl_get_default_allocator()); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + ret = rcl_wait_set_fini(&wait_set); + EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + }); // Add a dummy guard condition to avoid an error rcl_guard_condition_t guard_cond = rcl_get_zero_initialized_guard_condition(); ret = rcl_guard_condition_init( &guard_cond, this->context_ptr, rcl_guard_condition_get_default_options()); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + ret = rcl_guard_condition_fini(&guard_cond); + EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + }); ret = rcl_wait_set_add_guard_condition(&wait_set, &guard_cond, NULL); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; @@ -238,25 +258,24 @@ TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), canceled_timer) { rcl_allocator_t allocator = rcl_get_default_allocator(); ret = rcl_clock_init(RCL_STEADY_TIME, &clock, &allocator); ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + ret = rcl_clock_fini(&clock); + EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + }); rcl_timer_t canceled_timer = rcl_get_zero_initialized_timer(); ret = rcl_timer_init( &canceled_timer, &clock, this->context_ptr, RCL_MS_TO_NS(1), nullptr, rcl_get_default_allocator()); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; - ret = rcl_timer_cancel(&canceled_timer); - EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; - ret = rcl_wait_set_add_timer(&wait_set, &canceled_timer, NULL); - EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; - OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ - rcl_ret_t ret = rcl_guard_condition_fini(&guard_cond); - EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; - ret = rcl_wait_set_fini(&wait_set); - EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; ret = rcl_timer_fini(&canceled_timer); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; }); + ret = rcl_timer_cancel(&canceled_timer); + EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + ret = rcl_wait_set_add_timer(&wait_set, &canceled_timer, NULL); + EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; int64_t timeout = RCL_MS_TO_NS(10); std::chrono::steady_clock::time_point before_sc = std::chrono::steady_clock::now(); @@ -267,17 +286,19 @@ TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), canceled_timer) { // Check time int64_t diff = std::chrono::duration_cast(after_sc - before_sc).count(); EXPECT_LE(diff, RCL_MS_TO_NS(10) + TOLERANCE); - - ret = rcl_clock_fini(&clock); - EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; } // Test rcl_wait_set_t with excess capacity works. TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), excess_capacity) { + rcl_ret_t ret; rcl_wait_set_t wait_set = rcl_get_zero_initialized_wait_set(); - rcl_ret_t ret = - rcl_wait_set_init(&wait_set, 42, 42, 42, 42, 42, 0, context_ptr, rcl_get_default_allocator()); + ret = rcl_wait_set_init( + &wait_set, 42, 42, 42, 42, 42, 0, context_ptr, rcl_get_default_allocator()); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + ret = rcl_wait_set_fini(&wait_set); + EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + }); int64_t timeout = 1; ret = rcl_wait(&wait_set, timeout); @@ -350,7 +371,6 @@ TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), multi_wait_set_threade // *INDENT-ON* // Setup each test set. for (auto & test_set : test_sets) { - rcl_ret_t ret; // init the wake count test_set.wake_count.store(0); // setup the guard condition @@ -358,11 +378,13 @@ TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), multi_wait_set_threade ret = rcl_guard_condition_init( &test_set.guard_condition, this->context_ptr, rcl_guard_condition_get_default_options()); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + // guard_condition must live longer than this loop. Call fini from function-level exit-scope. // setup the wait set test_set.wait_set = rcl_get_zero_initialized_wait_set(); ret = rcl_wait_set_init( &test_set.wait_set, 0, 1, 0, 0, 0, 0, context_ptr, rcl_get_default_allocator()); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + // wait_set must live longer than this loop. Call fini from function-level exit-scope. ret = rcl_wait_set_add_guard_condition(&test_set.wait_set, &test_set.guard_condition, NULL); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; test_set.thread_id = 0; @@ -370,7 +392,7 @@ TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), multi_wait_set_threade // Setup safe tear-down. OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ for (auto & test_set : test_sets) { - rcl_ret_t ret = rcl_guard_condition_fini(&test_set.guard_condition); + ret = rcl_guard_condition_fini(&test_set.guard_condition); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; ret = rcl_wait_set_fini(&test_set.wait_set); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; @@ -417,23 +439,24 @@ TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), multi_wait_set_threade // Check the interaction of a guard condition and a negative timeout by // triggering a guard condition in a separate thread TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), guard_condition) { + rcl_ret_t ret; rcl_wait_set_t wait_set = rcl_get_zero_initialized_wait_set(); - rcl_ret_t ret = - rcl_wait_set_init(&wait_set, 0, 1, 0, 0, 0, 0, context_ptr, rcl_get_default_allocator()); + ret = rcl_wait_set_init(&wait_set, 0, 1, 0, 0, 0, 0, context_ptr, rcl_get_default_allocator()); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + ret = rcl_wait_set_fini(&wait_set); + EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + }); rcl_guard_condition_t guard_cond = rcl_get_zero_initialized_guard_condition(); ret = rcl_guard_condition_init( &guard_cond, this->context_ptr, rcl_guard_condition_get_default_options()); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; - ret = rcl_wait_set_add_guard_condition(&wait_set, &guard_cond, NULL); - EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; - OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ - rcl_ret_t ret = rcl_wait_set_fini(&wait_set); - EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; ret = rcl_guard_condition_fini(&guard_cond); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; }); + ret = rcl_wait_set_add_guard_condition(&wait_set, &guard_cond, NULL); + EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; std::promise p; @@ -464,11 +487,16 @@ TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), guard_condition) { // Check that index arguments are properly set when adding entities TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), add_with_index) { + rcl_ret_t ret; const size_t kNumEntities = 3u; rcl_wait_set_t wait_set = rcl_get_zero_initialized_wait_set(); - rcl_ret_t ret = rcl_wait_set_init( + ret = rcl_wait_set_init( &wait_set, 0, kNumEntities, 0, 0, 0, 0, context_ptr, rcl_get_default_allocator()); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + ret = rcl_wait_set_fini(&wait_set); + EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + }); // Initialize to invalid value size_t guard_condition_index = 42u; @@ -479,6 +507,10 @@ TEST_F(CLASSNAME(WaitSetTestFixture, RMW_IMPLEMENTATION), add_with_index) { ret = rcl_guard_condition_init( &guard_conditions[i], this->context_ptr, rcl_guard_condition_get_default_options()); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT({ + ret = rcl_guard_condition_fini(&guard_conditions[i]); + EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str; + }); ret = rcl_wait_set_add_guard_condition( &wait_set, &guard_conditions[i], &guard_condition_index); EXPECT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;