#!/usr/bin/env python3 import importlib.util import json import os import sys from pathlib import Path from types import SimpleNamespace REQUESTS = 140 TOKEN = "synthetic-local-token" def load_worker(): os.environ["WAREHOUSE_API_URL"] = "http://127.0.0.1:9" os.environ["WAREHOUSE_CALLBACK_TOKEN"] = TOKEN path = Path(__file__).resolve().parents[1] / "warehouse" / "worker.py" spec = importlib.util.spec_from_file_location("starpost_warehouse_poc", path) if spec is None or spec.loader is None: raise RuntimeError("cannot load warehouse worker") module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module def main(): worker = load_worker() starts = [] class FakeThread: def __init__(self, target, args, daemon): self.target = target self.args = args self.daemon = daemon def start(self): starts.append((self.target, self.args, self.daemon)) worker.threading = SimpleNamespace(Thread=FakeThread) client = worker.app.test_client() statuses = [] for order_id in range(1, REQUESTS + 1): response = client.post( "/jobs", json={"order_id": order_id}, headers={"X-Warehouse-Token": TOKEN}, ) statuses.append(response.status_code) result = { "synthetic_requests": REQUESTS, "accepted": statuses.count(202), "jobs_retained": len(worker.jobs), "worker_threads_started": len(starts), "compose_pid_limit": 128, } print(json.dumps(result, sort_keys=True)) if result["accepted"] != REQUESTS or result["worker_threads_started"] != REQUESTS: return 1 if result["jobs_retained"] != REQUESTS: return 1 return 0 if __name__ == "__main__": try: raise SystemExit(main()) except Exception as error: print(f"error: {error}", file=sys.stderr) raise SystemExit(1)