import argparse import json import time from concurrent.futures import ThreadPoolExecutor from datetime import UTC, datetime from pathlib import Path from uuid import uuid4 import httpx from scripts.stand import actor, auth, command, extra_api, sql def main(): parser = argparse.ArgumentParser() parser.add_argument("--output", required=True) args = parser.parse_args() output = Path(args.output) if output.exists(): raise SystemExit("Choose new output path") name, base = extra_api(pool_size=1) function = "poolwait_" + uuid4().hex[:8] trigger_created = False try: with httpx.Client( base_url=base, trust_env=False, timeout=15 ) as client: user, _, session = actor(client) headers = auth(session) chat = client.post( "/api/v1/chats", headers=headers, json={"title": "Pool test", "member_ids": [user["id"]]}, ).json() path = "/api/v1/chats/" + chat["id"] + "/messages" sql( "CREATE FUNCTION " + function + "() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF NEW.chat_id='" + chat["id"] + "'::uuid THEN PERFORM pg_sleep(4); END IF; RETURN NEW; END $$; CREATE TRIGGER " + function + " BEFORE INSERT ON messages FOR EACH ROW EXECUTE FUNCTION " + function + "()" ) trigger_created = True with ThreadPoolExecutor(max_workers=1) as pool: blocking = pool.submit( client.post, path, headers=headers, json={"text": "Synthetic delayed insert"}, ) for _ in range(30): active = sql( "SELECT count(*) FROM pg_stat_activity WHERE datname=current_database() AND state='active' AND query LIKE 'INSERT INTO messages%'" ) if int(active) > 0: break time.sleep(0.05) else: raise AssertionError("Blocked INSERT not observed") started = time.perf_counter() second = client.get("/api/v1/users/me", headers=headers) waited = time.perf_counter() - started assert second.status_code == 503 and 0.8 <= waited < 3 first = blocking.result(timeout=6) assert first.status_code == 503 sql( "DROP TRIGGER " + function + " ON messages; DROP FUNCTION " + function + "()" ) trigger_created = False assert client.get(path, headers=headers).json()["items"] == [] assert ( client.post( path, headers=headers, json={"text": "After recovery"} ).status_code == 201 ) result = { "date": datetime.now(UTC).isoformat(), "commit": command(["git", "rev-parse", "HEAD"]), "command": "python -m scripts.verify_pool", "pool_size": 1, "blocked_insert_observed_in_pg_stat_activity": True, "pool_wait_seconds": waited, "pool_timeout_status": second.status_code, "statement_timeout_status": first.status_code, "failed_write_rolled_back": True, "write_after_recovery": 201, "result": "PASS", } output.parent.mkdir(parents=True, exist_ok=True) output.write_text( json.dumps(result, ensure_ascii=False, indent=2) + "\n" ) print(json.dumps(result, ensure_ascii=False, indent=2)) finally: if trigger_created: sql( "DROP TRIGGER IF EXISTS " + function + " ON messages; DROP FUNCTION IF EXISTS " + function + "()" ) command(["docker", "rm", "-f", name]) if __name__ == "__main__": main()