5
5
import subprocess
6
6
import sys
7
7
8
+ from asyncio import AbstractEventLoop
8
9
from string import ascii_lowercase
9
10
10
11
import httpcore
11
12
import httpx
12
13
import pytest
13
14
15
+ from pytest import LogCaptureFixture
16
+
14
17
from sanic import Sanic
18
+ from sanic .request import Request
15
19
from sanic .response import text
16
20
17
21
@@ -45,7 +49,7 @@ def socket_cleanup():
45
49
pass
46
50
47
51
48
- def test_unix_socket_creation (caplog ):
52
+ def test_unix_socket_creation (caplog : LogCaptureFixture ):
49
53
from socket import AF_UNIX , socket
50
54
51
55
with socket (AF_UNIX ) as sock :
@@ -56,7 +60,7 @@ def test_unix_socket_creation(caplog):
56
60
app = Sanic (name = "test" )
57
61
58
62
@app .listener ("after_server_start" )
59
- def running (app , loop ):
63
+ def running (app : Sanic , loop : AbstractEventLoop ):
60
64
assert os .path .exists (SOCKPATH )
61
65
assert ino != os .stat (SOCKPATH ).st_ino
62
66
app .stop ()
@@ -73,7 +77,7 @@ def running(app, loop):
73
77
74
78
75
79
@pytest .mark .parametrize ("path" , ("." , "no-such-directory/sanictest.sock" ))
76
- def test_invalid_paths (path ):
80
+ def test_invalid_paths (path : str ):
77
81
app = Sanic (name = "test" )
78
82
79
83
with pytest .raises ((FileExistsError , FileNotFoundError )):
@@ -87,7 +91,7 @@ def test_dont_replace_file():
87
91
app = Sanic (name = "test" )
88
92
89
93
@app .listener ("after_server_start" )
90
- def stop (app , loop ):
94
+ def stop (app : Sanic , loop : AbstractEventLoop ):
91
95
app .stop ()
92
96
93
97
with pytest .raises (FileExistsError ):
@@ -104,7 +108,7 @@ def test_dont_follow_symlink():
104
108
app = Sanic (name = "test" )
105
109
106
110
@app .listener ("after_server_start" )
107
- def stop (app , loop ):
111
+ def stop (app : Sanic , loop : AbstractEventLoop ):
108
112
app .stop ()
109
113
110
114
with pytest .raises (FileExistsError ):
@@ -115,7 +119,7 @@ def test_socket_deleted_while_running():
115
119
app = Sanic (name = "test" )
116
120
117
121
@app .listener ("after_server_start" )
118
- async def hack (app , loop ):
122
+ async def hack (app : Sanic , loop : AbstractEventLoop ):
119
123
os .unlink (SOCKPATH )
120
124
app .stop ()
121
125
@@ -126,7 +130,7 @@ def test_socket_replaced_with_file():
126
130
app = Sanic (name = "test" )
127
131
128
132
@app .listener ("after_server_start" )
129
- async def hack (app , loop ):
133
+ async def hack (app : Sanic , loop : AbstractEventLoop ):
130
134
os .unlink (SOCKPATH )
131
135
with open (SOCKPATH , "w" ) as f :
132
136
f .write ("Not a socket" )
@@ -139,11 +143,11 @@ def test_unix_connection():
139
143
app = Sanic (name = "test" )
140
144
141
145
@app .get ("/" )
142
- def handler (request ):
146
+ def handler (request : Request ):
143
147
return text (f"{ request .conn_info .server } " )
144
148
145
149
@app .listener ("after_server_start" )
146
- async def client (app , loop ):
150
+ async def client (app : Sanic , loop : AbstractEventLoop ):
147
151
if httpx_version >= (0 , 20 ):
148
152
transport = httpx .AsyncHTTPTransport (uds = SOCKPATH )
149
153
else :
@@ -162,11 +166,11 @@ async def client(app, loop):
162
166
app_multi = Sanic (name = "test" )
163
167
164
168
165
- def handler (request ):
169
+ def handler (request : Request ):
166
170
return text (f"{ request .conn_info .server } " )
167
171
168
172
169
- async def client (app , loop ):
173
+ async def client (app : Sanic , loop : AbstractEventLoop ):
170
174
try :
171
175
async with httpx .AsyncClient (uds = SOCKPATH ) as client :
172
176
r = await client .get ("http://myhost.invalid/" )
@@ -229,7 +233,7 @@ def spawn():
229
233
ino = os .stat (SOCKPATH ).st_ino
230
234
task = asyncio .get_event_loop ().create_task (client ())
231
235
start_time = current_time ()
232
- while current_time () < start_time + 4 :
236
+ while current_time () < start_time + 6 :
233
237
# Start a new one and wait until the socket is replaced
234
238
processes .append (spawn ())
235
239
while ino == os .stat (SOCKPATH ).st_ino :
0 commit comments