comparison examples/run_server_make_request.py @ 3:b5afa9363c3b

add test helper
author drewp@bigasterisk.com
date Sat, 16 Mar 2024 11:40:08 -0700
parents
children b34cd6619316
comparison
equal deleted inserted replaced
2:b4dc3eb1f92b 3:b5afa9363c3b
1 import asyncio
2 from dataclasses import dataclass
3 from pathlib import Path
4
5 import aiohttp
6
7
8 @dataclass
9 class HttpServer:
10 server_path: Path
11
12 async def __aenter__(self):
13 self.subprocess = await asyncio.create_subprocess_exec(
14 'pdm', 'run', 'python', self.server_path)
15 self._session = await aiohttp.ClientSession().__aenter__()
16 return self
17
18 async def __aexit__(self, exc_type, exc, tb):
19 self.subprocess.terminate()
20
21 async def get(self, url: str) -> aiohttp.ClientResponse:
22 while True:
23 try:
24 return await self._session.get(url)
25 except aiohttp.ClientConnectorError:
26 await asyncio.sleep(0.05)