archive
/
catfish
Archived
1
Fork 0

Test process restarts when exit normally

This commit is contained in:
Jake Howard 2018-12-19 17:10:58 +00:00
parent fffcbf8f48
commit 711db92b8e
Signed by: jake
GPG Key ID: 57AFB45680EDD477
5 changed files with 22 additions and 2 deletions

View File

@ -61,7 +61,8 @@ async def publish_stdout_for(process, ctf_process: Process, project: Project):
sock.send_string(output.decode())
await process.wait()
exit_code = process.returncode
if exit_code == -signal.SIGHUP:
if exit_code in [-signal.SIGHUP, 0, 1]:
# If process gets SIGHUP, or exits cleanly / uncleanly, restart it
process = await start_process(project, ctf_process)
finally:
sock.close()

View File

@ -1,2 +1,3 @@
web: python -m http.server $PORT
bg: python src/dummy_program.py
temp: python src/die_soon.py

View File

@ -3,6 +3,7 @@
import time
from itertools import count
for num in count():
time.sleep(0.5)
print("Round {}".format(num)) # noqa: T001

View File

@ -15,7 +15,7 @@ class ProjectTestCase(BaseTestCase):
Project("/nonexistent")
def test_read_processes(self):
self.assertEqual(len(self.project.processes), 2)
self.assertEqual(len(self.project.processes), 3)
web_process = self.project.processes[0]
self.assertEqual(web_process.name, "web")
self.assertEqual(web_process.command, "python -m http.server $PORT")
@ -26,6 +26,11 @@ class ProjectTestCase(BaseTestCase):
self.assertEqual(bg_process.command, "python src/dummy_program.py")
self.assertEqual(bg_process.project, self.project)
bg_process = self.project.processes[2]
self.assertEqual(bg_process.name, "temp")
self.assertEqual(bg_process.command, "python src/die_soon.py")
self.assertEqual(bg_process.project, self.project)
def test_get_process(self):
self.assertEqual(self.project.get_process("web").name, "web")
self.assertEqual(self.project.get_process("bg").name, "bg")

View File

@ -69,6 +69,18 @@ class ProcessWorkerTestCase(BaseWorkerTestCase):
self.assertFalse(is_process_running(initial_pid))
self.assertTrue(is_process_running(new_process.pid))
def test_process_restart_on_0_exit(self):
response = send_to_server(
PayloadType.PROCESS, {"path": str(self.project.root), "process": "temp"}
)
initial_pid = response["pid"]
wait_for_process_terminate(initial_pid)
time.sleep(2)
new_process = find_running_process_for(self.project.get_process("temp"))
self.assertNotEqual(new_process.pid, initial_pid)
self.assertFalse(is_process_running(initial_pid))
self.assertTrue(is_process_running(new_process.pid))
def test_process_terminate(self):
response = send_to_server(
PayloadType.PROCESS,