Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions ocs/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ class _AgentRunner:
i.e. '../agents/fake_data/fake_data_agent.py'
agent_name (str): Short, unique name for the agent
args (list): Additional CLI arguments to add when starting the Agent
kill_to_exit (bool): If True, will send a kill signal to exit instead
of interrupt.

"""

def __init__(self, agent_path, agent_name, args):
def __init__(self, agent_path, agent_name, args, kill_to_exit=False):
self.env = os.environ.copy()
self.env['COVERAGE_FILE'] = f'.coverage.agent.{agent_name}'
self.env['OCS_CONFIG_DIR'] = os.getcwd()
Expand All @@ -45,6 +47,7 @@ def __init__(self, agent_path, agent_name, args):
self.cmd.extend(args)
self.agent_name = agent_name
self.proc = None
self._kill_to_exit = kill_to_exit
self._timer = None
self._timedout = False

Expand Down Expand Up @@ -96,7 +99,11 @@ def shutdown(self):
"""
# don't send SIGINT if we've already sent SIGKILL
if not self._timedout:
self.proc.send_signal(signal.SIGINT)
if self._kill_to_exit:
sig = signal.SIGKILL
else:
sig = signal.SIGINT
self.proc.send_signal(sig)
self._timer.cancel()

try:
Expand All @@ -112,7 +119,7 @@ def shutdown(self):
raise RuntimeError('Agent timed out.')


def create_agent_runner_fixture(agent_path, agent_name, args=None, timeout=60):
def create_agent_runner_fixture(agent_path, agent_name, args=None, timeout=60, kill_to_exit=False):
"""Create a pytest fixture for running a given OCS Agent.

Parameters:
Expand All @@ -124,11 +131,13 @@ def create_agent_runner_fixture(agent_path, agent_name, args=None, timeout=60):
be interrupted. This typically indicates a crash within the agent.
This timeout should be longer than you expect the agent to run for
during a given test. Defaults to 60 seconds.
kill_to_exit (bool): If True, will send a kill signal to exit instead of
interrupt.

"""
@pytest.fixture()
def run_agent(cov):
runner = _AgentRunner(agent_path, agent_name, args)
runner = _AgentRunner(agent_path, agent_name, args, kill_to_exit=kill_to_exit)
runner.run(timeout=timeout)

yield
Expand Down
Loading