Пример #1
0
    def testPromise(self):
        promises = weakref.WeakValueDictionary()
        req_queue = Queue()
        value_list = []

        time_unit = 0.1

        def test_thread_body():
            while True:
                idx, v, success = req_queue.get()
                if v is None:
                    break
                value_list.append(('thread_body', v))
                time.sleep(time_unit)
                promises[idx].step_next([(v, ), dict(_accept=success)])

        try:
            thread = threading.Thread(target=test_thread_body)
            thread.daemon = True
            thread.start()

            def gen_promise(value, accept=True):
                value_list.append(('gen_promise', value))
                p = promise.Promise()
                promises[p.id] = p
                req_queue.put((p.id, value + 1, accept))
                return p

            # simple promise call
            value_list = []
            p = gen_promise(0) \
                .then(lambda v: gen_promise(v)) \
                .then(lambda v: gen_promise(v))
            p.wait()
            self.assertListEqual(value_list, [('gen_promise', 0),
                                              ('thread_body', 1),
                                              ('gen_promise', 1),
                                              ('thread_body', 2),
                                              ('gen_promise', 2),
                                              ('thread_body', 3)])

            # continue accepted call with then
            value_list = []
            p.then(lambda *_: gen_promise(0)) \
                .then(lambda v: gen_promise(v)) \
                .wait()
            self.assertListEqual(value_list, [('gen_promise', 0),
                                              ('thread_body', 1),
                                              ('gen_promise', 1),
                                              ('thread_body', 2)])

            # immediate error
            value_list = []
            p = promise.finished() \
                .then(lambda *_: 5 / 0)
            p.catch(lambda *_: gen_promise(0)) \
                .wait()
            self.assertListEqual(value_list, [('gen_promise', 0),
                                              ('thread_body', 1)])

            # chained errors
            value_list = []
            p = promise.finished(_accept=False) \
                .catch(lambda *_: 1 / 0) \
                .catch(lambda *_: 2 / 0) \
                .catch(lambda *_: gen_promise(0)) \
                .catch(lambda *_: gen_promise(1))
            p.wait()
            self.assertListEqual(value_list, [('gen_promise', 0),
                                              ('thread_body', 1)])

            # continue error call
            value_list = []
            p = gen_promise(0) \
                .then(lambda *_: 5 / 0) \
                .then(lambda *_: gen_promise(2))
            time.sleep(0.5)
            value_list = []
            p.catch(lambda *_: gen_promise(0)) \
                .wait()
            self.assertListEqual(value_list, [('gen_promise', 0),
                                              ('thread_body', 1)])

            value_list = []
            gen_promise(0) \
                .then(lambda v: gen_promise(v).then(lambda x: x + 1)) \
                .then(lambda v: gen_promise(v)) \
                .wait()
            self.assertListEqual(value_list, [('gen_promise', 0),
                                              ('thread_body', 1),
                                              ('gen_promise', 1),
                                              ('thread_body', 2),
                                              ('gen_promise', 3),
                                              ('thread_body', 4)])

            value_list = []
            gen_promise(0) \
                .then(lambda v: gen_promise(v)) \
                .then(lambda v: gen_promise(v, False)) \
                .catch(lambda v: value_list.append(('catch', v))) \
                .wait()
            self.assertListEqual(value_list, [('gen_promise', 0),
                                              ('thread_body', 1),
                                              ('gen_promise', 1),
                                              ('thread_body', 2),
                                              ('gen_promise', 2),
                                              ('thread_body', 3),
                                              ('catch', 3)])

            value_list = []
            gen_promise(0) \
                .then(lambda v: gen_promise(v, False).then(lambda x: x + 1)) \
                .then(lambda v: gen_promise(v)) \
                .catch(lambda v: value_list.append(('catch', v))) \
                .wait()
            self.assertListEqual(value_list, [('gen_promise', 0),
                                              ('thread_body', 1),
                                              ('gen_promise', 1),
                                              ('thread_body', 2),
                                              ('catch', 2)])

            value_list = []
            gen_promise(0) \
                .then(lambda v: gen_promise(v)) \
                .then(lambda v: v + 1) \
                .then(lambda v: gen_promise(v, False)) \
                .catch(lambda v: value_list.append(('catch', v))) \
                .wait()
            self.assertListEqual(value_list, [('gen_promise', 0),
                                              ('thread_body', 1),
                                              ('gen_promise', 1),
                                              ('thread_body', 2),
                                              ('gen_promise', 3),
                                              ('thread_body', 4),
                                              ('catch', 4)])

            value_list = []
            gen_promise(0) \
                .then(lambda v: gen_promise(v)) \
                .catch(lambda v: gen_promise(v)) \
                .catch(lambda v: gen_promise(v)) \
                .then(lambda v: gen_promise(v, False)) \
                .catch(lambda v: value_list.append(('catch', v))) \
                .wait()
            self.assertListEqual(value_list, [('gen_promise', 0),
                                              ('thread_body', 1),
                                              ('gen_promise', 1),
                                              ('thread_body', 2),
                                              ('gen_promise', 2),
                                              ('thread_body', 3),
                                              ('catch', 3)])

            value_list = []
            gen_promise(0) \
                .then(lambda v: gen_promise(v)) \
                .catch(lambda v: gen_promise(v)) \
                .catch(lambda v: gen_promise(v)) \
                .then(lambda v: gen_promise(v, False)) \
                .then(lambda v: gen_promise(v), lambda v: gen_promise(v + 1, False)) \
                .catch(lambda v: value_list.append(('catch', v))) \
                .wait()
            self.assertListEqual(value_list, [('gen_promise', 0),
                                              ('thread_body', 1),
                                              ('gen_promise', 1),
                                              ('thread_body', 2),
                                              ('gen_promise', 2),
                                              ('thread_body', 3),
                                              ('gen_promise', 4),
                                              ('thread_body', 5),
                                              ('catch', 5)])

            value_list = []
            gen_promise(0) \
                .then(lambda v: gen_promise(v)) \
                .catch(lambda v: gen_promise(v)) \
                .catch(lambda v: gen_promise(v)) \
                .then(lambda v: gen_promise(v, False)) \
                .then(lambda v: gen_promise(v), lambda v: _raise_exception(ValueError)) \
                .catch(lambda *_: value_list.append(('catch',))) \
                .wait()
            self.assertListEqual(value_list, [('gen_promise', 0),
                                              ('thread_body', 1),
                                              ('gen_promise', 1),
                                              ('thread_body', 2),
                                              ('gen_promise', 2),
                                              ('thread_body', 3), ('catch', )])

            value_list = []
            gen_promise(0) \
                .then(lambda v: gen_promise(v, False)) \
                .catch(lambda v: gen_promise(v, False)) \
                .catch(lambda v: gen_promise(v)) \
                .then(lambda v: gen_promise(v)) \
                .catch(lambda v: value_list.append(('catch', v))) \
                .wait()
            self.assertListEqual(value_list, [('gen_promise', 0),
                                              ('thread_body', 1),
                                              ('gen_promise', 1),
                                              ('thread_body', 2),
                                              ('gen_promise', 2),
                                              ('thread_body', 3),
                                              ('gen_promise', 3),
                                              ('thread_body', 4),
                                              ('gen_promise', 4),
                                              ('thread_body', 5)])

            value_list = []
            gen_promise(0) \
                .then(lambda v: gen_promise(v, False)) \
                .then(lambda v: gen_promise(v)) \
                .catch(lambda v: value_list.append(('catch', v))) \
                .wait()
            self.assertListEqual(value_list, [('gen_promise', 0),
                                              ('thread_body', 1),
                                              ('gen_promise', 1),
                                              ('thread_body', 2),
                                              ('catch', 2)])
        finally:
            self.assertDictEqual(promise._promise_pool, {})
            req_queue.put((None, None, None))
