From ec417f7f84f3533f928a4bc2b8dd0c6c51cdbbad Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Sun, 4 Feb 2024 13:50:31 +0100 Subject: [PATCH] Create `anyio.Event` on async context (#2459) * Create `anyio.Event` using a blocking portal * Use cached property * Ups --- starlette/testclient.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/starlette/testclient.py b/starlette/testclient.py index 5b557032..9e3ece4d 100644 --- a/starlette/testclient.py +++ b/starlette/testclient.py @@ -10,6 +10,7 @@ import sys import typing import warnings from concurrent.futures import Future +from functools import cached_property from types import GeneratorType from urllib.parse import unquote, urljoin @@ -95,7 +96,6 @@ class WebSocketTestSession: def __enter__(self) -> WebSocketTestSession: self.exit_stack = contextlib.ExitStack() self.portal = self.exit_stack.enter_context(self.portal_factory()) - self.should_close = anyio.Event() try: _: Future[None] = self.portal.start_task_soon(self._run) @@ -109,6 +109,10 @@ class WebSocketTestSession: self.extra_headers = message.get("headers", None) return self + @cached_property + def should_close(self) -> anyio.Event: + return anyio.Event() + async def _notify_close(self) -> None: self.should_close.set()