Skip to content

Commit 129ef25

Browse files
Backport more pytest-asyncio changes (#12402)
1 parent 3bfd03a commit 129ef25

22 files changed

+787
-1388
lines changed

aiohttp/pytest_plugin.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,10 @@ def _passthrough_loop_context(
199199
else:
200200
# this shadows loop_context's standard behavior
201201
loop = setup_test_loop()
202-
yield loop
203-
teardown_test_loop(loop, fast=fast)
202+
try:
203+
yield loop
204+
finally:
205+
teardown_test_loop(loop, fast=fast)
204206

205207

206208
def pytest_pycollect_makeitem(collector, name, obj): # type: ignore[no-untyped-def]

tests/conftest.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from aiohttp.compression_utils import ZLibBackend, ZLibBackendProtocol, set_zlib_backend
3838
from aiohttp.helpers import TimerNoop
3939
from aiohttp.http import WS_KEY, HttpVersion11
40-
from aiohttp.test_utils import get_unused_port_socket, loop_context
40+
from aiohttp.test_utils import REUSE_ADDRESS, loop_context
4141

4242

4343
def pytest_configure(config: pytest.Config) -> None:
@@ -387,11 +387,8 @@ def unused_port_socket() -> Iterator[socket.socket]:
387387
race condition between checking if the port is in use and
388388
binding to it later in the test.
389389
"""
390-
s = get_unused_port_socket("127.0.0.1")
391-
try:
390+
with socket.create_server(("127.0.0.1", 0), reuse_port=REUSE_ADDRESS) as s:
392391
yield s
393-
finally:
394-
s.close()
395392

396393

397394
@pytest.fixture(params=["zlib", "zlib_ng.zlib_ng", "isal.isal_zlib"])
@@ -433,7 +430,7 @@ def maker(
433430
session = ClientSession()
434431
sessions.append(session)
435432
default_args: ClientRequestArgs = {
436-
"loop": loop,
433+
"loop": asyncio.get_running_loop(),
437434
"params": {},
438435
"headers": CIMultiDict[str](),
439436
"skip_auto_headers": None,

tests/test_client_proto.py

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@
1313
from aiohttp.http_parser import HttpParser, RawResponseMessage
1414

1515

16-
async def test_force_close(loop: asyncio.AbstractEventLoop) -> None:
16+
async def test_force_close() -> None:
1717
"""Ensure that the force_close method sets the should_close attribute to True.
1818
1919
This is used externally in aiodocker
2020
https://github.com/aio-libs/aiodocker/issues/920
2121
"""
22-
proto = ResponseHandler(loop=loop)
22+
proto = ResponseHandler(loop=asyncio.get_running_loop())
2323
proto.force_close()
2424
assert proto.should_close
2525

2626

27-
async def test_oserror(loop: asyncio.AbstractEventLoop) -> None:
28-
proto = ResponseHandler(loop=loop)
27+
async def test_oserror() -> None:
28+
proto = ResponseHandler(loop=asyncio.get_running_loop())
2929
transport = mock.Mock()
3030
proto.connection_made(transport)
3131
proto.connection_lost(OSError())
@@ -34,9 +34,9 @@ async def test_oserror(loop: asyncio.AbstractEventLoop) -> None:
3434
assert isinstance(proto.exception(), ClientOSError)
3535

3636

37-
async def test_pause_resume_on_error(loop: asyncio.AbstractEventLoop) -> None:
37+
async def test_pause_resume_on_error() -> None:
3838
parser = mock.create_autospec(HttpParser, spec_set=True, instance=True)
39-
proto = ResponseHandler(loop=loop)
39+
proto = ResponseHandler(loop=asyncio.get_running_loop())
4040
proto._parser = parser
4141
transport = mock.Mock()
4242
proto.connection_made(transport)
@@ -48,8 +48,8 @@ async def test_pause_resume_on_error(loop: asyncio.AbstractEventLoop) -> None:
4848
assert not proto._reading_paused
4949

5050

51-
async def test_client_proto_bad_message(loop: asyncio.AbstractEventLoop) -> None:
52-
proto = ResponseHandler(loop=loop)
51+
async def test_client_proto_bad_message() -> None:
52+
proto = ResponseHandler(loop=asyncio.get_running_loop())
5353
transport = mock.Mock()
5454
proto.connection_made(transport)
5555
proto.set_response_params()
@@ -60,8 +60,8 @@ async def test_client_proto_bad_message(loop: asyncio.AbstractEventLoop) -> None
6060
assert isinstance(proto.exception(), http.HttpProcessingError)
6161

6262

63-
async def test_uncompleted_message(loop: asyncio.AbstractEventLoop) -> None:
64-
proto = ResponseHandler(loop=loop)
63+
async def test_uncompleted_message() -> None:
64+
proto = ResponseHandler(loop=asyncio.get_running_loop())
6565
transport = mock.Mock()
6666
proto.connection_made(transport)
6767
proto.set_response_params(read_until_eof=True)
@@ -78,8 +78,8 @@ async def test_uncompleted_message(loop: asyncio.AbstractEventLoop) -> None:
7878
assert dict(exc.message.headers) == {"Location": "http://python.org/"}
7979

8080

81-
async def test_data_received_after_close(loop: asyncio.AbstractEventLoop) -> None:
82-
proto = ResponseHandler(loop=loop)
81+
async def test_data_received_after_close() -> None:
82+
proto = ResponseHandler(loop=asyncio.get_running_loop())
8383
transport = mock.Mock()
8484
proto.connection_made(transport)
8585
proto.set_response_params(read_until_eof=True)
@@ -92,9 +92,8 @@ async def test_data_received_after_close(loop: asyncio.AbstractEventLoop) -> Non
9292
assert isinstance(proto.exception(), http.HttpProcessingError)
9393

9494

95-
async def test_multiple_responses_one_byte_at_a_time(
96-
loop: asyncio.AbstractEventLoop,
97-
) -> None:
95+
async def test_multiple_responses_one_byte_at_a_time() -> None:
96+
loop = asyncio.get_running_loop()
9897
proto = ResponseHandler(loop=loop)
9998
proto.connection_made(mock.Mock())
10099
conn = mock.Mock(protocol=proto)
@@ -128,9 +127,8 @@ async def test_multiple_responses_one_byte_at_a_time(
128127
await response.read() == payload
129128

130129

131-
async def test_unexpected_exception_during_data_received(
132-
loop: asyncio.AbstractEventLoop,
133-
) -> None:
130+
async def test_unexpected_exception_during_data_received() -> None:
131+
loop = asyncio.get_running_loop()
134132
proto = ResponseHandler(loop=loop)
135133

136134
class PatchableHttpResponseParser(http.HttpResponseParser):
@@ -164,7 +162,8 @@ class PatchableHttpResponseParser(http.HttpResponseParser):
164162
assert isinstance(proto.exception(), http.HttpProcessingError)
165163

166164

167-
async def test_client_protocol_readuntil_eof(loop: asyncio.AbstractEventLoop) -> None:
165+
async def test_client_protocol_readuntil_eof() -> None:
166+
loop = asyncio.get_running_loop()
168167
proto = ResponseHandler(loop=loop)
169168
transport = mock.Mock()
170169
proto.connection_made(transport)
@@ -203,32 +202,32 @@ async def test_client_protocol_readuntil_eof(loop: asyncio.AbstractEventLoop) ->
203202
assert response.content.is_eof()
204203

205204

206-
async def test_empty_data(loop: asyncio.AbstractEventLoop) -> None:
207-
proto = ResponseHandler(loop=loop)
205+
async def test_empty_data() -> None:
206+
proto = ResponseHandler(loop=asyncio.get_running_loop())
208207
proto.data_received(b"")
209208

210209
# do nothing
211210

212211

213-
async def test_schedule_timeout(loop: asyncio.AbstractEventLoop) -> None:
214-
proto = ResponseHandler(loop=loop)
212+
async def test_schedule_timeout() -> None:
213+
proto = ResponseHandler(loop=asyncio.get_running_loop())
215214
proto.set_response_params(read_timeout=1)
216215
assert proto._read_timeout_handle is None
217216
proto.start_timeout()
218217
assert proto._read_timeout_handle is not None
219218

220219

221-
async def test_drop_timeout(loop: asyncio.AbstractEventLoop) -> None:
222-
proto = ResponseHandler(loop=loop)
220+
async def test_drop_timeout() -> None:
221+
proto = ResponseHandler(loop=asyncio.get_running_loop())
223222
proto.set_response_params(read_timeout=1)
224223
proto.start_timeout()
225224
assert proto._read_timeout_handle is not None
226225
proto._drop_timeout()
227226
assert proto._read_timeout_handle is None
228227

229228

230-
async def test_reschedule_timeout(loop: asyncio.AbstractEventLoop) -> None:
231-
proto = ResponseHandler(loop=loop)
229+
async def test_reschedule_timeout() -> None:
230+
proto = ResponseHandler(loop=asyncio.get_running_loop())
232231
proto.set_response_params(read_timeout=1)
233232
proto.start_timeout()
234233
assert proto._read_timeout_handle is not None
@@ -238,23 +237,21 @@ async def test_reschedule_timeout(loop: asyncio.AbstractEventLoop) -> None:
238237
assert proto._read_timeout_handle is not h
239238

240239

241-
async def test_eof_received(loop: asyncio.AbstractEventLoop) -> None:
242-
proto = ResponseHandler(loop=loop)
240+
async def test_eof_received() -> None:
241+
proto = ResponseHandler(loop=asyncio.get_running_loop())
243242
proto.set_response_params(read_timeout=1)
244243
proto.start_timeout()
245244
assert proto._read_timeout_handle is not None
246245
proto.eof_received()
247246
assert proto._read_timeout_handle is None
248247

249248

250-
async def test_connection_lost_sets_transport_to_none(
251-
loop: asyncio.AbstractEventLoop, mocker: MockerFixture
252-
) -> None:
249+
async def test_connection_lost_sets_transport_to_none(mocker: MockerFixture) -> None:
253250
"""Ensure that the transport is set to None when the connection is lost.
254251
255252
This ensures the writer knows that the connection is closed.
256253
"""
257-
proto = ResponseHandler(loop=loop)
254+
proto = ResponseHandler(loop=asyncio.get_running_loop())
258255
proto.connection_made(mocker.Mock())
259256
assert proto.transport is not None
260257

@@ -263,11 +260,9 @@ async def test_connection_lost_sets_transport_to_none(
263260
assert proto.transport is None
264261

265262

266-
async def test_connection_lost_exception_is_marked_retrieved(
267-
loop: asyncio.AbstractEventLoop,
268-
) -> None:
263+
async def test_connection_lost_exception_is_marked_retrieved() -> None:
269264
"""Test that connection_lost properly handles exceptions without warnings."""
270-
proto = ResponseHandler(loop=loop)
265+
proto = ResponseHandler(loop=asyncio.get_running_loop())
271266
proto.connection_made(mock.Mock())
272267

273268
# Access closed property before connection_lost to ensure future is created
@@ -286,11 +281,9 @@ async def test_connection_lost_exception_is_marked_retrieved(
286281
assert exc.__cause__ is ssl_error
287282

288283

289-
async def test_closed_property_lazy_creation(
290-
loop: asyncio.AbstractEventLoop,
291-
) -> None:
284+
async def test_closed_property_lazy_creation() -> None:
292285
"""Test that closed future is created lazily."""
293-
proto = ResponseHandler(loop=loop)
286+
proto = ResponseHandler(loop=asyncio.get_running_loop())
294287

295288
# Initially, the closed future should not be created
296289
assert proto._closed is None
@@ -305,11 +298,9 @@ async def test_closed_property_lazy_creation(
305298
assert proto.closed is closed_future
306299

307300

308-
async def test_closed_property_after_connection_lost(
309-
loop: asyncio.AbstractEventLoop,
310-
) -> None:
301+
async def test_closed_property_after_connection_lost() -> None:
311302
"""Test that closed property returns None after connection_lost if never accessed."""
312-
proto = ResponseHandler(loop=loop)
303+
proto = ResponseHandler(loop=asyncio.get_running_loop())
313304
proto.connection_made(mock.Mock())
314305

315306
# Don't access proto.closed before connection_lost
@@ -319,9 +310,9 @@ async def test_closed_property_after_connection_lost(
319310
assert proto.closed is None
320311

321312

322-
async def test_abort(loop: asyncio.AbstractEventLoop) -> None:
313+
async def test_abort() -> None:
323314
"""Test the abort() method."""
324-
proto = ResponseHandler(loop=loop)
315+
proto = ResponseHandler(loop=asyncio.get_running_loop())
325316

326317
# Create a mock transport
327318
transport = mock.Mock()
@@ -345,9 +336,9 @@ async def test_abort(loop: asyncio.AbstractEventLoop) -> None:
345336
mock_drop_timeout.assert_called_once()
346337

347338

348-
async def test_abort_without_transport(loop: asyncio.AbstractEventLoop) -> None:
339+
async def test_abort_without_transport() -> None:
349340
"""Test abort() when transport is None."""
350-
proto = ResponseHandler(loop=loop)
341+
proto = ResponseHandler(loop=asyncio.get_running_loop())
351342

352343
# Mock _drop_timeout method using patch.object
353344
with mock.patch.object(proto, "_drop_timeout") as mock_drop_timeout:

0 commit comments

Comments
 (0)