From c60e4031b1317dc097b4be1452f59c8b770a5ac9 Mon Sep 17 00:00:00 2001 From: hamsh Date: Fri, 3 May 2024 07:11:08 +0100 Subject: [PATCH] Add reusable client tests and refactor (#74) in python 3.12, its required to close the writer to stop the server. unless its hangs on `wait_closed` --- sanic_testing/reusable.py | 7 ++++--- tests/test_reusable_client.py | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 tests/test_reusable_client.py diff --git a/sanic_testing/reusable.py b/sanic_testing/reusable.py index 9bd928c..5690dc4 100644 --- a/sanic_testing/reusable.py +++ b/sanic_testing/reusable.py @@ -81,14 +81,15 @@ def stop(self): self._run( self.app._server_event("shutdown", "before", loop=self._loop) ) + if self._session: + self._run(self._session.aclose()) + self._session = None + if self._server: self._server.close() self._run(self._server.wait_closed()) self._server = None - if self._session: - self._run(self._session.aclose()) - self._session = None self._run(self.app._server_event("shutdown", "after", loop=self._loop)) def _sanic_endpoint_test( diff --git a/tests/test_reusable_client.py b/tests/test_reusable_client.py new file mode 100644 index 0000000..2749b62 --- /dev/null +++ b/tests/test_reusable_client.py @@ -0,0 +1,26 @@ +import pytest +from sanic import Sanic, response + +from sanic_testing.reusable import ReusableClient + + +@pytest.fixture +def reusable_app(): + sanic_app = Sanic(__name__) + + @sanic_app.get("/") + def basic(request): + return response.text("foo") + + return sanic_app + + +@pytest.mark.asyncio +def test_basic_asgi_client(reusable_app): + client = ReusableClient(reusable_app) + with client: + request, response = client.get("/") + + assert request.method.lower() == "get" + assert response.body == b"foo" + assert response.status == 200