Skip to content
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

executor ignore canceled timers (backport #220) #222

Merged
merged 1 commit into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions rclc/src/rclc/executor.c
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,13 @@ _rclc_execute(rclc_executor_handle_t * handle)
case TIMER:
// case TIMER_WITH_CONTEXT:
rc = rcl_timer_call(handle->timer);

// cancled timer are not handled, return success
if (rc == RCL_RET_TIMER_CANCELED) {
rc = RCL_RET_OK;
break;
}

if (rc != RCL_RET_OK) {
PRINT_RCLC_ERROR(rclc_execute, rcl_timer_call);
return rc;
Expand Down
89 changes: 89 additions & 0 deletions rclc/test/rclc/test_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,95 @@ TEST_F(TestDefaultExecutor, executor_spin_timer) {
EXPECT_EQ(RCL_RET_OK, rc) << rcl_get_error_string().str;
}

TEST_F(TestDefaultExecutor, executor_spin_publisher_timer_cancelled) {
rcl_ret_t rc;
rclc_executor_t executor;
unsigned int expected_msg;

rc = rclc_executor_init(&executor, &this->context, 10, this->allocator_ptr);
EXPECT_EQ(RCL_RET_OK, rc) << rcl_get_error_string().str;

_executor_results_init();

rc = rclc_executor_add_subscription(
&executor, &this->sub1, &this->sub1_msg,
&CALLBACK_1, ON_NEW_DATA);

rc = rclc_executor_add_timer(&executor, &this->timer1);

for (unsigned int i = 0; i < TC_SPIN_SOME_PUBLISHED_MSGS; i++) {
rc = rcl_publish(&this->pub1, &this->pub1_msg, nullptr);
EXPECT_EQ(RCL_RET_OK, rc) << " pub1 not published";
}

// wait until messages are received
bool success = false;
unsigned int tries;
unsigned int max_tries = 100;
uint64_t timeout_ns = 100000000; // 100ms

rc = rcl_timer_cancel(&this->timer1);
// process subscriptions. Assumption: messages for sub1 available
for (unsigned int i = 0; i < 100; i++) {
// Assumption: messages for all sub1, sub2 and sub3 are available
_wait_for_msg(
&this->sub1, &this->context, max_tries, timeout_ns, &tries,
&success);
ASSERT_TRUE(success);


EXPECT_EQ(RCL_RET_OK, rc) << "failed to cancel timer";

rc = rclc_executor_spin_some(&executor, rclc_test_timeout_ns);
if ((rc == RCL_RET_OK) || (rc == RCL_RET_TIMEOUT)) {
// valid return values
} else {
// any other error
EXPECT_EQ(RCL_RET_OK, rc) << "spin_some error";
}
if (_cb1_cnt == TC_SPIN_SOME_PUBLISHED_MSGS) {
break;
}
}

expected_msg = TC_SPIN_SOME_PUBLISHED_MSGS;
EXPECT_EQ(_cb1_cnt, expected_msg) << "cb1 msg does not match";

rc = rclc_executor_fini(&executor);
EXPECT_EQ(RCL_RET_OK, rc) << rcl_get_error_string().str;
rcutils_reset_error();
}

TEST_F(TestDefaultExecutor, executor_spin_timer_cancelled) {
rcl_ret_t rc;
rclc_executor_t executor;
rc = rclc_executor_init(&executor, &this->context, 10, this->allocator_ptr);
EXPECT_EQ(RCL_RET_OK, rc) << rcl_get_error_string().str;

// spin_timeout must be < timer1_timeout
const unsigned int spin_timeout = 50;
const unsigned int spin_repeat = 10;
const unsigned int expected_callbacks = (spin_timeout * spin_repeat) / timer1_timeout;
_cbt_cnt = 0;

rc = rclc_executor_add_timer(&executor, &this->timer1);
EXPECT_EQ(RCL_RET_OK, rc) << rcl_get_error_string().str;

for (size_t i = 0; i < spin_repeat; i++) {
rclc_executor_spin_some(&executor, RCL_MS_TO_NS(spin_timeout));
if (i > spin_repeat / 2) {
rc = rcl_timer_cancel(&this->timer1);
EXPECT_EQ(RCL_RET_OK, rc) << rcl_get_error_string().str;
}
}

EXPECT_LT(_cbt_cnt, expected_callbacks);

// tear down
rc = rclc_executor_fini(&executor);
EXPECT_EQ(RCL_RET_OK, rc) << rcl_get_error_string().str;
}

TEST_F(TestDefaultExecutor, executor_remove_timer) {
rcl_ret_t rc;
rclc_executor_t executor;
Expand Down