Skip to content

Commit

Permalink
Add reusable client tests and refactor (#74)
Browse files Browse the repository at this point in the history
in python 3.12, its required to close the writer to stop the server. unless its hangs on `wait_closed`
  • Loading branch information
hamedsh authored May 3, 2024
1 parent 6e632cc commit c60e403
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
7 changes: 4 additions & 3 deletions sanic_testing/reusable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
26 changes: 26 additions & 0 deletions tests/test_reusable_client.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c60e403

Please sign in to comment.