async def test_json_actor_command_write(json_actor, json_client): command = Command( command_string="ping", actor=json_actor, ) command.set_status("RUNNING") command.write("i", text="Pong") assert len(command.replies) == 2 assert command.replies[0].message_code == ">"
async def test_write_exception(actor): command = Command( command_string="ping", actor=actor, ) command.set_status("RUNNING") command.write("e", error=ValueError("Error message")) assert len(command.replies) == 2 assert command.replies[1].message_code == "e" assert command.replies[1].message["error"] == "Error message"
async def test_send_command_from_command(amqp_actor, mocker): send_command_mock = mocker.patch.object(amqp_actor.connection.exchange, "publish") command = Command( command_string="", commander_id="APO.Jose", command_id=5, actor=amqp_actor, ) await command.send_command("otheractor", "command1 --option") send_command_mock.assert_called()
async def test_write_exception(amqp_actor): command = Command( command_string="ping", actor=amqp_actor, ) command.set_status("RUNNING") command.write("e", error=ValueError("Error message")) assert len(command.replies) == 2 assert command.replies[1].message_code == "e" assert command.replies.get("error") == { "exception_module": "builtins", "exception_type": "ValueError", "exception_message": "Error message", }
async def test_send_command_from_command(actor, mocker): send_command_mock = mocker.patch.object(actor.tron, "send_command") command = Command( command_string="", commander_id="APO.Jose", command_id=5, actor=actor, ) command.send_command("otheractor", "command1 --option") send_command_mock.assert_called_once_with( "otheractor", "command1 --option", commander="APO.Jose.otheractor", mid=None, callback=None, time_limit=None, )
def send_command( self, target, command_string, *args, commander=None, mid=None, callback: Optional[Callable[[Reply], None]] = None, time_limit: Optional[float] = None, ): """Sends a command through the hub. Parameters ---------- target The actor to command. command_string The command to send. args Arguments to concatenate to the command string. commander The actor or client sending the command. The format for Tron is "commander message_id target command" where commander needs to start with a letter and have a program and a user joined by a dot. Otherwise the command will be accepted but the reply will fail to parse. If ``commander=None``, the instance ``commander`` value will be used. mid The message id. If `None`, a sequentially increasing value will be used. You should not specify a ``mid`` unless you really know what you're doing. callback A callback to invoke with each reply received from the actor. time_limit A delay after which the command is marked as timed out and done. Examples -------- These two are equivalent :: >>> tron.send_command('my_actor', 'do_something --now') >>> tron.send_command('my_actor', 'do_something', '--now') """ assert self.transport mid = mid or self._mid # The mid must be a 32-bit unsigned number. if mid >= 2**32: self._mid = mid = mid % 2**32 if len(args) > 0: command_string += " " + " ".join(map(str, args)) commander = commander or self.commander command_string = f"{commander} {mid} {target} {command_string}\n" command = Command( command_string=command_string, reply_callback=callback, time_limit=time_limit, ) self.running_commands[mid] = command self.transport.write(command_string.encode()) self._mid += 1 return command