"""12 simultaneous submits on one hosted-page session: exactly one may win. Uses the helpers of hpp_checks.py."""
import os, sys, json, threading, collections
import importlib.util, io, contextlib
# reuse helpers without re-running the checks
src = open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "hpp_checks.py")).read().split("# A. cancel redirect")[0]
ns = {}; exec(src, ns)
call, checkout = ns["call"], ns["checkout"]

for action in ["approve", "reject"]:
    sid, pid, _ = checkout()
    out, barrier = [], threading.Barrier(12)
    def go():
        barrier.wait()
        st, h, _ = call("POST", f"/hpp/{sid}", form={"action": action, "otp": "8888"})
        out.append((st, "redirect" if "location" in h else "processed-page"))
    ts = [threading.Thread(target=go) for _ in range(12)]
    [t.start() for t in ts]; [t.join() for t in ts]
    print(action, dict(collections.Counter(out)))
