Skip to content

Commit 0524c8d

Browse files
committed
add runner tests
Test that runners run at all (!), and that an async function can be used with the AsyncExecutor.
1 parent 99c80fe commit 0524c8d

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

adaptive/tests/test_runner.py

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,39 @@
22

33
import asyncio
44

5-
from ..learner import Learner2D
5+
import pytest
6+
7+
from ..learner import Learner1D, Learner2D
68
from ..runner import simple, BlockingRunner, AsyncRunner, SequentialExecutor
79

810

9-
def test_nonconforming_output():
11+
def blocking_runner(learner, goal):
12+
BlockingRunner(learner, goal, executor=SequentialExecutor())
13+
14+
15+
def async_runner(learner, goal):
16+
runner = AsyncRunner(learner, goal, executor=SequentialExecutor())
17+
asyncio.get_event_loop().run_until_complete(runner.task)
18+
19+
20+
runners = [simple, blocking_runner, async_runner]
21+
22+
def trivial_goal(learner):
23+
return learner.npoints > 10
24+
25+
@pytest.mark.parametrize('runner', runners)
26+
def test_simple(runner):
27+
"""Test that the runners actually run."""
28+
29+
def f(x):
30+
return x
31+
learner = Learner1D(f, (-1, 1))
32+
runner(learner, lambda l: l.npoints > 10)
33+
assert len(learner.data) > 10
34+
35+
36+
@pytest.mark.parametrize('runner', runners)
37+
def test_nonconforming_output(runner):
1038
"""Test that using a runner works with a 2D learner, even when the
1139
learned function outputs a 1-vector. This tests against the regression
1240
flagged in https://gitlab.kwant-project.org/qt/adaptive/issues/58.
@@ -15,15 +43,14 @@ def test_nonconforming_output():
1543
def f(x):
1644
return [0]
1745

18-
def goal(l):
19-
return l.npoints > 10
46+
runner(Learner2D(f, [(-1, 1), (-1, 1)]), trivial_goal)
2047

21-
learner = Learner2D(f, [(-1, 1), (-1, 1)])
22-
simple(learner, goal)
2348

24-
learner = Learner2D(f, [(-1, 1), (-1, 1)])
25-
BlockingRunner(learner, goal, executor=SequentialExecutor())
49+
def test_aync_def_function():
2650

27-
learner = Learner2D(f, [(-1, 1), (-1, 1)])
28-
runner = AsyncRunner(learner, goal, executor=SequentialExecutor())
51+
async def f(x):
52+
return x
53+
54+
learner = Learner1D(f, (-1, 1))
55+
runner = AsyncRunner(learner, trivial_goal)
2956
asyncio.get_event_loop().run_until_complete(runner.task)

0 commit comments

Comments
 (0)