Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Repository = "https://github.com/faust-streaming/mode"

[tool.pytest.ini_options]
minversion = "6.0"
asyncio_default_fixture_loop_scope = "function"
python_classes = "test_*"
testpaths = [
"tests/unit",
Expand Down
2 changes: 1 addition & 1 deletion requirements-tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ freezegun>=0.3.11
hypothesis>=3.31; platform_python_implementation != "PyPy"
hypothesis>=3.31,<6.156; platform_python_implementation == "PyPy"
pytest-aiofiles>=0.2.0
pytest-asyncio==0.21.1
pytest-asyncio>=1.0.0
pytest-base-url>=2.1.0
pytest-cov>=4.1.0
pytest-forked>=1.6.0
Expand Down
10 changes: 6 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pytest
import asyncio

import pytest_asyncio

@pytest.fixture
def loop(event_loop):
return event_loop

@pytest_asyncio.fixture
async def loop():
return asyncio.get_running_loop()
16 changes: 6 additions & 10 deletions tests/unit/test_threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ def test_stop__not_alive(self, *, thread):


class test_ServiceThread:
@pytest.fixture
def loop(self, *, event_loop):
return event_loop

@pytest.fixture
def thread_loop(self):
return Mock(name="thread_loop")
Expand Down Expand Up @@ -90,10 +86,10 @@ async def test_maybe_start(self, *, thread):
thread.start.assert_awaited_once()

@pytest.mark.asyncio
async def test_start(self, *, event_loop, thread):
async def test_start(self, *, loop, thread):
thread.add_future = AsyncMock(name="thread.add_future")
thread._thread_running = None
assert thread.parent_loop == event_loop
assert thread.parent_loop == loop
asyncio.ensure_future(self._wait_for_event(thread)) # noqa: RUF006
await thread.start()

Expand All @@ -103,11 +99,11 @@ async def test_start(self, *, event_loop, thread):
assert thread._thread_started.is_set()

@pytest.mark.asyncio
async def test_start__no_wait(self, *, event_loop, thread):
async def test_start__no_wait(self, *, loop, thread):
thread.add_future = Mock(name="thread.add_future")
thread.wait_for_thread = False
thread._thread_running = None
assert thread.parent_loop == event_loop
assert thread.parent_loop == loop
asyncio.ensure_future(self._wait_for_event(thread)) # noqa: RUF006
await thread.start()

Expand Down Expand Up @@ -265,7 +261,7 @@ async def test_crash(self, *, thread):
thread._thread_running.set_exception.assert_called_with(exc)

@pytest.mark.asyncio
async def test__wakeup_timer_in_thread(self, *, thread, event_loop):
async def test__wakeup_timer_in_thread(self, *, thread, loop):
thread.add_future = Mock(name="thread.add_future")
thread._wakeup_timer_in_thread = AsyncMock()
thread._stopped.is_set = Mock(return_value=False)
Expand All @@ -276,7 +272,7 @@ def cb():
thread._stopped.is_set.return_value = True
assert thread.should_stop

event_loop.call_soon(cb)
loop.call_soon(cb)
await thread._keepalive2()

thread._wakeup_timer_in_thread.assert_awaited()
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/utils/test_loops.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import pytest

from mode.utils import loops
from mode.utils.loops import (
_appropriate_signal_handler,
_call_asap,
Expand All @@ -17,6 +18,14 @@
)


@pytest.fixture(autouse=True)
def _no_cached_loop(monkeypatch):
# get_event_loop() caches the loop it resolves per thread. Earlier tests
# on this thread can leave a loop there that is still open (pytest-asyncio
# 1.x no longer closes it on teardown), so start each test without one.
monkeypatch.setattr(loops._current_loop, "loop", None, raising=False)


def test_get_event_loop__returns_running_loop_when_running():
async def main():
return get_event_loop()
Expand Down
Loading