|
| 1 | +import multiprocessing |
| 2 | +import queue |
| 3 | +import signal |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +import threading |
| 7 | +import time |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +BROKER_MODULE = """ |
| 13 | +import asyncio |
| 14 | +from collections.abc import AsyncGenerator |
| 15 | +
|
| 16 | +from taskiq import AsyncBroker, BrokerMessage |
| 17 | +
|
| 18 | +
|
| 19 | +class IdleBroker(AsyncBroker): |
| 20 | + async def kick(self, message: BrokerMessage) -> None: |
| 21 | + pass |
| 22 | +
|
| 23 | + async def listen(self) -> AsyncGenerator[bytes, None]: |
| 24 | + while True: |
| 25 | + await asyncio.sleep(3600) |
| 26 | + yield b"" |
| 27 | +
|
| 28 | +
|
| 29 | +broker = IdleBroker() |
| 30 | +""" |
| 31 | + |
| 32 | +# Runs the taskiq CLI, optionally forcing a multiprocessing start method first. |
| 33 | +CLI_RUNNER = """ |
| 34 | +import multiprocessing |
| 35 | +import sys |
| 36 | +
|
| 37 | +from taskiq.__main__ import main |
| 38 | +
|
| 39 | +if __name__ == "__main__": |
| 40 | + start_method = sys.argv.pop(1) |
| 41 | + if start_method != "default": |
| 42 | + multiprocessing.set_start_method(start_method) |
| 43 | + main() |
| 44 | +""" |
| 45 | + |
| 46 | +STARTUP_TIMEOUT = 30 |
| 47 | + |
| 48 | + |
| 49 | +def worker_start_methods() -> list[str]: |
| 50 | + # The worker always uses "spawn" on macOS: forcing another method would conflict. |
| 51 | + if sys.platform == "darwin": |
| 52 | + return ["default"] |
| 53 | + # The default method is one of these, so it's covered without a separate run. |
| 54 | + return multiprocessing.get_all_start_methods() |
| 55 | + |
| 56 | + |
| 57 | +def wait_for_output(process: subprocess.Popen[str], text: str) -> bool: |
| 58 | + lines: queue.Queue[str] = queue.Queue() |
| 59 | + |
| 60 | + def read_output() -> None: |
| 61 | + assert process.stdout is not None |
| 62 | + for line in process.stdout: |
| 63 | + lines.put(line) |
| 64 | + |
| 65 | + threading.Thread(target=read_output, daemon=True).start() |
| 66 | + deadline = time.monotonic() + STARTUP_TIMEOUT |
| 67 | + while (remaining := deadline - time.monotonic()) > 0: |
| 68 | + try: |
| 69 | + if text in lines.get(timeout=remaining): |
| 70 | + return True |
| 71 | + except queue.Empty: |
| 72 | + return False |
| 73 | + return False |
| 74 | + |
| 75 | + |
| 76 | +def stop(process: subprocess.Popen[str]) -> None: |
| 77 | + process.send_signal(signal.SIGINT) |
| 78 | + try: |
| 79 | + process.wait(timeout=10) |
| 80 | + except subprocess.TimeoutExpired: |
| 81 | + process.kill() |
| 82 | + process.wait() |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.skipif( |
| 86 | + sys.platform == "win32", |
| 87 | + reason="Worker processes can't be stopped with SIGINT on Windows.", |
| 88 | +) |
| 89 | +@pytest.mark.parametrize("start_method", worker_start_methods()) |
| 90 | +def test_worker_process_logs_listening_started( |
| 91 | + tmp_path: Path, |
| 92 | + start_method: str, |
| 93 | +) -> None: |
| 94 | + """ |
| 95 | + Worker processes emit their own log records. |
| 96 | +
|
| 97 | + A worker started with "spawn" or "forkserver" (the default on Linux |
| 98 | + since Python 3.14) doesn't inherit the main process logging config. |
| 99 | + """ |
| 100 | + (tmp_path / "idle_broker.py").write_text(BROKER_MODULE) |
| 101 | + (tmp_path / "run_cli.py").write_text(CLI_RUNNER) |
| 102 | + process = subprocess.Popen( # noqa: S603 |
| 103 | + [ |
| 104 | + sys.executable, |
| 105 | + "run_cli.py", |
| 106 | + start_method, |
| 107 | + "worker", |
| 108 | + "idle_broker:broker", |
| 109 | + "--workers", |
| 110 | + "1", |
| 111 | + "--log-level", |
| 112 | + "INFO", |
| 113 | + ], |
| 114 | + cwd=tmp_path, |
| 115 | + stdout=subprocess.PIPE, |
| 116 | + stderr=subprocess.STDOUT, |
| 117 | + text=True, |
| 118 | + ) |
| 119 | + try: |
| 120 | + assert wait_for_output(process, "Listening started.") |
| 121 | + finally: |
| 122 | + stop(process) |
0 commit comments