Skip to content

Commit

Permalink
Another test.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexhsamuel committed Jan 6, 2025
1 parent fcebe46 commit be220a9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
7 changes: 4 additions & 3 deletions test/int/procstar/ignore-term
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
from argparse import ArgumentParser
import signal
import time
import sys

parser = ArgumentParser()
parser.add_argument("sleep", metavar="SECS", type=float)
args = parser.parse_args()

def sigterm(signum, frame):
sig = signal.Signals(signum)
print(f"ignoring {sig.name}")
print(f"ignoring {sig.name}", file=sys.stderr)

signal.signal(signal.Signals.SIGTERM, sigterm)

print(f"sleeping for {args.sleep} sec")
print(f"sleeping for {args.sleep} sec", file=sys.stderr)
time.sleep(args.sleep)
print("done")
print("done", file=sys.stderr)

51 changes: 43 additions & 8 deletions test/int/procstar/test_stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@

#-------------------------------------------------------------------------------

IGNORE_TERM_PATH = Path(__file__).parent / "ignore-term"
SLEEP_JOB = jso_to_job({
"params": ["time"],
"program": {
"type": "procstar",
"argv": ["/usr/bin/sleep", "{{ time }}"],
}
}, "sleep")

JOB_ID = "ignore term"
JOB = jso_to_job({
IGNORE_TERM_PATH = Path(__file__).parent / "ignore-term"
IGNORE_TERM_JOB = jso_to_job({
"params": ["time"],
"program": {
"type": "procstar",
Expand All @@ -18,14 +24,36 @@
"grace_period": 2,
},
}
}, JOB_ID)
}, "ignore term")

def test_stop():
svc = ApsisService()
dump_job(svc.jobs_dir, SLEEP_JOB)
with svc, svc.agent():
# Schedule a 3 sec job but tell Apsis to stop it after 1 sec.
run_id = svc.client.schedule(
SLEEP_JOB.job_id, {"time": "3"},
stop_time="+1s",
)["run_id"]
res = svc.wait_run(run_id)

# The run was successfully stopped by Apsis, by sending it SIGTERM.
assert res["state"] == "success"
meta = res["meta"]["program"]
assert meta["status"]["signal"] == "SIGTERM"
assert meta["stop"]["signals"] == ["SIGTERM"]
assert meta["times"]["elapsed"] < 2


def test_dont_stop():
svc = ApsisService()
dump_job(svc.jobs_dir, JOB)
dump_job(svc.jobs_dir, IGNORE_TERM_JOB)
with svc, svc.agent():
# Schedule a 1 sec run but tell Apsis to stop it after 3 sec.
run_id = svc.client.schedule(JOB_ID, {"time": "1"}, stop_time="+3s")["run_id"]
run_id = svc.client.schedule(
IGNORE_TERM_JOB.job_id, {"time": "1"},
stop_time="+3s"
)["run_id"]
res = svc.wait_run(run_id)

assert res["state"] == "success"
Expand All @@ -37,12 +65,15 @@ def test_dont_stop():

def test_kill():
svc = ApsisService()
dump_job(svc.jobs_dir, JOB)
dump_job(svc.jobs_dir, IGNORE_TERM_JOB)
with svc, svc.agent():
# Schedule a 5 sec run but tell Apsis to stop it after 1 sec. The
# process ignores SIGTERM so Apsis will send SIGQUIT after the grace
# period.
run_id = svc.client.schedule(JOB_ID, {"time": "5"}, stop_time="+1s")["run_id"]
run_id = svc.client.schedule(
IGNORE_TERM_JOB.job_id, {"time": "5"},
stop_time="+1s"
)["run_id"]

time.sleep(1.5)
res = svc.client.get_run(run_id)
Expand All @@ -57,4 +88,8 @@ def test_kill():
assert meta["stop"]["signals"] == ["SIGTERM", "SIGKILL"]
assert meta["times"]["elapsed"] > 2.8

output = svc.client.get_output(run_id, "output").decode()
assert "ignoring SIGTERM" in output
assert "done" not in output


0 comments on commit be220a9

Please sign in to comment.