diff --git a/collegamento/simple_client_server/new_impl.py b/collegamento/simple_client_server/new_impl.py index 8e8251c..7d57129 100644 --- a/collegamento/simple_client_server/new_impl.py +++ b/collegamento/simple_client_server/new_impl.py @@ -264,7 +264,7 @@ def create_message_id(self) -> int: def request( self, request_details: dict, - ) -> int | None: + ) -> None: """Sends the main process a request of type command with given kwargs - external API""" command = request_details["command"] @@ -289,9 +289,6 @@ def request( self.request_queue.put(final_request) - if self.commands[command][1]: - return id - def parse_response(self, res: Response) -> None: """Parses main process output and discards useless responses - internal API""" id = res["id"] @@ -320,6 +317,19 @@ def check_responses(self) -> None: while not self.response_queue.empty(): self.parse_response(self.response_queue.get()) + def get_response(self, command: str) -> list[Response]: + """Checks responses and returns the current response of type command if it has been returned - external API""" + if command not in self.commands: + raise CollegamentoError( + f"Cannot get response of command {command}, valid commands are {self.commands}" + ) + + self.check_responses() + response: list[Response] = self.newest_responses[command] + self.newest_responses[command] = [] + # TODO: if we know that the command doesn't allow multiple requests don't give a list + return response + def add_command( self, name: str, @@ -359,28 +369,42 @@ def __del__(self): # Mini tests Client({"foo": foo}) x = Client({"foo": (foo, True), "foo2": foo}) - id1 = x.request({"command": "foo"}) - id2 = x.request({"command": "foo"}) - id3 = x.request({"command": "foo2"}) - id4 = x.request( + + x.request({"command": "foo"}) + x.request({"command": "foo"}) + x.request({"command": "foo2"}) + x.request( {"command": "foo2"} - ) # If you see four "Foo called"'s, thats bad news bears + ) # If you see six "Foo called"'s, thats bad news bears x.add_command("foo3", foo) - id5 = x.request({"command": "foo3"}) + x.request({"command": "foo3"}) x.add_command("foo4", foo, True) - id6 = x.request({"command": "foo4"}) - assert id1 is not None - assert id2 is not None - assert id3 is None - assert id4 is None - assert id5 is None - assert id6 is not None + x.request({"command": "foo4"}) + x.request({"command": "foo4"}) + sleep(1) + + x.check_responses() # Not necessary, we're just checking that doing + # this first doesn't break get_response + + foo_r: list[Response] = x.get_response("foo") + foo_two_r: list[Response] = x.get_response("foo2") + foo_three_r: list[Response] = x.get_response("foo3") + foo_four_r: list[Response] = x.get_response("foo4") + + assert len(foo_r) == 2 + assert len(foo_two_r) == 1 + assert len(foo_three_r) == 1 + assert len(foo_four_r) == 2 + x.check_responses() x.create_server() + Client() + Client({"foo": foo}).request({"command": "foo"}) Client().kill_IPC() Client().create_server() + sleep(1) # from doctest import testmod diff --git a/tests/test_simple.py b/tests/test_simple.py index 8815696..4d3bba3 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -1,53 +1,52 @@ from time import sleep -from collegamento import ( - USER_FUNCTION, - Request, - Response, - SimpleClient, - SimpleServer, -) +from collegamento import Response, Client -def foo(server: "SimpleServer", bar: Request) -> bool: - if bar["command"] == "test": - return True - return False +def foo(server, request): + print("Foo called", request["id"]) def main(): - commands: dict[str, tuple[USER_FUNCTION, bool]] = { - "test": (foo, False), - "testMulti": (foo, True), - } - context = SimpleClient(commands) # type: ignore - - output = context.request({"command": "test"}) - assert output is None - id1 = context.request({"command": "testMulti"}) - id2 = context.request({"command": "testMulti"}) + Client({"foo": foo}) + x = Client({"foo": (foo, True), "foo2": foo}) + + x.request({"command": "foo"}) + x.request({"command": "foo"}) + x.request({"command": "foo2"}) + x.request( + {"command": "foo2"} + ) # If you see six "Foo called"'s, thats bad news bears + x.add_command("foo3", foo) + x.request({"command": "foo3"}) + x.add_command("foo4", foo, True) + x.request({"command": "foo4"}) + x.request({"command": "foo4"}) sleep(1) - output1: Response | None = context.get_response(id1) - output2: Response | None = context.get_response(id2) - assert output1 and output2 + x.check_responses() # Not necessary, we're just checking that doing + # this first doesn't break get_response - context.add_command("test2", foo) - context.add_command("testMulti2", foo, True) + foo_r: list[Response] = x.get_response("foo") + foo_two_r: list[Response] = x.get_response("foo2") + foo_three_r: list[Response] = x.get_response("foo3") + foo_four_r: list[Response] = x.get_response("foo4") - output = context.request({"command": "test2"}) - assert output is None - id2 = context.request({"command": "testMulti2"}) - id2 = context.request({"command": "testMulti2"}) + assert len(foo_r) == 2 + assert len(foo_two_r) == 1 + assert len(foo_three_r) == 1 + assert len(foo_four_r) == 2 - sleep(1) + x.check_responses() + x.create_server() - output1: Response | None = context.get_response(id1) - output2: Response | None = context.get_response(id2) - assert output1 and output2 + Client() + Client({"foo": foo}).request({"command": "foo"}) + Client().kill_IPC() + Client().create_server() - context.kill_IPC() + sleep(1) if __name__ == "__main__":