Debian Codegraph @

compliance/asyncio/client.py

raw
1 import asyncio
2 import json
3 import logging
4
5 from websockets.asyncio.client import connect
6 from websockets.exceptions import WebSocketException
7
8
9 logging.basicConfig(level=logging.WARNING)
10
11 SERVER = "ws://localhost:9001"
12
13 AGENT = "websockets.asyncio"
14
15
16 async def get_case_count():
17 async with connect(f"{SERVER}/getCaseCount") as ws:
18 return json.loads(await ws.recv())
19
20
21 async def run_case(case):
22 async with connect(
23 f"{SERVER}/runCase?case={case}&agent={AGENT}",
24 max_size=2**25,
25 ) as ws:
26 try:
27 async for msg in ws:
28 await ws.send(msg)
29 except WebSocketException:
30 pass
31
32
33 async def update_reports():
34 async with connect(
35 f"{SERVER}/updateReports?agent={AGENT}",
36 open_timeout=60,
37 ):
38 pass
39
40
41 async def main():
42 cases = await get_case_count()
43 for case in range(1, cases + 1):
44 print(f"Running test case {case:03d} / {cases}... ", end="\t")
45 try:
46 await run_case(case)
47 except WebSocketException as exc:
48 print(f"ERROR: {type(exc).__name__}: {exc}")
49 except Exception as exc:
50 print(f"FAIL: {type(exc).__name__}: {exc}")
51 else:
52 print("OK")
53 print(f"Ran {cases} test cases")
54 await update_reports()
55 print("Updated reports")
56
57
58 if __name__ == "__main__":
59 asyncio.run(main())