Skip to content

Commit

Permalink
Update Director (and Fake) to changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
BenediktBurger committed May 27, 2024
1 parent a32402a commit ad66400
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
39 changes: 32 additions & 7 deletions pyleco/directors/director.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,23 +177,48 @@ def call_action(self, action: str, *args, actor: Optional[Union[bytes, str]] = N
return self.ask_rpc("call_action", action=action, actor=actor, **params)

# Async methods: Just send, read later.
def send(self, actor: Optional[Union[bytes, str]] = None, data=None, **kwargs) -> bytes:
def send(
self,
actor: Optional[Union[bytes, str]] = None,
data=None,
additional_payload: Optional[Iterable[bytes]] = None,
**kwargs,
) -> bytes:
"""Send a request and return the conversation_id."""
actor = self._actor_check(actor)
cid0 = generate_conversation_id()
self.communicator.send(actor, conversation_id=cid0, data=data, **kwargs)
self.communicator.send(
actor, conversation_id=cid0, data=data, additional_payload=additional_payload, **kwargs
)
return cid0

def ask_rpc_async(self, method: str, actor: Optional[Union[bytes, str]] = None,
**kwargs) -> bytes:
def ask_rpc_async(
self,
method: str,
actor: Optional[Union[bytes, str]] = None,
additional_payload: Optional[Iterable[bytes]] = None,
**kwargs,
) -> bytes:
"""Send a rpc request, the response can be read later with :meth:`read_rpc_response`."""
string = self.generator.build_request_str(method=method, **kwargs)
return self.send(actor=actor, data=string, message_type=MessageTypes.JSON)
return self.send(
actor=actor,
data=string,
message_type=MessageTypes.JSON,
additional_payload=additional_payload,
)

def read_rpc_response(self, conversation_id: Optional[bytes] = None, **kwargs) -> Any:
def read_rpc_response(
self,
conversation_id: Optional[bytes] = None,
extract_additional_payload: bool = False,
**kwargs,
) -> Any:
"""Read the response value corresponding to a request with a certain `conversation_id`."""
response_message = self.communicator.read_message(conversation_id=conversation_id, **kwargs)
return self.communicator.interpret_rpc_response(response_message=response_message)
return self.communicator.interpret_rpc_response(
response_message=response_message, extract_additional_payload=extract_additional_payload
)

# Actor
def get_parameters_async(self, parameters: Union[str, Sequence[str]],
Expand Down
20 changes: 16 additions & 4 deletions pyleco/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#

from __future__ import annotations
from typing import Any, Optional, Sequence, Union
from typing import Any, Iterable, Optional, Sequence, Union

from .core.message import Message
from .core.internal_protocols import CommunicatorProtocol
Expand Down Expand Up @@ -219,14 +219,26 @@ def __init__(self, remote_class, **kwargs):
super().__init__(**kwargs)
self.remote_class = remote_class

def ask_rpc(self, method: str, actor: Optional[Union[bytes, str]] = None, **kwargs) -> Any:
def ask_rpc(
self,
method: str,
actor: Optional[Union[bytes, str]] = None,
additional_payload: Optional[Iterable[bytes]] = None,
extract_additional_payload: bool = False,
**kwargs,
) -> Any:
assert hasattr(self.remote_class, method), f"Remote class does not have method '{method}'."
self.method = method
self.kwargs = kwargs
return self.return_value

def ask_rpc_async(self, method: str, actor: Optional[Union[bytes, str]] = None,
**kwargs) -> bytes:
def ask_rpc_async(
self,
method: str,
actor: Optional[Union[bytes, str]] = None,
additional_payload: Optional[Iterable[bytes]] = None,
**kwargs,
) -> bytes:
assert hasattr(self.remote_class, method), f"Remote class does not have method '{method}'."
self.method = method
self.kwargs = kwargs
Expand Down
16 changes: 16 additions & 0 deletions tests/directors/test_director.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,22 @@ def test_read_rpc_response(director: Director):
assert director.read_rpc_response(conversation_id=cid) == 7.5


def test_read_binary_rpc_response(director: Director):
director.communicator._r = [ # type: ignore
Message(
"director",
"actor",
conversation_id=cid,
message_type=MessageTypes.JSON,
data={"id": 1, "result": None, "jsonrpc": "2.0"},
additional_payload=[b"123"],
)
]
assert director.read_rpc_response(conversation_id=cid, extract_additional_payload=True) == [
b"123"
]


def test_get_properties_async(director: Director):
properties = ["a", "some"]
cid = director.get_parameters_async(parameters=properties)
Expand Down

0 comments on commit ad66400

Please sign in to comment.