Пример #2
0
    def testAll(self):
        promises = weakref.WeakValueDictionary()
        req_queue = Queue()
        value_list = []

        time_unit = 0.1

        def test_thread_body():
            while True:
                idx, v, success = req_queue.get()
                if v is None:
                    break
                value_list.append(('thread_body', v))
                time.sleep(time_unit)
                promises[idx].step_next([(v, ), dict(_accept=success)])

        def gen_promise(value, accept=True):
            p = promise.Promise()
            promises[p.id] = p
            req_queue.put((p.id, value + 1, accept))
            return p

        try:
            thread = threading.Thread(target=test_thread_body)
            thread.daemon = True
            thread.start()

            value_list = []
            promise.all_([]).then(lambda: value_list.append(('all', 0))).wait()
            self.assertListEqual(value_list, [('all', 0)])

            value_list = []
            prior_promises = [gen_promise(idx) for idx in range(4)]
            promise.all_(prior_promises).then(lambda: value_list.append(
                ('all', 5))).wait()
            del prior_promises

            self.assertListEqual(value_list, [('thread_body', 1),
                                              ('thread_body', 2),
                                              ('thread_body', 3),
                                              ('thread_body', 4), ('all', 5)])

            value_list = []
            prior_promises = [
                gen_promise(idx, bool((idx + 1) % 2)) for idx in range(4)
            ]
            promise.all_(prior_promises).then(
                lambda: value_list.append(('all', 5)),
                lambda *_: value_list.append(('all_catch', 5)),
            ).wait()
            del prior_promises

            expected = [('thread_body', 1), ('thread_body', 2),
                        ('all_catch', 5)]
            self.assertListEqual(value_list[:len(expected)], expected)
            time.sleep(0.5)

            def _gen_all_promise(*_):
                prior_promises = [
                    gen_promise(idx, bool((idx + 1) % 2)) for idx in range(4)
                ]
                return promise.all_(prior_promises)

            value_list = []
            gen_promise(0) \
                .then(lambda *_: value_list.append(('pre_all', 0))) \
                .then(_gen_all_promise) \
                .then(lambda v: gen_promise(v)) \
                .then(
                lambda: value_list.append(('all', 5)),
                lambda *_: value_list.append(('all_catch', 5)),
            ).wait()
            expected = [('thread_body', 1), ('pre_all', 0), ('thread_body', 1),
                        ('thread_body', 2), ('all_catch', 5)]
            self.assertListEqual(value_list[:len(expected)], expected)
            time.sleep(0.5)
        finally:
            self.assertEqual(promise.get_active_promise_count(), 0)
            req_queue.put((None, None, None))
