Пример #1
0
    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)
Пример #2
0
 def test_look_and_feel(self):
     br = Runnable(), Runnable()
     pb = ParallelBranches(*br)
     self.assertEqual(pb.name, 'ParallelBranches')
     self.assertEqual(str(pb), '(Runnable) & (Runnable)')
     self.assertEqual(repr(pb), 'ParallelBranches(Runnable(), Runnable())')
     self.assertEqual(tuple(pb), br)
Пример #3
0
    def test_parallel_independent_execution(self):
        class Component(Runnable):
            def __init__(self, runtime):
                super(Component, self).__init__()
                self.runtime = runtime
            def next(self, state):
                time.sleep(self.runtime)
                return state

        # make sure all branches really run in parallel
        pb = ParallelBranches(
            Component(1), Component(1), Component(1), Component(1), Component(1))
        with tictoc() as tt:
            pb.run(State()).result()

        # total runtime has to be smaller that the sum of individual runtimes
        self.assertTrue(1 <= tt.dt <= 2)
Пример #4
0
    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])
Пример #5
0
    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)

        # standard case (endomorphic; first output state is the input state)
        pb = ParallelBranches(Slow(), Fast(), Slow())
        res = pb.run(State(x=0)).result()
        self.assertEqual([s.x for s in res], [0, 2, 1, 2])

        # branches' outputs are of a different type that the inputs
        # (i.e. non-endomorphic branches)
        pb = ParallelBranches(Slow(), Fast(), Slow(), endomorphic=False)
        res = pb.run(State(x=0)).result()
        self.assertEqual([s.x for s in res], [2, 1, 2])