Mercurial > code > home > repos > rdferry
comparison examples/_run_server_child.py @ 11:0bc06da6bf74
start ferry1 patch protocol
author | drewp@bigasterisk.com |
---|---|
date | Mon, 18 Mar 2024 16:42:21 -0700 |
parents | 52e1bb1532f2 |
children |
comparison
equal
deleted
inserted
replaced
10:52e1bb1532f2 | 11:0bc06da6bf74 |
---|---|
1 import asyncio | 1 import asyncio |
2 import contextlib | |
3 import logging | |
2 from dataclasses import dataclass | 4 from dataclasses import dataclass |
5 from datetime import timedelta | |
3 from pathlib import Path | 6 from pathlib import Path |
4 | 7 |
5 import aiohttp | 8 import aiohttp |
9 from aiohttp_sse_client import client as sse_client | |
10 | |
11 log = logging.getLogger('chil') | |
6 | 12 |
7 | 13 |
8 @dataclass | 14 @dataclass |
9 class RunHttpServerChildProcess: | 15 class RunHttpServerChildProcess: |
10 server_path: Path | 16 server_path: Path |
24 while True: | 30 while True: |
25 try: | 31 try: |
26 return await self._session.get(url, headers=headers) | 32 return await self._session.get(url, headers=headers) |
27 except aiohttp.ClientConnectorError: | 33 except aiohttp.ClientConnectorError: |
28 await asyncio.sleep(0.05) | 34 await asyncio.sleep(0.05) |
35 | |
36 @contextlib.asynccontextmanager | |
37 async def eventSource(self, url: str): | |
38 async with sse_client.EventSource( | |
39 url, reconnection_time=timedelta(seconds=.05)) as es: | |
40 yield es | |
41 | |
42 | |
43 async def assert_event_stream_starts_with(http_server, url, expected_events): | |
44 events_left = expected_events[:] | |
45 async with http_server.eventSource(url) as es: | |
46 async for event in es: | |
47 assert (event.message, event.data) == events_left[0] | |
48 events_left.pop(0) | |
49 if not events_left: | |
50 break |