From 9bd0cc986a3cbe1fbc5263ab015299ccb84f4e1a Mon Sep 17 00:00:00 2001 From: Joseph Weston <joseph@weston.cloud> Date: Thu, 25 Oct 2018 16:01:01 +0200 Subject: [PATCH] add tests against different executors --- adaptive/tests/test_runner.py | 55 ++++++++++++++++++++++++++++++++++- test-requirements.txt | 1 + 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/adaptive/tests/test_runner.py b/adaptive/tests/test_runner.py index a402f47c..3e9c0bd2 100644 --- a/adaptive/tests/test_runner.py +++ b/adaptive/tests/test_runner.py @@ -5,7 +5,8 @@ import asyncio import pytest from ..learner import Learner1D, Learner2D -from ..runner import simple, BlockingRunner, AsyncRunner, SequentialExecutor +from ..runner import (simple, BlockingRunner, AsyncRunner, SequentialExecutor, + with_ipyparallel, with_distributed) def blocking_runner(learner, goal): @@ -19,15 +20,18 @@ def async_runner(learner, goal): runners = [simple, blocking_runner, async_runner] + def trivial_goal(learner): return learner.npoints > 10 + @pytest.mark.parametrize('runner', runners) def test_simple(runner): """Test that the runners actually run.""" def f(x): return x + learner = Learner1D(f, (-1, 1)) runner(learner, lambda l: l.npoints > 10) assert len(learner.data) > 10 @@ -54,3 +58,52 @@ def test_aync_def_function(): learner = Learner1D(f, (-1, 1)) runner = AsyncRunner(learner, trivial_goal) asyncio.get_event_loop().run_until_complete(runner.task) + + +### Test with different executors + +@pytest.fixture(scope="session") +def ipyparallel_executor(): + from ipyparallel import Client + import pexpect + + child = pexpect.spawn('ipcluster start -n 1') + child.expect('Engines appear to have started successfully', timeout=35) + yield Client() + if not child.terminate(force=True): + raise RuntimeError('Could not stop ipcluster') + + +@pytest.fixture(scope="session") +def dask_executor(): + from distributed import LocalCluster, Client + + client = Client(n_workers=1) + yield client + client.close() + + +def linear(x): + return x + + +def test_concurrent_futures_executor(): + from concurrent.futures import ProcessPoolExecutor + BlockingRunner(Learner1D(linear, (-1, 1)), trivial_goal, + executor=ProcessPoolExecutor(max_workers=1)) + + +@pytest.mark.skipif(not with_ipyparallel, reason='IPyparallel is not installed') +def test_ipyparallel_executor(ipyparallel_executor): + learner = Learner1D(linear, (-1, 1)) + BlockingRunner(learner, trivial_goal, + executor=ipyparallel_executor) + assert learner.npoints > 0 + + +@pytest.mark.skipif(not with_distributed, reason='dask.distributed is not installed') +def test_distributed_executor(dask_executor): + learner = Learner1D(linear, (-1, 1)) + BlockingRunner(learner, trivial_goal, + executor=dask_executor) + assert learner.npoints > 0 diff --git a/test-requirements.txt b/test-requirements.txt index 6f055500..47cc784e 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,3 +1,4 @@ pytest pytest-randomly pytest-cov +pexpect -- GitLab