Skip to content

Commit

Permalink
Manually call os.kill to interrupt
Browse files Browse the repository at this point in the history
This works around an issue with `subprocess` on Windows.
  • Loading branch information
RealOrangeOne committed Sep 10, 2024
1 parent 49ce403 commit a935797
Showing 1 changed file with 4 additions and 15 deletions.
19 changes: 4 additions & 15 deletions tests/tests/test_database_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1199,15 +1199,6 @@ def start_worker(
self.processes.append(p)
return p

@staticmethod
def _get_ctrl_c_signal() -> int:
"""
Windows uses a different signal for CTRL-C than Unix.
"""
if hasattr(signal, "CTRL_C_EVENT"):
return cast(int, signal.CTRL_C_EVENT)
return signal.SIGINT

def test_run_subprocess(self) -> None:
result = test_tasks.noop_task.enqueue()
process = self.start_worker(["--batch"])
Expand All @@ -1232,7 +1223,7 @@ def test_interrupt_no_tasks(self) -> None:

def test_interrupt_signals(self) -> None:
for sig in [
self._get_ctrl_c_signal(),
signal.SIGINT, # ctrl-c
signal.SIGTERM,
]:
with self.subTest(sig):
Expand All @@ -1246,7 +1237,7 @@ def test_interrupt_signals(self) -> None:
result.refresh()
self.assertEqual(result.status, ResultStatus.RUNNING)

process.send_signal(sig)
os.kill(process.pid, sig)

process.wait(timeout=1)

Expand All @@ -1257,8 +1248,6 @@ def test_interrupt_signals(self) -> None:
self.assertEqual(result.status, ResultStatus.COMPLETE)

def test_repeat_ctrl_c(self) -> None:
ctrl_c_signal = self._get_ctrl_c_signal()

result = test_tasks.hang.enqueue()

process = self.start_worker()
Expand All @@ -1269,15 +1258,15 @@ def test_repeat_ctrl_c(self) -> None:
result.refresh()
self.assertEqual(result.status, ResultStatus.RUNNING)

process.send_signal(ctrl_c_signal)
os.kill(process.pid, signal.SIGINT)

time.sleep(0.5)

self.assertIsNone(process.poll())
result.refresh()
self.assertEqual(result.status, ResultStatus.RUNNING)

process.send_signal(ctrl_c_signal)
os.kill(process.pid, signal.SIGINT)

process.wait(timeout=2)

Expand Down

0 comments on commit a935797

Please sign in to comment.