import json
import os
import sys
import time
import urllib.request
import urllib.error
EXPECTED = {4: 3, 10: 55}
def send_one(base: str, key: str, job_id: int, n: int = 4) -> tuple[bool, str, float]:
url = f"{base.rstrip('/')}/execute"
body = json.dumps({"n": n}).encode("utf-8")
req = urllib.request.Request(
url,
data=body,
method="POST",
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {key}",
"X-Zakuro-Requirements": json.dumps({
"strategy": "round_robin",
"estimated_duration_secs": 1,
}),
},
)
t0 = time.perf_counter()
try:
with urllib.request.urlopen(req, timeout=30) as resp:
data = resp.read()
elapsed = time.perf_counter() - t0
try:
text = data.decode("utf-8", errors="replace")
j = json.loads(text)
result = j.get("result")
expected = EXPECTED.get(n)
ok = result == expected if expected is not None else result is not None
msg = f"fib({n})={result}" + (f" (expected {expected})" if expected is not None else "")
return ok, msg, elapsed
except json.JSONDecodeError as e:
return False, f"not JSON: {text[:80]!r}...", elapsed
except urllib.error.HTTPError as e:
elapsed = time.perf_counter() - t0
body = e.read()[:500]
try:
body_str = body.decode("utf-8", errors="replace").strip()
except Exception:
body_str = repr(body)
body_short = body_str[:60] + "..." if len(body_str) > 60 else body_str
return False, f"HTTP {e.code} {e.reason}: {body_short}", elapsed
except Exception as e:
elapsed = time.perf_counter() - t0
return False, str(e), elapsed
def main() -> None:
base = os.environ.get("ZAKURO_API_URL", "http://localhost:9000")
key = os.environ.get("ZAKURO_API_KEY")
if not key:
print("Set ZAKURO_API_KEY to run this script.", file=sys.stderr)
print("Example: export ZAKURO_API_KEY=zk_1000000001_xxx", file=sys.stderr)
sys.exit(1)
n_per_job = 4 total = 10
print(f"Publishing {total} fibonacci jobs (n={n_per_job}, expected result={EXPECTED.get(n_per_job)}) to {base}/execute ...")
print()
ok_count = 0
for i in range(total):
ok, msg, elapsed = send_one(base, key, i + 1, n=n_per_job)
if ok:
ok_count += 1
print(f" Job {i+1:2d}/{total}: OK {msg} ({elapsed:.2f}s)")
else:
print(f" Job {i+1:2d}/{total}: FAIL {msg} ({elapsed:.2f}s)")
print()
if ok_count == total:
print(f"All {total} jobs completed successfully. Workers were assigned and returned correct results.")
sys.exit(0)
else:
print(f"Only {ok_count}/{total} jobs succeeded.")
print("Ensure broker and fibonacci worker are running and ZAKURO_API_KEY is valid.")
sys.exit(1)
if __name__ == "__main__":
main()