Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
111 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import asyncio | ||
from asyncio.events import BaseDefaultEventLoopPolicy | ||
|
||
import pytest | ||
|
||
|
||
class CustomSelectorLoop(asyncio.SelectorEventLoop): | ||
"""A subclass with no overrides, just to test for presence.""" | ||
|
||
pass | ||
|
||
|
||
class CustomSelectorLoopPolicy(BaseDefaultEventLoopPolicy): | ||
def new_event_loop(self): | ||
"""Create a new event loop. | ||
You must call set_event_loop() to make this the current event | ||
loop. | ||
""" | ||
return CustomSelectorLoop() | ||
|
||
|
||
@pytest.fixture(autouse=True, scope="package") | ||
def event_loop_policy(): | ||
"""Create an instance of the default event loop for each test case.""" | ||
asyncio.set_event_loop_policy(CustomSelectorLoopPolicy()) | ||
yield | ||
asyncio.set_event_loop_policy(None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""Unit tests for overriding the event loop.""" | ||
import asyncio | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
async def an_async_fixture(): | ||
"""An async fixture, to test if the loop is set correctly.""" | ||
assert type(asyncio.get_event_loop()).__name__ == "CustomSelectorLoop" | ||
yield 1 | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_for_custom_loop(an_async_fixture): | ||
"""This test should be executed using the custom loop.""" | ||
await asyncio.sleep(0.01) | ||
assert type(asyncio.get_event_loop()).__name__ == "CustomSelectorLoop" | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_dependent_fixture(dependent_fixture): | ||
await asyncio.sleep(0.1) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_for_custom_loop_2(): | ||
"""This test should be executed using the custom loop.""" | ||
await asyncio.sleep(0.01) | ||
assert type(asyncio.get_event_loop()).__name__ == "CustomSelectorLoop" |