| 1 |
|
| 2 |
import logging |
| 3 |
import os |
| 4 |
import signal |
| 5 |
import subprocess |
| 6 |
import sys |
| 7 |
import time |
| 8 |
import unittest |
| 9 |
from urllib.parse import urlunsplit |
| 10 |
|
| 11 |
import psycopg2 |
| 12 |
import requests |
| 13 |
from django.conf import settings |
| 14 |
|
| 15 |
DJANGO_SETTINGS_MODULE = "integration_test.settings" |
| 16 |
os.environ.setdefault("DJANGO_SETTINGS_MODULE", DJANGO_SETTINGS_MODULE) |
| 17 |
|
| 18 |
logger = logging.getLogger(__name__) |
| 19 |
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) |
| 20 |
|
| 21 |
|
| 22 |
class Process: |
| 23 |
@staticmethod |
| 24 |
def _command(args): |
| 25 |
return list(args) |
| 26 |
|
| 27 |
@classmethod |
| 28 |
def run(cls, *args): |
| 29 |
subprocess.check_call(cls._command(args)) |
| 30 |
|
| 31 |
def __init__(self, *args): |
| 32 |
self.args = list(args) |
| 33 |
|
| 34 |
def start(self): |
| 35 |
self.process = subprocess.Popen(self._command(self.args), preexec_fn=os.setsid) |
| 36 |
logger.info(f"START PROCESS args:{self.args} pid:{self.process.pid}") |
| 37 |
time.sleep(1) |
| 38 |
|
| 39 |
def stop(self): |
| 40 |
# to be sure we kill all the children: |
| 41 |
os.killpg(self.process.pid, signal.SIGTERM) |
| 42 |
|
| 43 |
def __enter__(self): |
| 44 |
self.start() |
| 45 |
return self |
| 46 |
|
| 47 |
def __exit__(self, *args): |
| 48 |
self.stop() |
| 49 |
|
| 50 |
|
| 51 |
class DjangoCommand(Process): |
| 52 |
@staticmethod |
| 53 |
def _command(args): |
| 54 |
return ["./manage.py"] + list(args) + ["--settings", DJANGO_SETTINGS_MODULE] |
| 55 |
|
| 56 |
|
| 57 |
def terminate_all_postgres_connections(profile="default"): |
| 58 |
db_settings = settings.DATABASES[profile] |
| 59 |
conn_params = { |
| 60 |
'database': 'template1', |
| 61 |
'user': db_settings["USER"], |
| 62 |
'password': db_settings["PASSWORD"], |
| 63 |
'host': db_settings["HOST"], |
| 64 |
'port': db_settings["PORT"], |
| 65 |
} |
| 66 |
with psycopg2.connect(**conn_params) as conn: |
| 67 |
cursor = conn.cursor() |
| 68 |
cursor.execute( |
| 69 |
""" |
| 70 |
SELECT pg_terminate_backend(pg_stat_activity.pid) |
| 71 |
FROM pg_stat_activity |
| 72 |
WHERE pg_stat_activity.datname = %s |
| 73 |
""", |
| 74 |
(db_settings["NAME"],), |
| 75 |
) |
| 76 |
|
| 77 |
|
| 78 |
class IntegrationTest(unittest.TestCase): |
| 79 |
ADDRPORT = "127.0.0.1:8000" |
| 80 |
HOME_URL = urlunsplit(("http", ADDRPORT, "/", "", "")) |
| 81 |
|
| 82 |
def setUp(self): |
| 83 |
DjangoCommand.run("flush", "--noinput") |
| 84 |
# self.site = DjangoCommand("runserver", self.ADDRPORT) |
| 85 |
self.site = Process( |
| 86 |
"gunicorn", |
| 87 |
"-b", |
| 88 |
self.ADDRPORT, |
| 89 |
"--timeout", |
| 90 |
"600", # useful for worker debugging |
| 91 |
"integration_test.wsgi:application", |
| 92 |
) |
| 93 |
self.site.start() |
| 94 |
|
| 95 |
def tearDown(self): |
| 96 |
self.site.stop() |
| 97 |
|
| 98 |
def assertFailure(self): |
| 99 |
r = requests.get(self.HOME_URL) |
| 100 |
self.assertEqual(r.status_code, 500) |
| 101 |
|
| 102 |
def assertEntries(self, expected): |
| 103 |
r = requests.get(self.HOME_URL) |
| 104 |
self.assertEqual(r.status_code, 200) |
| 105 |
self.assertEqual(r.text, "Entries: {}".format(",".join(expected))) |
| 106 |
|
| 107 |
def enqueue(self, name): |
| 108 |
r = requests.post(self.HOME_URL, {"name": name}) |
| 109 |
self.assertEqual(r.status_code, 200) |
| 110 |
self.assertEqual(r.text, "Enqueued") |
| 111 |
|
| 112 |
def test_db_is_empty(self): |
| 113 |
self.assertEntries([]) |
| 114 |
|
| 115 |
def test_burst(self): |
| 116 |
self.enqueue("first") |
| 117 |
DjangoCommand.run("rqworker", "--burst") |
| 118 |
self.assertEntries(["first"]) |
| 119 |
|
| 120 |
def test_site_fails_and_the_reconnects(self): |
| 121 |
self.enqueue("first") |
| 122 |
DjangoCommand.run("rqworker", "--burst") |
| 123 |
|
| 124 |
terminate_all_postgres_connections() |
| 125 |
|
| 126 |
# the DB connection is gone, so the worker must first detect the problem: |
| 127 |
self.assertFailure() |
| 128 |
# now the gunicorn worker is ok again: |
| 129 |
self.assertEntries(["first"]) |
| 130 |
|
| 131 |
def test_worker_lost_connection(self): |
| 132 |
with DjangoCommand("rqworker"): |
| 133 |
self.enqueue("first") |
| 134 |
time.sleep(2) # wait for the worker to do the job |
| 135 |
self.assertEntries(["first"]) # job is done |
| 136 |
|
| 137 |
terminate_all_postgres_connections() |
| 138 |
|
| 139 |
self.enqueue("second") |
| 140 |
time.sleep(2) # wait for the worker to do the job |
| 141 |
|
| 142 |
self.assertFailure() # let the gunicorn worker reconnect |
| 143 |
self.assertEntries(["first", "second"]) # work is done |
| 144 |
|
| 145 |
|
| 146 |
if __name__ == '__main__': |
| 147 |
unittest.main() |