Skip to content

Commit 8f6c87c

Browse files
Fix Test Cases: test_http for Py3.9+, test_json_response_json for ujson 5.4.0+, and test_zero_downtime; Test Case Type Annotations (sanic-org#2504)
1 parent 4429e76 commit 8f6c87c

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

tests/test_http.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from collections import namedtuple
44
from pathlib import Path
5+
from sys import version_info
56

67
import pytest
78

@@ -35,15 +36,15 @@ async def upload_handler(request):
3536

3637

3738
@pytest.fixture
38-
def runner(test_app):
39+
def runner(test_app: Sanic):
3940
client = ReusableClient(test_app, port=PORT)
4041
client.run()
4142
yield client
4243
client.stop()
4344

4445

4546
@pytest.fixture
46-
def client(runner):
47+
def client(runner: ReusableClient):
4748
client = namedtuple("Client", ("raw", "send", "recv"))
4849

4950
raw = RawClient(runner.host, runner.port)
@@ -74,7 +75,10 @@ def test_full_message(client):
7475
"""
7576
)
7677
response = client.recv()
77-
assert len(response) == 151
78+
79+
# AltSvcCheck touchup removes the Alt-Svc header from the
80+
# response in the Python 3.9+ in this case
81+
assert len(response) == (151 if version_info < (3, 9) else 140)
7882
assert b"200 OK" in response
7983

8084

tests/test_json_encoding.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,27 @@
33
from dataclasses import asdict, dataclass
44
from functools import partial
55
from json import dumps as sdumps
6+
from string import ascii_lowercase
7+
from typing import Dict
68

79
import pytest
810

911

1012
try:
13+
import ujson
14+
1115
from ujson import dumps as udumps
1216

17+
ujson_version = tuple(
18+
map(int, ujson.__version__.strip(ascii_lowercase).split("."))
19+
)
20+
1321
NO_UJSON = False
1422
DEFAULT_DUMPS = udumps
1523
except ModuleNotFoundError:
1624
NO_UJSON = True
1725
DEFAULT_DUMPS = partial(sdumps, separators=(",", ":"))
26+
ujson_version = None
1827

1928
from sanic import Sanic
2029
from sanic.response import BaseHTTPResponse, json
@@ -34,7 +43,7 @@ def foo():
3443

3544

3645
@pytest.fixture
37-
def payload(foo):
46+
def payload(foo: Foo):
3847
return {"foo": foo}
3948

4049

@@ -58,7 +67,7 @@ def my_custom_encoder():
5867

5968

6069
@pytest.mark.skipif(NO_UJSON is True, reason="ujson not installed")
61-
def test_json_response_ujson(payload):
70+
def test_json_response_ujson(payload: Dict[str, Foo]):
6271
"""ujson will look at __json__"""
6372
response = json(payload)
6473
assert response.body == b'{"foo":{"bar":"bar"}}'
@@ -75,7 +84,13 @@ def test_json_response_ujson(payload):
7584
json(payload)
7685

7786

78-
@pytest.mark.skipif(NO_UJSON is True, reason="ujson not installed")
87+
@pytest.mark.skipif(
88+
NO_UJSON is True or ujson_version >= (5, 4, 0),
89+
reason=(
90+
"ujson not installed or version is 5.4.0 or newer, "
91+
"which can handle arbitrary size integers"
92+
),
93+
)
7994
def test_json_response_json():
8095
"""One of the easiest ways to tell the difference is that ujson cannot
8196
serialize over 64 bits"""

tests/test_unix_socket.py

+16-12
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
import subprocess
66
import sys
77

8+
from asyncio import AbstractEventLoop
89
from string import ascii_lowercase
910

1011
import httpcore
1112
import httpx
1213
import pytest
1314

15+
from pytest import LogCaptureFixture
16+
1417
from sanic import Sanic
18+
from sanic.request import Request
1519
from sanic.response import text
1620

1721

@@ -45,7 +49,7 @@ def socket_cleanup():
4549
pass
4650

4751

48-
def test_unix_socket_creation(caplog):
52+
def test_unix_socket_creation(caplog: LogCaptureFixture):
4953
from socket import AF_UNIX, socket
5054

5155
with socket(AF_UNIX) as sock:
@@ -56,7 +60,7 @@ def test_unix_socket_creation(caplog):
5660
app = Sanic(name="test")
5761

5862
@app.listener("after_server_start")
59-
def running(app, loop):
63+
def running(app: Sanic, loop: AbstractEventLoop):
6064
assert os.path.exists(SOCKPATH)
6165
assert ino != os.stat(SOCKPATH).st_ino
6266
app.stop()
@@ -73,7 +77,7 @@ def running(app, loop):
7377

7478

7579
@pytest.mark.parametrize("path", (".", "no-such-directory/sanictest.sock"))
76-
def test_invalid_paths(path):
80+
def test_invalid_paths(path: str):
7781
app = Sanic(name="test")
7882

7983
with pytest.raises((FileExistsError, FileNotFoundError)):
@@ -87,7 +91,7 @@ def test_dont_replace_file():
8791
app = Sanic(name="test")
8892

8993
@app.listener("after_server_start")
90-
def stop(app, loop):
94+
def stop(app: Sanic, loop: AbstractEventLoop):
9195
app.stop()
9296

9397
with pytest.raises(FileExistsError):
@@ -104,7 +108,7 @@ def test_dont_follow_symlink():
104108
app = Sanic(name="test")
105109

106110
@app.listener("after_server_start")
107-
def stop(app, loop):
111+
def stop(app: Sanic, loop: AbstractEventLoop):
108112
app.stop()
109113

110114
with pytest.raises(FileExistsError):
@@ -115,7 +119,7 @@ def test_socket_deleted_while_running():
115119
app = Sanic(name="test")
116120

117121
@app.listener("after_server_start")
118-
async def hack(app, loop):
122+
async def hack(app: Sanic, loop: AbstractEventLoop):
119123
os.unlink(SOCKPATH)
120124
app.stop()
121125

@@ -126,7 +130,7 @@ def test_socket_replaced_with_file():
126130
app = Sanic(name="test")
127131

128132
@app.listener("after_server_start")
129-
async def hack(app, loop):
133+
async def hack(app: Sanic, loop: AbstractEventLoop):
130134
os.unlink(SOCKPATH)
131135
with open(SOCKPATH, "w") as f:
132136
f.write("Not a socket")
@@ -139,11 +143,11 @@ def test_unix_connection():
139143
app = Sanic(name="test")
140144

141145
@app.get("/")
142-
def handler(request):
146+
def handler(request: Request):
143147
return text(f"{request.conn_info.server}")
144148

145149
@app.listener("after_server_start")
146-
async def client(app, loop):
150+
async def client(app: Sanic, loop: AbstractEventLoop):
147151
if httpx_version >= (0, 20):
148152
transport = httpx.AsyncHTTPTransport(uds=SOCKPATH)
149153
else:
@@ -162,11 +166,11 @@ async def client(app, loop):
162166
app_multi = Sanic(name="test")
163167

164168

165-
def handler(request):
169+
def handler(request: Request):
166170
return text(f"{request.conn_info.server}")
167171

168172

169-
async def client(app, loop):
173+
async def client(app: Sanic, loop: AbstractEventLoop):
170174
try:
171175
async with httpx.AsyncClient(uds=SOCKPATH) as client:
172176
r = await client.get("http://myhost.invalid/")
@@ -229,7 +233,7 @@ def spawn():
229233
ino = os.stat(SOCKPATH).st_ino
230234
task = asyncio.get_event_loop().create_task(client())
231235
start_time = current_time()
232-
while current_time() < start_time + 4:
236+
while current_time() < start_time + 6:
233237
# Start a new one and wait until the socket is replaced
234238
processes.append(spawn())
235239
while ino == os.stat(SOCKPATH).st_ino:

0 commit comments

Comments
 (0)