From 708dfbf7bff220cb4571ae33d5b523e7c71f243b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C5=82a=C5=BCej=20Sowa?= Date: Tue, 17 Dec 2024 15:10:13 +0000 Subject: [PATCH] Fix mypy errors in rosbridge_server tests --- .../test/websocket/advertise_action.test.py | 7 +++++++ .../websocket/advertise_action_feedback.test.py | 8 +++++++- .../test/websocket/advertise_service.test.py | 1 + .../advertise_service_duplicate.test.py | 1 + .../websocket/best_effort_publisher.test.py | 1 + .../test/websocket/call_service.test.py | 1 + rosbridge_server/test/websocket/common.py | 17 ++++++++++------- .../test/websocket/multiple_subscribers.test.py | 1 + .../websocket/multiple_subscribers_raw.test.py | 1 + .../test/websocket/send_action_goal.test.py | 1 + rosbridge_server/test/websocket/smoke.test.py | 1 + .../websocket/transient_local_publisher.test.py | 1 + 12 files changed, 33 insertions(+), 8 deletions(-) diff --git a/rosbridge_server/test/websocket/advertise_action.test.py b/rosbridge_server/test/websocket/advertise_action.test.py index 5021aa635..84997a697 100644 --- a/rosbridge_server/test/websocket/advertise_action.test.py +++ b/rosbridge_server/test/websocket/advertise_action.test.py @@ -7,6 +7,7 @@ from example_interfaces.action import Fibonacci from rclpy.action import ActionClient from rclpy.node import Node +from rclpy.task import Future from twisted.python import log sys.path.append(os.path.dirname(__file__)) # enable importing from common.py in this directory @@ -20,6 +21,9 @@ class TestAdvertiseAction(unittest.TestCase): + goal1_result_future: Future | None + goal2_result_future: Future | None + def goal1_response_callback(self, future): goal_handle = future.result() if not goal_handle.accepted: @@ -48,6 +52,7 @@ async def test_two_concurrent_calls(self, node: Node, make_client): requests_future, ws_client.message_handler = expect_messages( 2, "WebSocket", node.get_logger() ) + assert node.executor is not None requests_future.add_done_callback(lambda _: node.executor.wake()) self.goal1_result_future = None @@ -90,8 +95,10 @@ async def test_two_concurrent_calls(self, node: Node, make_client): } ) + assert self.goal1_result_future is not None result1 = await self.goal1_result_future self.assertEqual(result1.result, Fibonacci.Result(sequence=[0, 1, 1, 2])) + assert self.goal2_result_future is not None result2 = await self.goal2_result_future self.assertEqual(result2.result, Fibonacci.Result(sequence=[0, 1, 1, 2, 3, 5])) diff --git a/rosbridge_server/test/websocket/advertise_action_feedback.test.py b/rosbridge_server/test/websocket/advertise_action_feedback.test.py index 69b2c6864..e1be67eb9 100644 --- a/rosbridge_server/test/websocket/advertise_action_feedback.test.py +++ b/rosbridge_server/test/websocket/advertise_action_feedback.test.py @@ -2,11 +2,13 @@ import os import sys import unittest +from typing import Any from action_msgs.msg import GoalStatus from example_interfaces.action import Fibonacci from rclpy.action import ActionClient from rclpy.node import Node +from rclpy.task import Future from twisted.python import log sys.path.append(os.path.dirname(__file__)) # enable importing from common.py in this directory @@ -20,7 +22,9 @@ class TestActionFeedback(unittest.TestCase): - def goal_response_callback(self, future): + goal_result_future: Future | None + + def goal_response_callback(self, future: Future): goal_handle = future.result() if not goal_handle.accepted: return @@ -45,6 +49,7 @@ async def test_feedback(self, node: Node, make_client): requests_future, ws_client.message_handler = expect_messages( 1, "WebSocket", node.get_logger() ) + assert node.executor is not None requests_future.add_done_callback(lambda _: node.executor.wake()) self.goal_result_future = None @@ -81,6 +86,7 @@ async def test_feedback(self, node: Node, make_client): } ) + assert self.goal_result_future is not None result = await self.goal_result_future self.assertIsNotNone(self.latest_feedback) self.assertEqual(self.latest_feedback.feedback, Fibonacci.Feedback(sequence=[0, 1, 1, 2])) diff --git a/rosbridge_server/test/websocket/advertise_service.test.py b/rosbridge_server/test/websocket/advertise_service.test.py index 6097dc3f3..724ffdf73 100644 --- a/rosbridge_server/test/websocket/advertise_service.test.py +++ b/rosbridge_server/test/websocket/advertise_service.test.py @@ -34,6 +34,7 @@ async def test_two_concurrent_calls(self, node: Node, make_client): requests_future, ws_client.message_handler = expect_messages( 2, "WebSocket", node.get_logger() ) + assert node.executor is not None requests_future.add_done_callback(lambda _: node.executor.wake()) response1_future = client.call_async(SetBool.Request(data=True)) diff --git a/rosbridge_server/test/websocket/advertise_service_duplicate.test.py b/rosbridge_server/test/websocket/advertise_service_duplicate.test.py index 466e44350..08ba07797 100644 --- a/rosbridge_server/test/websocket/advertise_service_duplicate.test.py +++ b/rosbridge_server/test/websocket/advertise_service_duplicate.test.py @@ -33,6 +33,7 @@ async def test_double_advertise(self, node: Node, make_client): requests1_future, ws_client1.message_handler = expect_messages( 1, "WebSocket 1", node.get_logger() ) + assert node.executor is not None requests1_future.add_done_callback(lambda _: node.executor.wake()) client.call_async(SetBool.Request(data=True)) diff --git a/rosbridge_server/test/websocket/best_effort_publisher.test.py b/rosbridge_server/test/websocket/best_effort_publisher.test.py index 2c84e6907..f011b8853 100644 --- a/rosbridge_server/test/websocket/best_effort_publisher.test.py +++ b/rosbridge_server/test/websocket/best_effort_publisher.test.py @@ -44,6 +44,7 @@ async def test_best_effort_publisher(self, node: Node, make_client): ws1_completed_future, ws_client1.message_handler = expect_messages( 1, "WebSocket 1", node.get_logger() ) + assert node.executor is not None ws1_completed_future.add_done_callback(lambda _: node.executor.wake()) self.assertEqual( diff --git a/rosbridge_server/test/websocket/call_service.test.py b/rosbridge_server/test/websocket/call_service.test.py index c8e041819..b6e8948be 100644 --- a/rosbridge_server/test/websocket/call_service.test.py +++ b/rosbridge_server/test/websocket/call_service.test.py @@ -35,6 +35,7 @@ def service_cb(req, res): responses_future, ws_client.message_handler = expect_messages( 1, "WebSocket", node.get_logger() ) + assert node.executor is not None responses_future.add_done_callback(lambda _: node.executor.wake()) ws_client.sendJson( diff --git a/rosbridge_server/test/websocket/common.py b/rosbridge_server/test/websocket/common.py index 450534ae9..e380bfaeb 100644 --- a/rosbridge_server/test/websocket/common.py +++ b/rosbridge_server/test/websocket/common.py @@ -2,10 +2,12 @@ import json from typing import Any, Awaitable, Callable +from launch.launch_description import LaunchDescription import launch import launch_ros import rclpy import rclpy.task + from autobahn.twisted.websocket import WebSocketClientFactory, WebSocketClientProtocol from rcl_interfaces.srv import GetParameters from rclpy.executors import SingleThreadedExecutor @@ -60,19 +62,19 @@ def _generate_node(): try: from launch_testing.actions import ReadyToTest - def generate_test_description() -> launch.LaunchDescription: + def generate_test_description() -> LaunchDescription: """ Generate a launch description that runs the websocket server. Re-export this from a test file and use add_launch_test() to run the test. """ - return launch.LaunchDescription([_generate_node(), ReadyToTest()]) + return LaunchDescription([_generate_node(), ReadyToTest()]) except ImportError: - def generate_test_description(ready_fn) -> launch.LaunchDescription: + def generate_test_description(ready_fn) -> LaunchDescription: # type: ignore[misc] """ Generate a launch description that runs the websocket server. Re-export this from a test file and use add_launch_test() to run the test. """ - return launch.LaunchDescription( + return LaunchDescription( [_generate_node(), launch.actions.OpaqueFunction(function=lambda context: ready_fn())] ) @@ -97,6 +99,7 @@ async def connect_to_server(node: Node) -> TestClientProtocol: factory.protocol = TestClientProtocol future = rclpy.task.Future() + assert node.executor is not None future.add_done_callback(lambda _: node.executor.wake()) def connect(): @@ -104,7 +107,7 @@ def connect(): future.set_result ) - reactor.callFromThread(connect) + reactor.callFromThread(connect) # type: ignore[attr-defined] protocol = await future protocol.connected_future.add_done_callback(lambda _: node.executor.wake()) @@ -128,8 +131,8 @@ async def task(): future = executor.create_task(task) - reactor.callInThread(executor.spin_until_future_complete, future) - reactor.run(installSignalHandlers=False) + reactor.callInThread(executor.spin_until_future_complete, future) # type: ignore[attr-defined] + reactor.run(installSignalHandlers=False) # type: ignore[attr-defined] executor.remove_node(node) node.destroy_node() diff --git a/rosbridge_server/test/websocket/multiple_subscribers.test.py b/rosbridge_server/test/websocket/multiple_subscribers.test.py index 58aaadb1b..f49080483 100644 --- a/rosbridge_server/test/websocket/multiple_subscribers.test.py +++ b/rosbridge_server/test/websocket/multiple_subscribers.test.py @@ -40,6 +40,7 @@ async def test_multiple_subscribers(self, node: Node, make_client): ws1_completed_future, ws_client1.message_handler = expect_messages( 1, "WebSocket 1", node.get_logger() ) + assert node.executor is not None ws1_completed_future.add_done_callback(lambda _: node.executor.wake()) ws2_completed_future, ws_client2.message_handler = expect_messages( 1, "WebSocket 2", node.get_logger() diff --git a/rosbridge_server/test/websocket/multiple_subscribers_raw.test.py b/rosbridge_server/test/websocket/multiple_subscribers_raw.test.py index a1f5c1f3e..ddfe653c5 100644 --- a/rosbridge_server/test/websocket/multiple_subscribers_raw.test.py +++ b/rosbridge_server/test/websocket/multiple_subscribers_raw.test.py @@ -33,6 +33,7 @@ async def test_multiple_subscribers(self, node: Node, make_client): ws1_completed_future, ws_client1.message_handler = expect_messages( 1, "WebSocket 1", node.get_logger() ) + assert node.executor is not None ws1_completed_future.add_done_callback(lambda _: node.executor.wake()) ws2_completed_future, ws_client2.message_handler = expect_messages( 1, "WebSocket 2", node.get_logger() diff --git a/rosbridge_server/test/websocket/send_action_goal.test.py b/rosbridge_server/test/websocket/send_action_goal.test.py index 1db33ab53..bd4b9b77e 100644 --- a/rosbridge_server/test/websocket/send_action_goal.test.py +++ b/rosbridge_server/test/websocket/send_action_goal.test.py @@ -56,6 +56,7 @@ async def test_one_call(self, node: Node, make_client): responses_future, ws_client.message_handler = expect_messages( 5, "WebSocket", node.get_logger() ) + assert node.executor is not None responses_future.add_done_callback(lambda _: node.executor.wake()) ws_client.sendJson( diff --git a/rosbridge_server/test/websocket/smoke.test.py b/rosbridge_server/test/websocket/smoke.test.py index 2447ed424..f8959fa4c 100644 --- a/rosbridge_server/test/websocket/smoke.test.py +++ b/rosbridge_server/test/websocket/smoke.test.py @@ -37,6 +37,7 @@ async def test_smoke(self, node: Node, make_client): ws_completed_future, ws_client.message_handler = expect_messages( NUM_MSGS, "WebSocket", node.get_logger() ) + assert node.executor is not None ws_completed_future.add_done_callback(lambda _: node.executor.wake()) sub_a = node.create_subscription(String, A_TOPIC, sub_handler, NUM_MSGS) diff --git a/rosbridge_server/test/websocket/transient_local_publisher.test.py b/rosbridge_server/test/websocket/transient_local_publisher.test.py index d492350fb..1ad4ae1a0 100644 --- a/rosbridge_server/test/websocket/transient_local_publisher.test.py +++ b/rosbridge_server/test/websocket/transient_local_publisher.test.py @@ -46,6 +46,7 @@ async def test_transient_local_publisher(self, node: Node, make_client): ws_completed_future, ws_client.message_handler = expect_messages( 1, "WebSocket " + str(num), node.get_logger() ) + assert node.executor is not None ws_completed_future.add_done_callback(lambda _: node.executor.wake()) self.assertEqual(