def test_basic(self): ident = Identity() state = State(x=1, y='a', z=[1, 2, 3]) inp = copy.deepcopy(state) out = ident.run(state).result() self.assertEqual(inp, out) self.assertFalse(out is inp)
def test_mimo(self): """Parallel should support branches with States inputs""" inp = States(State(x=1), State(x=2)) wrk = ParallelBranches(Identity(), Identity(), Identity()) out = wrk.run(inp).result() self.assertEqual(len(out), 3) self.assertIsInstance(out, States) self.assertEqual(out.first, inp)
def test_stopped(self): class Fast(Runnable): def next(self, state, **runopts): time.sleep(0.1) return state.updated(x=state.x + 1) class Slow(Runnable): def init(self, state, **runopts): self.time_to_stop = threading.Event() def next(self, state, **runopts): self.time_to_stop.wait() return state.updated(x=state.x + 2) def halt(self): self.time_to_stop.set() # default case rb = RacingBranches(Slow(), Fast(), Slow()) res = rb.run(State(x=0)).result() self.assertEqual([s.x for s in res], [2, 1, 2]) # "endomorphic case" rb = RacingBranches(Identity(), Slow(), Fast(), Slow()) res = rb.run(State(x=0)).result() self.assertEqual([s.x for s in res], [0, 2, 1, 2])
def test_interruptable(self): ident = Identity() state = State(x=1) out = ident.run(state, racing_context=True) # ident block should not finish done, not_done = futures.wait({out}, timeout=0.1) self.assertEqual(len(done), 0) self.assertEqual(len(not_done), 1) # until we stop it ident.stop() done, not_done = futures.wait({out}, timeout=0.1) self.assertEqual(len(done), 1) self.assertEqual(len(not_done), 0) self.assertEqual(out.result().x, 1)
def test_error_prop(self): class ErrorSilencer(Runnable): def next(self, state): return state def error(self, exc): return State(error=True) class Identity(Runnable): def next(self, state): return state branch = ErrorSilencer() | Identity() s1 = Present(exception=KeyError()) s2 = branch.run(s1).result() self.assertEqual(s2.error, True)
def test_basic(self): class Fast(Runnable): def next(self, state): time.sleep(0.1) return state.updated(x=state.x + 1) class Slow(Runnable): def next(self, state): time.sleep(0.2) return state.updated(x=state.x + 2) # "endomorphic case" pb = ParallelBranches(Identity(), Slow(), Fast(), Slow()) res = pb.run(State(x=0)).result() self.assertEqual([s.x for s in res], [0, 2, 1, 2]) # default case pb = ParallelBranches(Slow(), Fast(), Slow()) res = pb.run(State(x=0)).result() self.assertEqual([s.x for s in res], [2, 1, 2])
def test_input_type_invariant(self): inp1 = State(x=1) self.assertEqual(Identity().run(inp1).result(), inp1) inp2 = States(State(x=1), State(x=2)) self.assertEqual(Identity().run(inp2).result(), inp2)