Skip to content
Open
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
1 change: 1 addition & 0 deletions diagnostic_updater/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
35 changes: 35 additions & 0 deletions diagnostic_updater/test/test_updater_period.py
Original file line number Diff line number Diff line change
@@ -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)