-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathtest_event_loop_scope.py
40 lines (28 loc) · 1.06 KB
/
test_event_loop_scope.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""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():
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():
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():
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
def test_4(event_loop):
# If a test sets the loop to None -- pytest_fixture_post_finalizer()
# still should work
# Close to avoid ResourceWarning about unclosed socket as a side effect
event_loop.close()
asyncio.get_event_loop_policy().set_event_loop(None)