Skip to content

Commit 558d02c

Browse files
committed
Allow joint state and command streams' mocking
Signed-off-by: Michel Hidalgo <[email protected]>
1 parent 800b1f7 commit 558d02c

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

spot_wrapper/testing/grpc.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import grpc
1515
from bosdyn.api.header_pb2 import CommonError
1616

17-
from spot_wrapper.testing.helpers import ForwardingWrapper
17+
from spot_wrapper.testing.helpers import ForwardingWrapper, cache1
1818

1919

2020
def implemented(function: typing.Callable) -> bool:
@@ -273,9 +273,9 @@ class AutoCompletingStreamUnaryRpcHandler(ForwardingWrapper):
273273
"""
274274

275275
def __call__(self, request_iterator: typing.Iterator, context: grpc.ServicerContext) -> typing.Any:
276-
*head_requests, tail_request = request_iterator
277-
response = self.__wrapped__(iter([*head_requests, tail_request]), context)
278-
fill_response_header(tail_request, response)
276+
cached_request_iterator = cache1(request_iterator)
277+
response = self.__wrapped__(cached_request_iterator, context)
278+
fill_response_header(cached_request_iterator.cache, response)
279279
return response
280280

281281

@@ -295,9 +295,9 @@ class AutoCompletingStreamStreamRpcHandler(ForwardingWrapper):
295295
"""
296296

297297
def __call__(self, request_iterator: typing.Iterator, context: grpc.ServicerContext) -> typing.Iterator:
298-
*head_requests, tail_request = request_iterator
299-
for response in self.__wrapped__(iter([*head_requests, tail_request]), context):
300-
fill_response_header(tail_request, response)
298+
cached_request_iterator = cache1(request_iterator)
299+
for response in self.__wrapped__(cached_request_iterator, context):
300+
fill_response_header(cached_request_iterator.cache, response)
301301
yield response
302302

303303

spot_wrapper/testing/helpers.py

+23
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,26 @@ def walk_resource_tree(resource_tree: ResourceTree) -> typing.Iterable[ResourceT
3939
yield resource_tree
4040
for subtree in resource_tree.sub_resources:
4141
yield from walk_resource_tree(subtree)
42+
43+
44+
_T = typing.TypeVar("_T")
45+
46+
47+
class cache1(typing.Iterator[_T]):
48+
"""Iterator wrapper that caches the last item retrieved. """
49+
50+
def __init__(self, inner: typing.Iterator[_T]):
51+
"""Initialize cached iterator
52+
53+
Args:
54+
inner: inner iterator to cache
55+
"""
56+
self.__inner = inner
57+
self.cache: typing.Optional[_T] = None
58+
59+
def __iter__(self) -> "cache1":
60+
return self
61+
62+
def __next__(self) -> _T:
63+
self.cache = next(self.__inner)
64+
return self.cache

spot_wrapper/testing/mocks/__init__.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,18 @@
5959
from bosdyn.api.power_service_pb2_grpc import (
6060
PowerServiceServicer as PowerCommandServiceServicer,
6161
)
62-
from bosdyn.api.robot_command_service_pb2_grpc import RobotCommandServiceServicer
62+
from bosdyn.api.robot_command_service_pb2_grpc import (
63+
RobotCommandServiceServicer,
64+
RobotCommandStreamingService,
65+
)
6366
from bosdyn.api.robot_id_service_pb2_grpc import RobotIdServiceServicer
64-
from bosdyn.api.robot_state_service_pb2_grpc import RobotStateServiceServicer
67+
from bosdyn.api.robot_state_service_pb2_grpc import (
68+
RobotStateServiceServicer,
69+
RobotStateStreamingService,
70+
)
6571
from bosdyn.api.spot.choreography_service_pb2_grpc import ChoreographyServiceServicer
6672
from bosdyn.api.spot.door_service_pb2_grpc import DoorServiceServicer
67-
from bosdyn.api.spot.inverse_kinematics_service_pb2_grpc import (
68-
InverseKinematicsServiceServicer,
69-
)
73+
from bosdyn.api.spot.inverse_kinematics_service_pb2_grpc import InverseKinematicsServiceServicer
7074
from bosdyn.api.spot.spot_check_service_pb2_grpc import SpotCheckServiceServicer
7175
from bosdyn.api.spot_cam.service_pb2_grpc import (
7276
AudioServiceServicer,
@@ -145,8 +149,10 @@ class BaseMockSpot(
145149
PtzServiceServicer,
146150
RemoteMissionServiceServicer,
147151
RobotCommandServiceServicer,
152+
RobotCommandStreamingService,
148153
RobotIdServiceServicer,
149154
RobotStateServiceServicer,
155+
RobotStateStreamingService,
150156
SpotCheckServiceServicer,
151157
StreamQualityServiceServicer,
152158
TimeSyncServiceServicer,

0 commit comments

Comments
 (0)