|
37 | 37 | )
|
38 | 38 |
|
39 | 39 | from tests.communication.ros2.helpers import (
|
| 40 | + ClockPublisher, |
40 | 41 | ImagePublisher,
|
41 | 42 | MessagePublisher,
|
42 | 43 | MessageSubscriber,
|
@@ -225,3 +226,122 @@ def test_get_transform_tool(ros_setup: None, request: pytest.FixtureRequest) ->
|
225 | 226 | assert "rotation" in response
|
226 | 227 | finally:
|
227 | 228 | shutdown_executors_and_threads(executors, threads)
|
| 229 | + |
| 230 | + |
| 231 | +def test_get_transform_tool_with_stale_tf_data( |
| 232 | + ros_setup: None, request: pytest.FixtureRequest |
| 233 | +) -> None: |
| 234 | + """Test that GetROS2TransformTool returns stale transform data with warning.""" |
| 235 | + topic_name = f"{request.node.originalname}_topic" # type: ignore |
| 236 | + connector = ROS2Connector() |
| 237 | + publisher = TransformPublisher(topic=topic_name) |
| 238 | + executors, threads = multi_threaded_spinner([publisher]) |
| 239 | + tool = GetROS2TransformTool(connector=connector) |
| 240 | + |
| 241 | + # Let the publisher run for a bit to establish transforms |
| 242 | + time.sleep(1.0) |
| 243 | + response = tool._run( |
| 244 | + source_frame="map", |
| 245 | + target_frame="base_link", |
| 246 | + timeout_sec=5.0, |
| 247 | + ) # type: ignore |
| 248 | + assert "translation" in response |
| 249 | + assert "rotation" in response |
| 250 | + |
| 251 | + # stop the /tf publisher |
| 252 | + publisher.timer.cancel() |
| 253 | + |
| 254 | + # wait for some time to pass then get the transform again |
| 255 | + time.sleep(2.0) |
| 256 | + |
| 257 | + try: |
| 258 | + response = tool._run( |
| 259 | + source_frame="map", |
| 260 | + target_frame="base_link", |
| 261 | + timeout_sec=5.0, |
| 262 | + ) # type: ignore |
| 263 | + |
| 264 | + response_lower = response.lower() |
| 265 | + staleness_indicators = [ |
| 266 | + "stale", |
| 267 | + "old", |
| 268 | + "outdated", |
| 269 | + "warning", |
| 270 | + "invalid", |
| 271 | + ] |
| 272 | + staleness_warning_present = any( |
| 273 | + indicator in response_lower for indicator in staleness_indicators |
| 274 | + ) |
| 275 | + |
| 276 | + # Stale data should be warned about |
| 277 | + if not staleness_warning_present: |
| 278 | + pytest.fail( |
| 279 | + f"Response: {response}. " |
| 280 | + "Expected behavior: Either include staleness warning in response or raise appropriate error." |
| 281 | + ) |
| 282 | + |
| 283 | + finally: |
| 284 | + # Clean shutdown |
| 285 | + shutdown_executors_and_threads(executors, threads) |
| 286 | + |
| 287 | + |
| 288 | +def test_get_transform_tool_with_stale_tf_data_sim_time( |
| 289 | + ros_setup: None, request: pytest.FixtureRequest |
| 290 | +) -> None: |
| 291 | + """Test that GetROS2TransformTool returns stale transform data with warning when using sim time.""" |
| 292 | + topic_name = f"{request.node.originalname}_topic" # type: ignore |
| 293 | + connector = ROS2Connector(use_sim_time=True) |
| 294 | + |
| 295 | + # Create clock publisher to provide simulation time |
| 296 | + clock_publisher = ClockPublisher() |
| 297 | + publisher = TransformPublisher(topic=topic_name, use_sim_time=True) |
| 298 | + executors, threads = multi_threaded_spinner([clock_publisher, publisher]) |
| 299 | + tool = GetROS2TransformTool(connector=connector) |
| 300 | + |
| 301 | + # Let the publishers run for a bit to establish transforms and clock |
| 302 | + time.sleep(1.0) |
| 303 | + response = tool._run( |
| 304 | + source_frame="map", |
| 305 | + target_frame="base_link", |
| 306 | + timeout_sec=5.0, |
| 307 | + ) # type: ignore |
| 308 | + assert "translation" in response |
| 309 | + assert "rotation" in response |
| 310 | + |
| 311 | + # stop the /tf publisher |
| 312 | + publisher.timer.cancel() |
| 313 | + |
| 314 | + # wait for some time to pass then get the transform again |
| 315 | + time.sleep(2.0) |
| 316 | + |
| 317 | + try: |
| 318 | + response = tool._run( |
| 319 | + source_frame="map", |
| 320 | + target_frame="base_link", |
| 321 | + timeout_sec=5.0, |
| 322 | + ) # type: ignore |
| 323 | + |
| 324 | + response_lower = response.lower() |
| 325 | + staleness_indicators = [ |
| 326 | + "stale", |
| 327 | + "old", |
| 328 | + "outdated", |
| 329 | + "warning", |
| 330 | + "invalid", |
| 331 | + ] |
| 332 | + staleness_warning_present = any( |
| 333 | + indicator in response_lower for indicator in staleness_indicators |
| 334 | + ) |
| 335 | + |
| 336 | + # Stale data should be warned about |
| 337 | + if not staleness_warning_present: |
| 338 | + pytest.fail( |
| 339 | + f"Response: {response}. " |
| 340 | + "Expected behavior: Either include staleness warning in response or raise appropriate error." |
| 341 | + ) |
| 342 | + |
| 343 | + finally: |
| 344 | + clock_publisher.timer.cancel() |
| 345 | + # wait for the timer to be canceled |
| 346 | + time.sleep(0.1) |
| 347 | + shutdown_executors_and_threads(executors, threads) |
0 commit comments