Пример #3
0
    def testPromise(self):
        promises = weakref.WeakValueDictionary()
        req_queue = Queue()
        value_list = []

        time_unit = 0.1

        def test_thread_body():
            while True:
                idx, v, success = req_queue.get()
                if v is None:
                    break
                value_list.append(('thread_body', v))
                time.sleep(time_unit)
                promises[idx].step_next(v, _accept=success)

        try:
            thread = threading.Thread(target=test_thread_body)
            thread.daemon = True
            thread.start()

            def gen_promise(value, accept=True):
                value_list.append(('gen_promise', value))
                p = promise.Promise()
                promises[p.id] = p
                req_queue.put((p.id, value + 1, accept))
                return p

            value_list = []
            gen_promise(0) \
                .then(lambda v: gen_promise(v)) \
                .then(lambda v: gen_promise(v)) \
                .wait()
            self.assertListEqual(value_list, [('gen_promise', 0),
                                              ('thread_body', 1),
                                              ('gen_promise', 1),
                                              ('thread_body', 2),
                                              ('gen_promise', 2),
                                              ('thread_body', 3)])

            value_list = []
            gen_promise(0) \
                .then(lambda v: gen_promise(v).then(lambda x: x + 1)) \
                .then(lambda v: gen_promise(v)) \
                .wait()
            self.assertListEqual(value_list, [('gen_promise', 0),
                                              ('thread_body', 1),
                                              ('gen_promise', 1),
                                              ('thread_body', 2),
                                              ('gen_promise', 3),
                                              ('thread_body', 4)])

            value_list = []
            gen_promise(0) \
                .then(lambda v: gen_promise(v)) \
                .then(lambda v: gen_promise(v, False)) \
                .catch(lambda v: value_list.append(('catch', v))) \
                .wait()
            self.assertListEqual(value_list, [('gen_promise', 0),
                                              ('thread_body', 1),
                                              ('gen_promise', 1),
                                              ('thread_body', 2),
                                              ('gen_promise', 2),
                                              ('thread_body', 3),
                                              ('catch', 3)])

            value_list = []
            gen_promise(0) \
                .then(lambda v: gen_promise(v, False).then(lambda x: x + 1)) \
                .then(lambda v: gen_promise(v)) \
                .catch(lambda v: value_list.append(('catch', v))) \
                .wait()
            self.assertListEqual(value_list, [('gen_promise', 0),
                                              ('thread_body', 1),
                                              ('gen_promise', 1),
                                              ('thread_body', 2),
                                              ('catch', 2)])

            value_list = []
            gen_promise(0) \
                .then(lambda v: gen_promise(v)) \
                .then(lambda v: v + 1) \
                .then(lambda v: gen_promise(v, False)) \
                .catch(lambda v: value_list.append(('catch', v))) \
                .wait()
            self.assertListEqual(value_list, [('gen_promise', 0),
                                              ('thread_body', 1),
                                              ('gen_promise', 1),
                                              ('thread_body', 2),
                                              ('gen_promise', 3),
                                              ('thread_body', 4),
                                              ('catch', 4)])

            value_list = []
            gen_promise(0) \
                .then(lambda v: gen_promise(v)) \
                .catch(lambda v: gen_promise(v)) \
                .catch(lambda v: gen_promise(v)) \
                .then(lambda v: gen_promise(v, False)) \
                .catch(lambda v: value_list.append(('catch', v))) \
                .wait()
            self.assertListEqual(value_list, [('gen_promise', 0),
                                              ('thread_body', 1),
                                              ('gen_promise', 1),
                                              ('thread_body', 2),
                                              ('gen_promise', 2),
                                              ('thread_body', 3),
                                              ('catch', 3)])

            value_list = []
            gen_promise(0) \
                .then(lambda v: gen_promise(v)) \
                .catch(lambda v: gen_promise(v)) \
                .catch(lambda v: gen_promise(v)) \
                .then(lambda v: gen_promise(v, False)) \
                .then(lambda v: gen_promise(v), lambda v: gen_promise(v + 1, False)) \
                .catch(lambda v: value_list.append(('catch', v))) \
                .wait()
            self.assertListEqual(value_list, [('gen_promise', 0),
                                              ('thread_body', 1),
                                              ('gen_promise', 1),
                                              ('thread_body', 2),
                                              ('gen_promise', 2),
                                              ('thread_body', 3),
                                              ('gen_promise', 4),
                                              ('thread_body', 5),
                                              ('catch', 5)])

            value_list = []
            gen_promise(0) \
                .then(lambda v: gen_promise(v)) \
                .catch(lambda v: gen_promise(v)) \
                .catch(lambda v: gen_promise(v)) \
                .then(lambda v: gen_promise(v, False)) \
                .then(lambda v: gen_promise(v), lambda v: _raise_exception(ValueError)) \
                .catch(lambda *_: value_list.append(('catch',))) \
                .wait()
            self.assertListEqual(value_list, [('gen_promise', 0),
                                              ('thread_body', 1),
                                              ('gen_promise', 1),
                                              ('thread_body', 2),
                                              ('gen_promise', 2),
                                              ('thread_body', 3), ('catch', )])

            value_list = []
            gen_promise(0) \
                .then(lambda v: gen_promise(v, False)) \
                .catch(lambda v: gen_promise(v, False)) \
                .catch(lambda v: gen_promise(v)) \
                .then(lambda v: gen_promise(v)) \
                .catch(lambda v: value_list.append(('catch', v))) \
                .wait()
            self.assertListEqual(value_list, [('gen_promise', 0),
                                              ('thread_body', 1),
                                              ('gen_promise', 1),
                                              ('thread_body', 2),
                                              ('gen_promise', 2),
                                              ('thread_body', 3),
                                              ('gen_promise', 3),
                                              ('thread_body', 4),
                                              ('gen_promise', 4),
                                              ('thread_body', 5)])

            value_list = []
            gen_promise(0) \
                .then(lambda v: gen_promise(v, False)) \
                .then(lambda v: gen_promise(v)) \
                .catch(lambda v: value_list.append(('catch', v))) \
                .wait()
            self.assertListEqual(value_list, [('gen_promise', 0),
                                              ('thread_body', 1),
                                              ('gen_promise', 1),
                                              ('thread_body', 2),
                                              ('catch', 2)])
        finally:
            gc.collect()
            self.assertDictEqual(promise._promise_pool, {})
            req_queue.put((None, None, None))