Dispatcharr/dispatcharr/tests/test_wsgi.py
Endoze 9b6c793b11
fix(wsgi): ensure gevent monkey-patching before Django import chain
Celery's Redis connection pool was silently failing in modular
deployment mode because Django's setup imports celery.py before uWSGI's
gevent plugin reliably patches stdlib sockets. Moving patch_all() to the
top of wsgi.py guarantees all broker connections are gevent-aware
regardless of uWSGI initialization order.
2026-02-26 10:23:50 -05:00

39 lines
1.3 KiB
Python

"""Tests for dispatcharr/wsgi.py gevent monkey-patching."""
import os
import subprocess
import sys
import unittest
class TestWSGIGeventPatching(unittest.TestCase):
"""Verify that wsgi.py applies gevent monkey-patching before Django imports.
These tests run in subprocesses because monkey.patch_all() must execute
before any other imports. By the time Django's test runner loads this
file, ssl/socket are already imported, so calling patch_all() in-process
would fail. A subprocess mirrors how uWSGI actually loads wsgi.py.
"""
def test_socket_is_patched_after_wsgi_import(self):
"""Loading wsgi.py first (as uWSGI does) should patch sockets."""
result = subprocess.run(
[
sys.executable, "-c",
"import dispatcharr.wsgi; "
"from gevent import monkey; "
"assert monkey.is_module_patched('socket'), "
"'socket module was not patched by wsgi.py'; "
"print('PASS')",
],
capture_output=True,
text=True,
env={**os.environ, "DJANGO_SETTINGS_MODULE": "dispatcharr.settings"},
timeout=60,
cwd="/app",
)
self.assertEqual(
result.returncode,
0,
f"monkey-patching check failed:\n{result.stderr}",
)