#!/usr/bin/env python3 """Verify the API admission limit while the worker is stopped.""" import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "tests")) from client import Client, arguments, check, run def main(): args = arguments("Backpressure admission-limit test") client = Client(args.base_url, args.ca_file) alice, bob, eve, chat = client.bootstrap() content = (ROOT / "tests" / "fixtures" / "sample.png").read_bytes() upload_path = ( "/api/v1/chats/" + chat["id"] + "/attachments?filename=backpressure.png" ) headers = {"Content-Type": "image/png"} accepted = [] print("Uploading 20 attachments...") for number in range(1, 21): result = client.call( "POST", upload_path, expected=202, actor=alice, raw=content, headers=headers, schema="Attachment", ) attachment = result["json"] check( attachment["status"] == "queued", "accepted attachment is not queued", ) accepted.append(attachment["id"]) print(f"Accepted {number}/20: {attachment['id']}") print("Sending the 21st attachment...") rejected = client.call( "POST", upload_path, expected=429, actor=alice, raw=content, headers=headers, ) check(rejected["status"] == 429, "expected HTTP 429") print("PASS: 20 attachments accepted; the 21st was rejected with HTTP 429.") print("Accepted attachment IDs:") for attachment_id in accepted: print(attachment_id) if __name__ == "__main__": run(main)