From 895e159502da174e5509a0f4c522c6d9dd7b4af5 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 19 Dec 2018 17:26:09 +0000 Subject: [PATCH] Add tests for successful and fatal exit codes --- example/etc/environments/development/Procfile | 3 ++- example/src/die_soon.py | 11 +++++++++++ example/src/exit_soon.py | 11 +++++++++++ tests/test_project/test_project.py | 9 +++++++-- tests/test_worker/test_server.py | 18 +++++++++++++++--- 5 files changed, 46 insertions(+), 6 deletions(-) create mode 100755 example/src/die_soon.py create mode 100755 example/src/exit_soon.py diff --git a/example/etc/environments/development/Procfile b/example/etc/environments/development/Procfile index 96d95c8..83f71c1 100644 --- a/example/etc/environments/development/Procfile +++ b/example/etc/environments/development/Procfile @@ -1,3 +1,4 @@ web: python -m http.server $PORT bg: python src/dummy_program.py -temp: python src/die_soon.py +die: python src/die_soon.py +exit: python src/exit_soon.py diff --git a/example/src/die_soon.py b/example/src/die_soon.py new file mode 100755 index 0000000..cc664bf --- /dev/null +++ b/example/src/die_soon.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python3 + +import time + +print("Started") # noqa: T001 + +time.sleep(1) + +print("Ended") # noqa: T001 + +exit(1) diff --git a/example/src/exit_soon.py b/example/src/exit_soon.py new file mode 100755 index 0000000..cc664bf --- /dev/null +++ b/example/src/exit_soon.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python3 + +import time + +print("Started") # noqa: T001 + +time.sleep(1) + +print("Ended") # noqa: T001 + +exit(1) diff --git a/tests/test_project/test_project.py b/tests/test_project/test_project.py index d47a385..53bd0a2 100644 --- a/tests/test_project/test_project.py +++ b/tests/test_project/test_project.py @@ -15,7 +15,7 @@ class ProjectTestCase(BaseTestCase): Project("/nonexistent") def test_read_processes(self): - self.assertEqual(len(self.project.processes), 3) + self.assertEqual(len(self.project.processes), 4) web_process = self.project.processes[0] self.assertEqual(web_process.name, "web") self.assertEqual(web_process.command, "python -m http.server $PORT") @@ -27,10 +27,15 @@ class ProjectTestCase(BaseTestCase): self.assertEqual(bg_process.project, self.project) bg_process = self.project.processes[2] - self.assertEqual(bg_process.name, "temp") + self.assertEqual(bg_process.name, "die") self.assertEqual(bg_process.command, "python src/die_soon.py") self.assertEqual(bg_process.project, self.project) + bg_process = self.project.processes[3] + self.assertEqual(bg_process.name, "exit") + self.assertEqual(bg_process.command, "python src/exit_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") diff --git a/tests/test_worker/test_server.py b/tests/test_worker/test_server.py index f6ce7ec..a86e99a 100644 --- a/tests/test_worker/test_server.py +++ b/tests/test_worker/test_server.py @@ -69,14 +69,26 @@ 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): + def test_process_restart_on_1_exit(self): response = send_to_server( - PayloadType.PROCESS, {"path": str(self.project.root), "process": "temp"} + PayloadType.PROCESS, {"path": str(self.project.root), "process": "die"} ) initial_pid = response["pid"] wait_for_process_terminate(initial_pid) time.sleep(2) - new_process = find_running_process_for(self.project.get_process("temp")) + new_process = find_running_process_for(self.project.get_process("die")) + 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_restart_on_0_exit(self): + response = send_to_server( + PayloadType.PROCESS, {"path": str(self.project.root), "process": "exit"} + ) + initial_pid = response["pid"] + wait_for_process_terminate(initial_pid) + time.sleep(2) + new_process = find_running_process_for(self.project.get_process("exit")) self.assertNotEqual(new_process.pid, initial_pid) self.assertFalse(is_process_running(initial_pid)) self.assertTrue(is_process_running(new_process.pid))