|
| 1 | +import os |
| 2 | +from tempfile import TemporaryDirectory |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | +from django.test import SimpleTestCase |
| 6 | + |
| 7 | +import steady_queue |
| 8 | +from steady_queue.processes.pidfile import Pidfile |
| 9 | + |
| 10 | + |
| 11 | +class PidfileDefaultsTestCase(SimpleTestCase): |
| 12 | + def test_supervisor_pidfile_defaults_to_none(self): |
| 13 | + self.assertIsNone(steady_queue.supervisor_pidfile) |
| 14 | + |
| 15 | + |
| 16 | +class PidfileLifecycleTestCase(SimpleTestCase): |
| 17 | + def test_setup_registers_atexit_cleanup_hook(self): |
| 18 | + with TemporaryDirectory() as temp_dir, patch("atexit.register") as register: |
| 19 | + path = os.path.join(temp_dir, "pids", "steady_queue_supervisor.pid") |
| 20 | + |
| 21 | + pidfile = Pidfile(path) |
| 22 | + pidfile.setup() |
| 23 | + |
| 24 | + register.assert_called_once() |
| 25 | + self.assertTrue(os.path.exists(path)) |
| 26 | + with open(path) as f: |
| 27 | + self.assertEqual(str(os.getpid()), f.read().strip()) |
| 28 | + |
| 29 | + def test_setup_replaces_stale_pidfile_for_dead_process(self): |
| 30 | + with TemporaryDirectory() as temp_dir: |
| 31 | + path = os.path.join(temp_dir, "pids", "steady_queue_supervisor.pid") |
| 32 | + |
| 33 | + os.makedirs(os.path.dirname(path), exist_ok=True) |
| 34 | + with open(path, "w") as f: |
| 35 | + f.write("999999") |
| 36 | + |
| 37 | + with patch("atexit.register"): |
| 38 | + pidfile = Pidfile(path) |
| 39 | + pidfile.setup() |
| 40 | + |
| 41 | + with open(path) as f: |
| 42 | + self.assertEqual(str(os.getpid()), f.read().strip()) |
0 commit comments