#!/usr/bin/env python3 # The public smoke checks sequential replay; # this test checks the cross-replica race. import argparse import sys import threading import uuid from pathlib import Path ROOT = Path(__file__).resolve().parents[2] sys.path.insert(0, str(ROOT / "tests")) from client import Client, Failure, check # noqa: E402 def main(): parser = argparse.ArgumentParser( description="Concurrent idempotency check through the load balancer" ) parser.add_argument("--base-url", default="http://localhost:8080") parser.add_argument("--ca-file", default=None) parser.add_argument("--requests", type=int, default=12) args = parser.parse_args() setup = Client(args.base_url, args.ca_file) alice, _, _, chat = setup.bootstrap() key = "concurrent-" + uuid.uuid4().hex body = {"text": "one concurrent message"} path = f"/api/v1/chats/{chat['id']}/messages" results = [] lock = threading.Lock() def worker(): client = Client(args.base_url, args.ca_file) try: response = client.call( "POST", path, 201, actor=alice, body=body, headers={"Idempotency-Key": key}, schema="Message", ) with lock: results.append( (response["json"], response["headers"].get("X-Instance-Id")) ) except Failure as error: with lock: results.append((error, None)) threads = [threading.Thread(target=worker) for _ in range(args.requests)] for thread in threads: thread.start() for thread in threads: thread.join() check(len(results) == args.requests, "not every concurrent request finished") check( all(isinstance(message, dict) for message, _ in results), "a concurrent request failed", ) ids = {message["id"] for message, _ in results} check(len(ids) == 1, "concurrent requests returned different message IDs") instances = {instance for _, instance in results if instance} check( len(instances) >= 2, "expected the load balancer to use at least two replicas" ) page = setup.call("GET", path + "?limit=100", actor=alice, schema="MessagePage")[ "json" ] stored = [message for message in page["items"] if message["id"] == next(iter(ids))] check(len(stored) == 1, "concurrent requests stored more than one message") setup.call( "POST", path, 409, actor=alice, body={"text": "different body"}, headers={"Idempotency-Key": key}, ) print( f"PASS: {args.requests} concurrent requests used {len(instances)} replicas and stored one message" ) if __name__ == "__main__": main()