def test_all_schedulers(): for scheduler in (ThreadedScheduler(max_threads=8), LinearizedScheduler(), NaiveScheduler()): for wf_scheduler in (ThreadedScheduler(max_threads=8), LinearizedScheduler(), NaiveScheduler()): for case in (_call_workflow, _run_workflow, _test_workflow_chain): yield case, scheduler, wf_scheduler
def test_two_to_one_connected(): """Two converging lines. --func1(.)--+ | +-func2(.,.)-- | --func1(.)--+ """ def func1(x): return x * 2 def func2(x, y): return x + y in_actor1 = FuncActor(func1, outports=('a', )) in_actor2 = FuncActor(func1, outports=('a', )) out_actor = FuncActor(func2, outports=('a', )) out_actor.inports['x'] += in_actor1.outports['a'] out_actor.inports['y'] += in_actor2.outports['a'] in_value1 = 1 in_value2 = 2 in_actor2.inports['x'].put(in_value2) in_actor1.inports['x'].put(in_value1) NaiveScheduler().run_actor(in_actor1) NaiveScheduler().run_actor(in_actor2) assert func2(func1(in_value1), func1(in_value2)) == out_actor.outports['a'].pop()
def test_ignore_PEP_484_annotations(): def func(x, y) -> int: return int(x + y + 2) x, y = 2, 3 fa = FuncActor(func) fa.inports.x.put(x) fa.inports.y.put(y) NaiveScheduler().run_actor(fa) res = func(x, y) assert (fa.outports.out.pop() == res) # test with a given outport name fa = FuncActor(func, outports=('o', )) fa.inports.x.put(x) fa.inports.y.put(y) NaiveScheduler().run_actor(fa) res = func(x, y) assert (fa.outports.o.pop() == res)
def test_LoopWhileActor(): def condition(x): return x < 10 def func(x): return x + 1 fa = FuncActor(func, outports=('x', )) lw = LoopWhile("a_loop", condition_func=condition) fa.inports['x'] += lw.outports['loop'] lw.inports['loop'] += fa.outports['x'] lw.inports['init'].put(0) NaiveScheduler().run_actor(lw) result = lw.outports['exit'].pop() assert (result == 10) # test with condition actor fa = FuncActor(func, outports=('x', )) ca = FuncActor(condition, outports=('out', )) lw = LoopWhile("a_loop") lw.inports['condition_out'] += ca.outports['out'] ca.inports['x'] += lw.outports['condition_in'] fa.inports['x'] += lw.outports['loop'] lw.inports['loop'] += fa.outports['x'] lw.inports['init'].put(0) NaiveScheduler().run_actor(lw) result = lw.outports['exit'].pop() assert (result == 10)
def test_SwitchActorWorkflow(): for val1 in (True, False): for val2 in (True, False): token = random.randint(0, 100) sw1 = Switch("switch", lambda x: val1) sw2 = Switch("switch", lambda x: val2) pname1 = 'true' if val1 else 'false' sink = Sink() sink.inports.append('inp') if val1: sw2.inports['inp'] += sw1.outports['true'] sink.inports['inp'] += sw1.outports['false'] else: sw2.inports['inp'] += sw1.outports['false'] sink.inports['inp'] += sw1.outports['true'] pname2 = 'true' if val2 else 'false' wf = sw2.get_workflow() NaiveScheduler().run_workflow(wf, inp=token) if val2: assert wf.outports['true'].pop() == token assert wf.outports['false'].isempty() else: assert wf.outports['false'].pop() == token assert wf.outports['true'].isempty()
def test_Shellrunner(): import platform command = "echo" arg = "test" if platform.system() == 'Windows': runner = ShellRunner(command, shell=True) else: runner = ShellRunner(command, shell=False) runner.inports['inp'].put(arg) NaiveScheduler().run_actor(runner) rvalue = runner.outports['ret'].pop() stdout = runner.outports['stdout'].pop() stderr = runner.outports['stderr'].pop() print("Return value: ", rvalue) print("Std out: ", stdout.strip()) print("Std err: ", stderr.strip()) assert (rvalue == 0) assert (stdout.strip() == arg) assert (stderr.strip() == "")
def test_DictionaryMerge(): d = DictionaryMerge(inport_names=("a", "b")) d.inports["a"].put("aa") d.inports["b"].put("bb") NaiveScheduler().run_actor(d) out = d.outports["out"].pop() assert (len(out) == 2) assert (out["a"] == "aa") assert (out["b"] == "bb")
def test_SwitchActor(): for val in (True, False): token = random.randint(0, 100) sw = Switch("switch", lambda x: val) sw.inports['inp'].put(token) pname = 'true' if val else 'false' NaiveScheduler().run_actor(sw) assert sw.outports[pname].pop() == token assert not sw._in_condition for val in (True, False): token = random.randint(0, 100) sw = Switch("switch") ca = FuncActor(lambda x: val) sw.inports['condition_out'] += ca.outports['out'] ca.inports['x'] += sw.outports['condition_in'] sw.inports['inp'].put(token) pname = 'true' if val else 'false' NaiveScheduler().run_actor(sw) assert sw.outports[pname].pop() == token assert not sw._in_condition
def test_FuncActor_partial_kwargs(): def func(x, y): return x + 1, y + 2 x, y = 2, 3.1 fa = FuncActor(func, kwargs={'y': y}, outports=('a', 'b')) fa.inports.x.put(x) NaiveScheduler().run_actor(fa) a, b = func(x, y) assert (fa.outports.a.pop() == a) assert (fa.outports.b.pop() == b)
def test_FuncActor_return_annotation(): def func(x, y) -> ('a', 'b'): return x + 1, y + 2 x, y = 2, 3.1 f_actors = (FuncActor(func, outports=('a', 'b')), FuncActor(func)) for fa in f_actors: fa.inports.x.put(x) fa.inports.y.put(y) NaiveScheduler().run_actor(fa) a, b = func(x, y) assert (fa.outports.a.pop() == a) assert (fa.outports.b.pop() == b)
def test_two_connected(): """Two connected function actors. --func1(.)--func2(.)-- """ def func1(x): return x * 2 def func2(a): return a + 3 actor1 = FuncActor(func1, outports=('a', )) actor2 = FuncActor(func2, outports=('y', )) actor2.inports['a'] += actor1.outports['a'] for in_value in range(10): actor1.inports['x'].put(in_value) NaiveScheduler().run_actor(actor1) assert func2(func1(in_value)) == actor2.outports['y'].pop()
def test_three_in_line(): """Three linearly connected function actors. --func(.)--func(.)--func(.)-- """ def func(x): return x * 2 actor1 = FuncActor(func, outports=('x', )) actor2 = FuncActor(func, outports=('x', )) actor3 = FuncActor(func, outports=('x', )) actor2.inports['x'] += actor1.outports['x'] actor3.inports['x'] += actor2.outports['x'] in_value = 4 actor1.inports['x'].put(in_value) NaiveScheduler().run_actor(actor1) assert (func(func(func(in_value)))) == actor3.outports['x'].pop()
def test_splitter(): splitter = Splitter(multiplicity=2, inport_name="x") assert len(splitter.outports) == 2 scheduler = NaiveScheduler() for i in range(0, 10): scheduler.put_value(splitter.inports.x, i) scheduler.execute() x1_all = list(splitter.outports["x_1"].pop_all()) x2_all = list(splitter.outports["x_2"].pop_all()) print("x1:", x1_all) print("x2:", x2_all) assert [0, 2, 4, 6, 8] == x1_all assert [1, 3, 5, 7, 9] == x2_all