summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAlex Grönholm <alex.gronholm@nextday.fi>2016-09-15 15:10:08 +0300
committerAlex Grönholm <alex.gronholm@nextday.fi>2016-09-15 15:10:47 +0300
commitd4bce351a4dc15a5eb4be1887999426d005ba2ac (patch)
tree2e3d107852e44aa0b10e4d99d423ffd56d9c5dcd /tests
parent9fa8a833bbc5ed75cbc9f5e3e4aa14ba2279b5dc (diff)
downloadapscheduler-d4bce351a4dc15a5eb4be1887999426d005ba2ac.tar.gz
Added coroutine support for asyncio and Tornado executors (fixes #96, #98)
Diffstat (limited to 'tests')
-rw-r--r--tests/conftest.py4
-rw-r--r--tests/test_executors_py35.py86
2 files changed, 90 insertions, 0 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
index 8d32424..19fba99 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -15,6 +15,10 @@ except ImportError:
from mock import Mock
+def pytest_ignore_collect(path, config):
+ return path.basename.endswith('_py35.py') and sys.version_info < (3, 5)
+
+
def minpython(*version):
version_str = '.'.join([str(num) for num in version])
diff --git a/tests/test_executors_py35.py b/tests/test_executors_py35.py
new file mode 100644
index 0000000..cc25d88
--- /dev/null
+++ b/tests/test_executors_py35.py
@@ -0,0 +1,86 @@
+"""Contains test functions using Python 3.3+ syntax."""
+from datetime import datetime
+
+import pytest
+from apscheduler.executors.asyncio import AsyncIOExecutor
+from apscheduler.executors.tornado import TornadoExecutor
+from apscheduler.schedulers.asyncio import AsyncIOScheduler
+from apscheduler.schedulers.tornado import TornadoScheduler
+from pytz import utc
+
+
+@pytest.fixture
+def asyncio_scheduler(event_loop):
+ scheduler = AsyncIOScheduler(event_loop=event_loop)
+ scheduler.start(paused=True)
+ yield scheduler
+ scheduler.shutdown(False)
+
+
+@pytest.fixture
+def asyncio_executor(asyncio_scheduler):
+ executor = AsyncIOExecutor()
+ executor.start(asyncio_scheduler, 'default')
+ yield executor
+ executor.shutdown()
+
+
+@pytest.fixture
+def tornado_scheduler(io_loop):
+ scheduler = TornadoScheduler(io_loop=io_loop)
+ scheduler.start(paused=True)
+ yield scheduler
+ scheduler.shutdown(False)
+
+
+@pytest.fixture
+def tornado_executor(tornado_scheduler):
+ executor = TornadoExecutor()
+ executor.start(tornado_scheduler, 'default')
+ yield executor
+ executor.shutdown()
+
+
+async def waiter(sleep, exception):
+ await sleep(0.1)
+ if exception:
+ raise Exception('dummy error')
+ else:
+ return True
+
+
+@pytest.mark.parametrize('exception', [False, True])
+@pytest.mark.asyncio
+async def test_run_coroutine_job(asyncio_scheduler, asyncio_executor, exception):
+ from asyncio import Future, sleep
+
+ future = Future()
+ job = asyncio_scheduler.add_job(waiter, 'interval', seconds=1, args=[sleep, exception])
+ asyncio_executor._run_job_success = lambda job_id, events: future.set_result(events)
+ asyncio_executor._run_job_error = lambda job_id, exc, tb: future.set_exception(exc)
+ asyncio_executor.submit_job(job, [datetime.now(utc)])
+ events = await future
+ assert len(events) == 1
+ if exception:
+ assert str(events[0].exception) == 'dummy error'
+ else:
+ assert events[0].retval is True
+
+
+@pytest.mark.parametrize('exception', [False, True])
+@pytest.mark.gen_test
+async def test_run_coroutine_job_tornado(tornado_scheduler, tornado_executor, exception):
+ from tornado.concurrent import Future
+ from tornado.gen import sleep
+
+ future = Future()
+ job = tornado_scheduler.add_job(waiter, 'interval', seconds=1, args=[sleep, exception])
+ tornado_executor._run_job_success = lambda job_id, events: future.set_result(events)
+ tornado_executor._run_job_error = lambda job_id, exc, tb: future.set_exception(exc)
+ tornado_executor.submit_job(job, [datetime.now(utc)])
+ events = await future
+ assert len(events) == 1
+ if exception:
+ assert str(events[0].exception) == 'dummy error'
+ else:
+ assert events[0].retval is True