Beispiel #1
0
    def testSpawnPromisedActor(self):
        try:
            with create_actor_pool(n_process=1) as pool:
                serve_ref = pool.create_actor(ServeActor, uid='ServeActor')
                test_ref = pool.create_actor(PromiseTestActor)

                start_time = time.time()
                test_ref.test_spawn()
                self.assertLess(time.time() - start_time, 0.1)
                wait_test_actor_result(test_ref, 30)
                self.assertEqual(serve_ref.get_result(), [0])

                self.assertGreaterEqual(time.time() - start_time, 0.1)
                self.assertLess(time.time() - start_time, 0.3)

                serve_ref.clear_result()
                test_ref.reset_finished()

                start_time = time.time()
                test_ref.test_spawn(raises=True)
                self.assertLess(time.time() - start_time, 0.1)
                wait_test_actor_result(test_ref, 30)
                self.assertEqual(serve_ref.get_result(), ['SystemError'])

                self.assertGreaterEqual(time.time() - start_time, 0.1)
                self.assertLess(time.time() - start_time, 0.3)
        finally:
            self.assertEqual(promise.get_active_promise_count(), 0)
Beispiel #2
0
    def testClosureRefcount(self):
        try:
            with create_actor_pool(n_process=1) as pool:
                serve_ref = pool.create_actor(ServeActor, uid='ServeActor')
                test_ref = pool.create_actor(PromiseTestActor)

                test_ref.test_closure_refcount()
                wait_test_actor_result(test_ref, 30)
                self.assertIsNone(serve_ref.get_result()[-1]())
        finally:
            self.assertEqual(promise.get_active_promise_count(), 0)
Beispiel #3
0
    def testAddrReject(self):
        try:
            with create_actor_pool(n_process=1) as pool:
                serve_ref = pool.create_actor(ServeActor, uid='ServeActor')
                test_ref = pool.create_actor(PromiseTestActor)

                test_ref.test_addr_reject()

                wait_test_actor_result(test_ref, 30)
                self.assertListEqual(serve_ref.get_result(), [0, 'WorkerDead'])
        finally:
            self.assertEqual(promise.get_active_promise_count(), 0)
Beispiel #4
0
    def testNoTimeoutActor(self):
        try:
            with create_actor_pool(n_process=1) as pool:
                serve_ref = pool.create_actor(ServeActor, uid='ServeActor')
                test_ref = pool.create_actor(PromiseTestActor)

                test_ref.test_no_timeout()

                wait_test_actor_result(test_ref, 30)

                self.assertListEqual(serve_ref.get_result(), [0])
        finally:
            self.assertEqual(promise.get_active_promise_count(), 0)
Beispiel #5
0
    def testAllActor(self):
        try:
            with create_actor_pool(n_process=1) as pool:
                serve_ref = pool.create_actor(ServeActor, uid='ServeActor')
                test_ref = pool.create_actor(PromiseTestActor)

                test_ref.test_all_promise()

                wait_test_actor_result(test_ref, 30)
                self.assertListEqual(serve_ref.get_result(),
                                     [-128] + list(range(0, 20, 2)) +
                                     list(range(1, 20, 2)) + [127])
        finally:
            self.assertEqual(promise.get_active_promise_count(), 0)
Beispiel #6
0
    def testPromiseActor(self):
        try:
            with create_actor_pool(n_process=1) as pool:
                serve_ref = pool.create_actor(ServeActor, uid='ServeActor')
                test_ref = pool.create_actor(PromiseTestActor)

                test_ref.test_normal()
                wait_test_actor_result(test_ref, 10)
                self.assertListEqual(serve_ref.get_result(), list(range(11)))

                serve_ref.clear_result()
                test_ref.reset_finished()

                test_ref.test_error_raise()
                wait_test_actor_result(test_ref, 10)
                self.assertListEqual(serve_ref.get_result(), [-1])
        finally:
            self.assertEqual(promise.get_active_promise_count(), 0)
Beispiel #7
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))