diff --git a/pyproject.toml b/pyproject.toml index ae48c7c..bd1a5e5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", diff --git a/requirements-tests.txt b/requirements-tests.txt index d5424ff..0532bf1 100644 --- a/requirements-tests.txt +++ b/requirements-tests.txt @@ -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 diff --git a/tests/conftest.py b/tests/conftest.py index 9a43bc0..888f4dc 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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() diff --git a/tests/unit/test_threads.py b/tests/unit/test_threads.py index caf385d..05a66a2 100644 --- a/tests/unit/test_threads.py +++ b/tests/unit/test_threads.py @@ -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") @@ -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() @@ -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() @@ -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) @@ -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() diff --git a/tests/unit/utils/test_loops.py b/tests/unit/utils/test_loops.py index 4783688..dd25b2a 100644 --- a/tests/unit/utils/test_loops.py +++ b/tests/unit/utils/test_loops.py @@ -7,6 +7,7 @@ import pytest +from mode.utils import loops from mode.utils.loops import ( _appropriate_signal_handler, _call_asap, @@ -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()