"""Cache fallback and mandatory database readiness, using the LB only.""" from pathlib import Path import subprocess import sys import time import urllib.error import urllib.request ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / 'tests')) from client import Client def compose(*args): subprocess.run(['docker', 'compose', *args], check=True, stdout=subprocess.DEVNULL) def main(): c = Client() actor, _, _, chat = c.bootstrap() sent = c.message(actor, chat, 'dependency-proof')['json'] try: compose('stop', 'redis') for _ in range(4): assert c.call('GET', '/api/v1/users/me', actor=actor, schema='User')['json'] == actor['user'] c.call('GET', '/health/ready', schema='Health') print('PASS: Redis outage falls back to PostgreSQL; session still checked.', flush=True) finally: compose('up', '-d', '--wait', 'redis') try: compose('stop', 'postgres') started = time.monotonic() for _ in range(30): try: urllib.request.urlopen(c.base + '/health/ready', timeout=3) except urllib.error.HTTPError as exc: assert exc.code == 503 break time.sleep(.2) else: raise AssertionError('Database outage did not affect readiness') print('PASS: database outage makes LB readiness unavailable in %.3f seconds.' % (time.monotonic() - started), flush=True) finally: compose('up', '-d', '--wait', 'postgres') subprocess.run([sys.executable, str(ROOT / 'scripts/wait_ready.py')], check=True) assert c.call('GET', '/api/v1/users/me', actor=actor, schema='User')['json'] == actor['user'] found = c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages', actor=actor, schema='MessagePage')['json']['items'] assert sent in found print('PASS: session and confirmed message preserved after database recovery.') if __name__ == '__main__': main()