Exemplo n.º 1
0
    def test_basic(self):
        ident = BlockingIdentity()
        state = State(x=1, y='a', z=[1, 2, 3])

        inp = copy.deepcopy(state)
        fut = ident.run(state)
        ident.stop()
        out = fut.result()

        self.assertEqual(inp, out)
        self.assertFalse(out is inp)
Exemplo n.º 2
0
    def test_input_type_invariant(self):
        inp = State(x=1)
        ii = BlockingIdentity()
        fut = ii.run(inp)
        ii.stop()
        out = fut.result()
        self.assertEqual(out, inp)

        inp = States(State(x=1), State(x=2))
        ii = BlockingIdentity()
        fut = ii.run(inp)
        ii.stop()
        out = fut.result()
        self.assertEqual(out, inp)
Exemplo n.º 3
0
    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(BlockingIdentity(), Slow(), Fast(), Slow())
        res = rb.run(State(x=0)).result()
        self.assertEqual([s.x for s in res], [0, 2, 1, 2])
Exemplo n.º 4
0
    def test_interruptable(self):
        ident = BlockingIdentity()
        state = State(x=1)
        out = ident.run(state)

        # 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)