Skip to content

Commit

Permalink
refactor: Replaced tests asserting that the event loop is properly cl…
Browse files Browse the repository at this point in the history
…osed.

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 <m.seifert@digitalernachschub.de>
  • Loading branch information
seifertm authored and Tinche committed May 30, 2021
1 parent 70989fd commit 2751982
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions tests/test_event_loop_scope.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
"""Test the event loop fixture is properly disposed of.
"""Test the event loop fixture provides a separate loop for each test.
These tests need to be run together.
"""
import asyncio

import pytest

loop: asyncio.AbstractEventLoop


def test_1():
loop = asyncio.get_event_loop()
assert not loop.is_closed()
global loop
# The main thread should have a default event loop.
loop = asyncio.get_event_loop_policy().get_event_loop()


@pytest.mark.asyncio
async def test_2():
pass
global loop
running_loop = asyncio.get_event_loop_policy().get_event_loop()
# Make sure this test case received a different loop
assert running_loop is not loop
loop = running_loop # Store the loop reference for later


def test_3():
loop = asyncio.get_event_loop()
assert not loop.is_closed()
global loop
current_loop = asyncio.get_event_loop_policy().get_event_loop()
# Now the event loop from test_2 should have been cleaned up
assert loop is not current_loop

0 comments on commit 2751982

Please sign in to comment.