diff --git a/diagnostic_updater/CMakeLists.txt b/diagnostic_updater/CMakeLists.txt index 46c2ecc6..f7ab56a7 100644 --- a/diagnostic_updater/CMakeLists.txt +++ b/diagnostic_updater/CMakeLists.txt @@ -96,6 +96,7 @@ if(BUILD_TESTING) find_package(ament_cmake_pytest REQUIRED) ament_add_pytest_test(diagnostic_updater_test.py "test/diagnostic_updater_test.py") ament_add_pytest_test(test_DiagnosticStatusWrapper.py "test/test_diagnostic_status_wrapper.py") + ament_add_pytest_test(test_updater_period.py "test/test_updater_period.py") # ament_add_pytest_test(status_msg_test.py "test/status_msg_test.py") endif() diff --git a/diagnostic_updater/diagnostic_updater/_diagnostic_updater.py b/diagnostic_updater/diagnostic_updater/_diagnostic_updater.py index e05ba6de..067efd7b 100644 --- a/diagnostic_updater/diagnostic_updater/_diagnostic_updater.py +++ b/diagnostic_updater/diagnostic_updater/_diagnostic_updater.py @@ -313,7 +313,7 @@ def period(self): @period.setter def period(self, period): self.__period = period - self.timer.reset() + self.node.destroy_timer(self.timer) self.timer = self.node.create_timer(self.__period, self.update) def force_update(self): diff --git a/diagnostic_updater/test/test_updater_period.py b/diagnostic_updater/test/test_updater_period.py new file mode 100644 index 00000000..25e6e6ec --- /dev/null +++ b/diagnostic_updater/test/test_updater_period.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +"""Tests for the Updater period setter.""" + +import unittest + +from diagnostic_updater import Updater + +import rclpy + + +class TestUpdaterPeriod(unittest.TestCase): + + def setUp(self): + rclpy.init() + self.node = rclpy.create_node('test_updater_period') + + def tearDown(self): + self.node.destroy_node() + rclpy.shutdown() + + def test_period_setter_replaces_timer(self): + updater = Updater(self.node, period=1.0) + self.assertEqual(len(list(self.node.timers)), 1) + old_timer = updater.timer + + updater.period = 0.5 + + self.assertEqual(updater.period, 0.5) + # The old timer must be destroyed rather than left running next to + # the new one, otherwise update() gets called by both of them. + self.assertEqual(len(list(self.node.timers)), 1) + self.assertIsNot(updater.timer, old_timer) + self.assertEqual(updater.timer.timer_period_ns, 500000000)