changeset 3:b5afa9363c3b

add test helper
author drewp@bigasterisk.com
date Sat, 16 Mar 2024 11:40:08 -0700
parents b4dc3eb1f92b
children ddf021c87083
files examples/run_server_make_request.py
diffstat 1 files changed, 26 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/run_server_make_request.py	Sat Mar 16 11:40:08 2024 -0700
@@ -0,0 +1,26 @@
+import asyncio
+from dataclasses import dataclass
+from pathlib import Path
+
+import aiohttp
+
+
+@dataclass
+class HttpServer:
+    server_path: Path
+
+    async def __aenter__(self):
+        self.subprocess = await asyncio.create_subprocess_exec(
+            'pdm', 'run', 'python', self.server_path)
+        self._session = await aiohttp.ClientSession().__aenter__()
+        return self
+
+    async def __aexit__(self, exc_type, exc, tb):
+        self.subprocess.terminate()
+
+    async def get(self, url: str) -> aiohttp.ClientResponse:
+        while True:
+            try:
+                return await self._session.get(url)
+            except aiohttp.ClientConnectorError:
+                await asyncio.sleep(0.05)