Skip to content

Commit

Permalink
Switched from pytest-asyncio to anyio as the async test plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed Nov 23, 2024
1 parent 83614a8 commit 278493f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
4 changes: 1 addition & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ zookeeper = ["kazoo"]
test = [
"APScheduler[mongodb,redis,rethinkdb,sqlalchemy,tornado,zookeeper,etcd]",
"pytest",
"pytest-asyncio >= 0.24.0",
"anyio >= 4.5.2",
"PySide6; python_implementation == 'CPython' and python_version < '3.14'",
"gevent; python_version < '3.14'",
"twisted; python_version < '3.14'",
Expand Down Expand Up @@ -98,8 +98,6 @@ local_scheme = "dirty-tag"
addopts = "-rsx --tb=short"
testpaths = "tests"
filterwarnings = "always"
asyncio_mode = "strict"
asyncio_default_fixture_loop_scope = "function"
markers = [
"external_service: marks tests as requiring some external service",
]
Expand Down
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
from apscheduler.schedulers.blocking import BlockingScheduler


@pytest.fixture
def anyio_backend():
return "asyncio"


@pytest.fixture
def timezone(monkeypatch):
tz = pytz.timezone("Europe/Berlin")
Expand Down
14 changes: 7 additions & 7 deletions tests/test_executors.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ def executor(request, mock_scheduler):


@pytest.fixture
def asyncio_scheduler(event_loop):
scheduler = AsyncIOScheduler(event_loop=event_loop)
async def asyncio_scheduler():
scheduler = AsyncIOScheduler()
scheduler.start(paused=True)
yield scheduler
scheduler.shutdown(False)


@pytest.fixture
def asyncio_executor(asyncio_scheduler):
async def asyncio_executor(asyncio_scheduler):
executor = AsyncIOExecutor()
executor.start(asyncio_scheduler, "default")
yield executor
Expand Down Expand Up @@ -207,7 +207,7 @@ def func():
assert len(foos) == 0


@pytest.mark.asyncio
@pytest.mark.anyio
async def test_run_async_job_memory_leak():
class FooBar:
pass
Expand Down Expand Up @@ -253,7 +253,7 @@ def listener(evt):


@pytest.mark.parametrize("exception", [False, True])
@pytest.mark.asyncio
@pytest.mark.anyio
async def test_run_coroutine_job(asyncio_scheduler, asyncio_executor, exception):
from asyncio import Future, sleep

Expand All @@ -273,7 +273,7 @@ async def test_run_coroutine_job(asyncio_scheduler, asyncio_executor, exception)


@pytest.mark.parametrize("exception", [False, True])
@pytest.mark.asyncio
@pytest.mark.anyio
async def test_run_coroutine_job_tornado(
tornado_scheduler, tornado_executor, exception
):
Expand All @@ -295,7 +295,7 @@ async def test_run_coroutine_job_tornado(
assert events[0].retval is True


@pytest.mark.asyncio
@pytest.mark.anyio
async def test_asyncio_executor_shutdown(asyncio_scheduler, asyncio_executor):
"""Test that the AsyncIO executor cancels its pending tasks on shutdown."""
from asyncio import sleep
Expand Down
14 changes: 9 additions & 5 deletions tests/test_schedulers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import logging
import pickle
from datetime import datetime, timedelta
Expand Down Expand Up @@ -37,6 +38,7 @@
SchedulerAlreadyRunningError,
SchedulerNotRunningError,
)
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.schedulers.base import STATE_RUNNING, STATE_STOPPED, BaseScheduler
from apscheduler.triggers.base import BaseTrigger
from apscheduler.util import undefined
Expand Down Expand Up @@ -1148,20 +1150,22 @@ def scheduler(self):

class TestAsyncIOScheduler(SchedulerImplementationTestBase):
@pytest.fixture
def scheduler(self, event_loop):
asyncio = pytest.importorskip("apscheduler.schedulers.asyncio")
return asyncio.AsyncIOScheduler(event_loop=event_loop)
def scheduler(self):
return AsyncIOScheduler()

@pytest.fixture
def start_scheduler(self, request, event_loop, scheduler):
event_loop.call_soon_threadsafe(scheduler.start)
def start_scheduler(self, request, scheduler):
event_loop = asyncio.new_event_loop()
event_loop.call_soon(scheduler.start)
thread = Thread(target=event_loop.run_forever)
yield thread.start

if scheduler.running:
event_loop.call_soon_threadsafe(scheduler.shutdown)

event_loop.call_soon_threadsafe(event_loop.stop)
thread.join()
event_loop.close()


class TestGeventScheduler(SchedulerImplementationTestBase):
Expand Down

0 comments on commit 278493f

Please sign in to comment.