From 9092fc78745db48e076bf9dacba654d2e9b13df8 Mon Sep 17 00:00:00 2001 From: Devin Bonnie Date: Mon, 15 Apr 2019 10:09:53 -0700 Subject: [PATCH] Add method to read timer cancellation Signed-off-by: Devin Bonnie --- rclcpp/CMakeLists.txt | 8 ++++ rclcpp/include/rclcpp/timer.hpp | 5 +++ rclcpp/src/rclcpp/timer.cpp | 11 ++++++ rclcpp/test/test_timer.cpp | 67 +++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 rclcpp/test/test_timer.cpp diff --git a/rclcpp/CMakeLists.txt b/rclcpp/CMakeLists.txt index 798d8e60fd..94c006066c 100644 --- a/rclcpp/CMakeLists.txt +++ b/rclcpp/CMakeLists.txt @@ -397,6 +397,14 @@ if(BUILD_TESTING) target_link_libraries(test_time ${PROJECT_NAME}) endif() + ament_add_gtest(test_timer test/test_timer.cpp + APPEND_LIBRARY_DIRS "${append_library_dirs}") + if(TARGET test_timer) + ament_target_dependencies(test_timer + "rcl") + target_link_libraries(test_timer ${PROJECT_NAME}) + endif() + ament_add_gtest(test_time_source test/test_time_source.cpp APPEND_LIBRARY_DIRS "${append_library_dirs}") if(TARGET test_time_source) diff --git a/rclcpp/include/rclcpp/timer.hpp b/rclcpp/include/rclcpp/timer.hpp index 34eac08348..8ee489fea5 100644 --- a/rclcpp/include/rclcpp/timer.hpp +++ b/rclcpp/include/rclcpp/timer.hpp @@ -58,6 +58,11 @@ class TimerBase void cancel(); + /// Return the timer cancellation state. + RCLCPP_PUBLIC + bool + is_cancelled(); + RCLCPP_PUBLIC void reset(); diff --git a/rclcpp/src/rclcpp/timer.cpp b/rclcpp/src/rclcpp/timer.cpp index b46b3fd423..416131d8be 100644 --- a/rclcpp/src/rclcpp/timer.cpp +++ b/rclcpp/src/rclcpp/timer.cpp @@ -75,6 +75,17 @@ TimerBase::cancel() } } +bool +TimerBase::is_cancelled() +{ + bool is_canceled = false; + if (rcl_timer_is_canceled(timer_handle_.get(), &is_canceled) != RCL_RET_OK) { + throw std::runtime_error(std::string("Couldn't get timer cancelled state: ") + + rcl_get_error_string().str); + } + return is_canceled; +} + void TimerBase::reset() { diff --git a/rclcpp/test/test_timer.cpp b/rclcpp/test/test_timer.cpp new file mode 100644 index 0000000000..f6d23c675c --- /dev/null +++ b/rclcpp/test/test_timer.cpp @@ -0,0 +1,67 @@ +// Copyright 2019 Open Source Robotics Foundation, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include + +#include "rcl/timer.h" + +#include "rclcpp/rclcpp.hpp" + +using namespace std::chrono_literals; + +class TestTimer : public ::testing::Test +{ +protected: + void SetUp() + { + rclcpp::init(0, nullptr); + test_node = std::make_shared("test_timer_node"); + } + + void TearDown() + { + test_node.reset(); + rclcpp::shutdown(); + } + + rclcpp::Node::SharedPtr test_node; +}; + +TEST_F(TestTimer, test_is_cancelled) +{ + try { + auto timer = test_node->create_wall_timer(1s, + []() -> void { + printf("this is a test\n"); + } + ); + + // start and cancel + ASSERT_FALSE(timer->is_cancelled()); + timer->cancel(); + ASSERT_TRUE(timer->is_cancelled()); + + // reset and cancel + timer->reset(); + ASSERT_FALSE(timer->is_cancelled()); + timer->cancel(); + ASSERT_TRUE(timer->is_cancelled()); + } catch (std::exception & e) { + FAIL() << e.what(); + } +}