From a4d83413caee31114094240e8e1c37ab70acf563 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janek=20Nouvertn=C3=A9?= <25355197+provinzkraut@users.noreply.github.com> Date: Sun, 15 Sep 2024 15:58:19 +0200 Subject: [PATCH] Use context manager --- litestar/testing/client/subprocess_client.py | 22 ++++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/litestar/testing/client/subprocess_client.py b/litestar/testing/client/subprocess_client.py index 337e302a59..7f91c8f338 100644 --- a/litestar/testing/client/subprocess_client.py +++ b/litestar/testing/client/subprocess_client.py @@ -28,21 +28,21 @@ def _get_available_port() -> int: def run_app(workdir: pathlib.Path, app: str) -> Iterator[str]: """Launch a litestar application in a subprocess with a random available port.""" port = _get_available_port() - proc = subprocess.Popen( + with subprocess.Popen( args=["litestar", "--app", app, "run", "--port", str(port)], stderr=subprocess.PIPE, stdout=subprocess.PIPE, cwd=workdir, - ) - url = f"http://127.0.0.1:{port}" - for _ in range(100): - try: - httpx.get(url, timeout=0.1) - break - except httpx.TransportError: - time.sleep(1) - yield url - proc.kill() + ) as proc: + url = f"http://127.0.0.1:{port}" + for _ in range(100): + try: + httpx.get(url, timeout=0.1) + break + except httpx.TransportError: + time.sleep(1) + yield url + proc.kill() @asynccontextmanager