55import pytest
66
77from ..learner import Learner1D , Learner2D
8- from ..runner import simple , BlockingRunner , AsyncRunner , SequentialExecutor
8+ from ..runner import (simple , BlockingRunner , AsyncRunner , SequentialExecutor ,
9+ with_ipyparallel , with_distributed )
910
1011
1112def blocking_runner (learner , goal ):
@@ -19,15 +20,18 @@ def async_runner(learner, goal):
1920
2021runners = [simple , blocking_runner , async_runner ]
2122
23+
2224def trivial_goal (learner ):
2325 return learner .npoints > 10
2426
27+
2528@pytest .mark .parametrize ('runner' , runners )
2629def test_simple (runner ):
2730 """Test that the runners actually run."""
2831
2932 def f (x ):
3033 return x
34+
3135 learner = Learner1D (f , (- 1 , 1 ))
3236 runner (learner , lambda l : l .npoints > 10 )
3337 assert len (learner .data ) > 10
@@ -54,3 +58,52 @@ async def f(x):
5458 learner = Learner1D (f , (- 1 , 1 ))
5559 runner = AsyncRunner (learner , trivial_goal )
5660 asyncio .get_event_loop ().run_until_complete (runner .task )
61+
62+
63+ ### Test with different executors
64+
65+ @pytest .fixture (scope = "session" )
66+ def ipyparallel_executor ():
67+ from ipyparallel import Client
68+ import pexpect
69+
70+ child = pexpect .spawn ('ipcluster start -n 1' )
71+ child .expect ('Engines appear to have started successfully' , timeout = 35 )
72+ yield Client ()
73+ if not child .terminate (force = True ):
74+ raise RuntimeError ('Could not stop ipcluster' )
75+
76+
77+ @pytest .fixture (scope = "session" )
78+ def dask_executor ():
79+ from distributed import LocalCluster , Client
80+
81+ client = Client (n_workers = 1 )
82+ yield client
83+ client .close ()
84+
85+
86+ def linear (x ):
87+ return x
88+
89+
90+ def test_concurrent_futures_executor ():
91+ from concurrent .futures import ProcessPoolExecutor
92+ BlockingRunner (Learner1D (linear , (- 1 , 1 )), trivial_goal ,
93+ executor = ProcessPoolExecutor (max_workers = 1 ))
94+
95+
96+ @pytest .mark .skipif (not with_ipyparallel , reason = 'IPyparallel is not installed' )
97+ def test_ipyparallel_executor (ipyparallel_executor ):
98+ learner = Learner1D (linear , (- 1 , 1 ))
99+ BlockingRunner (learner , trivial_goal ,
100+ executor = ipyparallel_executor )
101+ assert learner .npoints > 0
102+
103+
104+ @pytest .mark .skipif (not with_distributed , reason = 'dask.distributed is not installed' )
105+ def test_distributed_executor (dask_executor ):
106+ learner = Learner1D (linear , (- 1 , 1 ))
107+ BlockingRunner (learner , trivial_goal ,
108+ executor = dask_executor )
109+ assert learner .npoints > 0
0 commit comments