def test_send_control_c_group(): """Test send control c to process group.""" # Can't test process group id kill directly, # because o/w pytest would be stopped. process = Popen(["sleep", "1"]) # nosec pgid = os.getpgid(process.pid) time.sleep(0.1) with patch("os.killpg") as mock_killpg: send_control_c(process, kill_group=True) process.communicate(timeout=3) mock_killpg.assert_called_with(pgid, signal.SIGINT)
def test_interact(self): """Test the 'aea interact' command with the echo skill.""" self.add_item("skill", "fetchai/echo:0.5.0") self.run_agent() process = self.run_interaction() time.sleep(1.0) # send first message process.stdin.write(b"hello\n") process.stdin.flush() time.sleep(3.0) # read incoming messages process.stdin.write(b"\n") process.stdin.flush() time.sleep(1.0) # read another message - should return nothing process.stdin.write(b"\n") process.stdin.flush() time.sleep(1.0) send_control_c(process) time.sleep(0.5) expected_output = [ "Starting AEA interaction channel...", "Provide message of protocol fetchai/default:0.4.0 for performative bytes:", "Sending envelope:", f"to: {self.agent_name}", f"sender: {self.agent_name}_interact", "protocol_id: fetchai/default:0.4.0", "message_id=1 target=0 performative=bytes content=b'hello')", "Provide message of protocol fetchai/default:0.4.0 for performative bytes:", "Interrupting input, checking inbox ...", "Received envelope:", f"to: {self.agent_name}_interact", f"sender: {self.agent_name}", "protocol_id: fetchai/default:0.4.0", "message_id=2 target=1 performative=bytes content=b'hello')", "Provide message of protocol fetchai/default:0.4.0 for performative bytes:", "Interrupting input, checking inbox ...", "Received no new envelope!", "Provide message of protocol fetchai/default:0.4.0 for performative bytes:", "Interaction interrupted!", ] missing = self.missing_from_output(process, expected_output, timeout=10, is_terminating=False) assert len( missing) == 0, "Strings {} didn't appear in agent output.".format( missing)
def test_send_control_c(): """Test send control c to process.""" # Can't test process group id kill directly, # because o/w pytest would be stopped. process = Popen( # nosec ["timeout" if platform.system() == "Windows" else "sleep", "5"], **win_popen_kwargs()) time.sleep(0.001) send_control_c(process) process.communicate(timeout=3) assert process.returncode != 0
def test_send_control_c_windows(): """Test send control c on Windows.""" process = Popen( # nosec ["timeout" if platform.system() == "Windows" else "sleep", "5"]) time.sleep(0.001) pid = process.pid with patch("aea.helpers.base.signal") as mock_signal: mock_signal.CTRL_C_EVENT = "mock" with patch("platform.system", return_value="Windows"): with patch("os.kill") as mock_kill: send_control_c(process) mock_kill.assert_called_with(pid, mock_signal.CTRL_C_EVENT)
def terminate_agents( cls, *subprocesses: subprocess.Popen, timeout: int = 20, ) -> None: """ Terminate agent subprocesses. Run from agent's directory. :param subprocesses: the subprocesses running the agents :param timeout: the timeout for interruption """ if not subprocesses: subprocesses = tuple(cls.subprocesses) for process in subprocesses: process.poll() if process.returncode is None: # stop only pending processes send_control_c(process) for process in subprocesses: process.wait(timeout=timeout)
def test_interact(self): """Test the 'aea interact' command with the echo skill.""" self.add_item("skill", str(ECHO_SKILL_PUBLIC_ID)) self.run_agent() process = self.run_interaction() assert not self.missing_from_output( process, [ "Starting AEA interaction channel...", f"Provide message of protocol '{str(DefaultMessage.protocol_id)}' for performative bytes", ], timeout=10, is_terminating=False, ) # send first message process.stdin.write(b"hello\n") process.stdin.flush() assert not self.missing_from_output( process, [ "Sending envelope:", f"to: {self.agent_name}", f"sender: {self.agent_name}_interact", f"protocol_id: {str(DefaultMessage.protocol_id)}", "message_id=1", "target=0", "performative=bytes", "content=b'hello'", f"Provide message of protocol '{str(DefaultMessage.protocol_id)}' for performative bytes:", ], timeout=10, is_terminating=False, ) # read incoming messages process.stdin.write(b"\n") process.stdin.flush() assert not self.missing_from_output( process, [ "Interrupting input, checking inbox ...", "Received envelope:", f"to: {self.agent_name}_interact", f"sender: {self.agent_name}", f"protocol_id: {str(DefaultMessage.protocol_id)}", "message_id=2", "target=1", "performative=bytes", "content=b'hello'", f"Provide message of protocol '{str(DefaultMessage.protocol_id)}' for performative bytes:", ], timeout=10, is_terminating=False, ) # read another message - should return nothing process.stdin.write(b"\n") process.stdin.flush() assert not self.missing_from_output( process, [ "Interrupting input, checking inbox ...", "Received no new envelope!", f"Provide message of protocol '{str(DefaultMessage.protocol_id)}' for performative bytes:", ], timeout=10, is_terminating=False, ) send_control_c(process) assert not self.missing_from_output(process, ["Interaction interrupted!"], timeout=10, is_terminating=False)
def control_c(self) -> None: """Send control c to process started.""" time.sleep(0.1) # sometimes it's better to wait a bit send_control_c(self.proc, True)