124 lines
4.5 KiB
Python
124 lines
4.5 KiB
Python
|
|
import unittest
|
||
|
|
from threading import Event
|
||
|
|
from unittest.mock import Mock, patch
|
||
|
|
|
||
|
|
from PyQt5 import QtCore
|
||
|
|
|
||
|
|
from ui.pose_query_worker import PoseQueryWorker, pb
|
||
|
|
|
||
|
|
|
||
|
|
class FakeChannel:
|
||
|
|
def __init__(self):
|
||
|
|
self.closed = Event()
|
||
|
|
|
||
|
|
def close(self):
|
||
|
|
self.closed.set()
|
||
|
|
|
||
|
|
|
||
|
|
class PoseQueryWorkerTests(unittest.TestCase):
|
||
|
|
def worker(self):
|
||
|
|
worker = PoseQueryWorker("unused:50052", "xiaozhao_left_arm", "base_link", "tcp-left", 3.0)
|
||
|
|
updates, errors = [], []
|
||
|
|
worker.updated.connect(updates.append, QtCore.Qt.DirectConnection)
|
||
|
|
worker.error.connect(errors.append, QtCore.Qt.DirectConnection)
|
||
|
|
return worker, updates, errors
|
||
|
|
|
||
|
|
def response(self):
|
||
|
|
response = pb.GetPose.Response()
|
||
|
|
response.header.success = True
|
||
|
|
response.pose.CopyFrom(pb.CartesianPose(x=0.1, y=0.2, z=0.3, rx=0.4, ry=0.5, rz=0.6))
|
||
|
|
return response
|
||
|
|
|
||
|
|
def run_query(self, response=None, error=None):
|
||
|
|
worker, updates, errors = self.worker()
|
||
|
|
channel = FakeChannel()
|
||
|
|
stub = Mock()
|
||
|
|
stub.getPose.return_value = response
|
||
|
|
stub.getPose.side_effect = error
|
||
|
|
with patch("ui.pose_query_worker.grpc.insecure_channel", return_value=channel), patch(
|
||
|
|
"ui.pose_query_worker.rpc.ArmServiceStub", return_value=stub
|
||
|
|
):
|
||
|
|
worker.start()
|
||
|
|
try:
|
||
|
|
self.assertTrue(worker.wait(1000))
|
||
|
|
finally:
|
||
|
|
worker.stop()
|
||
|
|
worker.wait(5000)
|
||
|
|
self.assertTrue(channel.closed.is_set())
|
||
|
|
return stub, updates, errors
|
||
|
|
|
||
|
|
def test_fetches_once_with_selected_device_links_and_timeout(self):
|
||
|
|
stub, updates, errors = self.run_query(self.response())
|
||
|
|
stub.getPose.assert_called_once()
|
||
|
|
request = stub.getPose.call_args.args[0]
|
||
|
|
self.assertEqual(request.header.device_id, "xiaozhao_left_arm")
|
||
|
|
self.assertGreater(request.header.timestamp.seconds, 0)
|
||
|
|
self.assertEqual((request.base_link, request.ee_link), ("base_link", "tcp-left"))
|
||
|
|
self.assertEqual(stub.getPose.call_args.kwargs["timeout"], 3.0)
|
||
|
|
self.assertEqual(updates, [{"x": 0.1, "y": 0.2, "z": 0.3, "rx": 0.4, "ry": 0.5, "rz": 0.6}])
|
||
|
|
self.assertFalse(errors)
|
||
|
|
|
||
|
|
def test_reports_rpc_and_server_errors_without_filling_a_pose(self):
|
||
|
|
rejected = self.response()
|
||
|
|
rejected.header.success = False
|
||
|
|
rejected.header.error_message = "unknown link"
|
||
|
|
for response, error, expected in (
|
||
|
|
(None, RuntimeError("timeout"), "timeout"),
|
||
|
|
(rejected, None, "unknown link"),
|
||
|
|
):
|
||
|
|
with self.subTest(expected=expected):
|
||
|
|
_, updates, errors = self.run_query(response, error)
|
||
|
|
self.assertFalse(updates)
|
||
|
|
self.assertEqual(len(errors), 1)
|
||
|
|
self.assertIn(expected, errors[0])
|
||
|
|
|
||
|
|
def test_rejects_missing_or_nonfinite_pose(self):
|
||
|
|
missing = pb.GetPose.Response()
|
||
|
|
missing.header.success = True
|
||
|
|
invalid = self.response()
|
||
|
|
invalid.pose.x = float("nan")
|
||
|
|
for response in (missing, invalid):
|
||
|
|
with self.subTest(response=response):
|
||
|
|
_, updates, errors = self.run_query(response)
|
||
|
|
self.assertFalse(updates)
|
||
|
|
self.assertEqual(len(errors), 1)
|
||
|
|
|
||
|
|
def test_cancellation_unblocks_rpc_and_discards_late_success(self):
|
||
|
|
worker, updates, errors = self.worker()
|
||
|
|
channel = FakeChannel()
|
||
|
|
entered = Event()
|
||
|
|
|
||
|
|
def get_pose(_request, timeout):
|
||
|
|
entered.set()
|
||
|
|
channel.closed.wait(5.0)
|
||
|
|
return self.response()
|
||
|
|
|
||
|
|
stub = Mock(getPose=Mock(side_effect=get_pose))
|
||
|
|
with patch("ui.pose_query_worker.grpc.insecure_channel", return_value=channel), patch(
|
||
|
|
"ui.pose_query_worker.rpc.ArmServiceStub", return_value=stub
|
||
|
|
):
|
||
|
|
worker.start()
|
||
|
|
try:
|
||
|
|
self.assertTrue(entered.wait(1.0))
|
||
|
|
worker.stop()
|
||
|
|
self.assertTrue(worker.wait(1000))
|
||
|
|
finally:
|
||
|
|
worker.stop()
|
||
|
|
worker.wait(5000)
|
||
|
|
self.assertFalse(updates)
|
||
|
|
self.assertFalse(errors)
|
||
|
|
self.assertTrue(channel.closed.is_set())
|
||
|
|
|
||
|
|
def test_stopped_before_start_does_not_connect(self):
|
||
|
|
worker, updates, errors = self.worker()
|
||
|
|
worker.stop()
|
||
|
|
with patch("ui.pose_query_worker.grpc.insecure_channel") as connect:
|
||
|
|
worker.run()
|
||
|
|
connect.assert_not_called()
|
||
|
|
self.assertFalse(updates)
|
||
|
|
self.assertFalse(errors)
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
unittest.main()
|