Skip to content

Commit

Permalink
Fix mypy errors in rosbridge_server tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bjsowa committed Dec 17, 2024
1 parent 2d2ccb7 commit 708dfbf
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 8 deletions.
7 changes: 7 additions & 0 deletions rosbridge_server/test/websocket/advertise_action.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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]))
Expand Down
1 change: 1 addition & 0 deletions rosbridge_server/test/websocket/advertise_service.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions rosbridge_server/test/websocket/call_service.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
17 changes: 10 additions & 7 deletions rosbridge_server/test/websocket/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())]
)

Expand All @@ -97,14 +99,15 @@ 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():
TCP4ClientEndpoint(reactor, "127.0.0.1", port).connect(factory).addCallback(
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())
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions rosbridge_server/test/websocket/send_action_goal.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions rosbridge_server/test/websocket/smoke.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 708dfbf

Please sign in to comment.