Skip to content

Commit 2751982

Browse files
seifertmTinche
authored andcommitted
refactor: Replaced tests asserting that the event loop is properly closed.
Under Python 3.10, the tests raise: "DeprecationWarning: There is no current event loop" This means that the asyncio.get_event_loop does not return any existing loop. It creates a new loop as a side effect instead. Therefore, test_1 and test_3 will always be successful. However, it would be wrong to simply delete the tests without a replacement. Although, we cannot retrieve the default loop with get_event_loop, we can still retrieve it from the event loop policy. The new tests do exactly that and assert that the loop is a different object than in the tests before. Signed-off-by: Michael Seifert <[email protected]>
1 parent 70989fd commit 2751982

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

tests/test_event_loop_scope.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
1-
"""Test the event loop fixture is properly disposed of.
1+
"""Test the event loop fixture provides a separate loop for each test.
22
33
These tests need to be run together.
44
"""
55
import asyncio
66

77
import pytest
88

9+
loop: asyncio.AbstractEventLoop
10+
911

1012
def test_1():
11-
loop = asyncio.get_event_loop()
12-
assert not loop.is_closed()
13+
global loop
14+
# The main thread should have a default event loop.
15+
loop = asyncio.get_event_loop_policy().get_event_loop()
1316

1417

1518
@pytest.mark.asyncio
1619
async def test_2():
17-
pass
20+
global loop
21+
running_loop = asyncio.get_event_loop_policy().get_event_loop()
22+
# Make sure this test case received a different loop
23+
assert running_loop is not loop
24+
loop = running_loop # Store the loop reference for later
1825

1926

2027
def test_3():
21-
loop = asyncio.get_event_loop()
22-
assert not loop.is_closed()
28+
global loop
29+
current_loop = asyncio.get_event_loop_policy().get_event_loop()
30+
# Now the event loop from test_2 should have been cleaned up
31+
assert loop is not current_loop

0 commit comments

Comments
 (0)