예제 #1
0
    def test_cancel_only_any(self):
        """
        Test that only futures "in" Any get cancelled if one of them fails
        """
        @coroutine
        def raises():
            raise RuntimeError()

        @coroutine
        def count():
            self.counter += 1

        @coroutine
        def main():
            count()
            try:
                fut1, fut2 = raises(), raises()
                yield AnyFuture(fut1, fut2)
            except RuntimeError:
                yield count()
                try:
                    yield fut1
                except RuntimeError:
                    yield count()

        ev = AsyncEventLoop()
        with ev:
            future = main()
        ev.execute_all_tasks()

        self.assertEqual(3, self.counter)
예제 #2
0
    def test_except_with_err_subtask(self):
        @task
        def count():
            self.counter += 1

        @count.do_finally
        def count():
            self.counter += 1

            @task
            def err():
                raise RuntimeError("Test")
            @err.do_except
            def err(err):
                if isinstance(err, RuntimeError):
                    self.counter += 1

            err()

        ev = AsyncEventLoop()
        with ev:
            count()
        ev.execute_all_tasks()

        self.assertEqual(3, self.counter)
예제 #3
0
    def test_cancel_except_finally(self):
        @task
        def raises():
            raise RuntimeError("Error")

        @task
        def other():
            self.counter -= 1
        @other.do_except
        def other(err):
            if isinstance(err, CancellationError):
                self.counter += 1
        @other.do_finally
        def other():
            self.counter += 1

        @task
        def main():
            raises()
            other()

        @main.do_except
        def main(err):
            self.counter += 1

        ev = AsyncEventLoop()
        with ev:
            main()
        ev.execute_all_tasks()

        self.assertEqual(3, self.counter)
예제 #4
0
 def test_two_tasks(self):
     ev = AsyncEventLoop()
     with ev:
         self.count()
         self.count()
     ev.execute_all_tasks()
     self.assertEqual(2, self.counter)
예제 #5
0
    def test_finally_reraise_subtask(self):
        @task
        def count():
            self.counter += 1

        @count.do_finally
        def count():
            self.counter += 1

            @task
            def err():
                raise RuntimeError("Test")

            @err.do_except
            def err(err):
                if isinstance(err, RuntimeError):
                    self.counter += 1
                raise err

            err()

        ev = AsyncEventLoop()
        with ev:
            count()
        ev.execute_all_tasks()

        self.assertEqual(3, self.counter)
예제 #6
0
    def test_except_and_finally_raise(self):
        @task
        def raises():
            raise RuntimeError("Error")

        @raises.do_except
        def raises(err):
            raise err

        @raises.do_finally
        def raises():
            raise ValueError("Finally wins")

        @task
        def main():
            raises()

        @main.do_except
        def main(err):
            if isinstance(err, ValueError):
                self.except_called = True
            elif isinstance(err, RuntimeError):
                self.except_called = False

        ev = AsyncEventLoop()
        with ev:
            main()
        ev.execute_all_tasks()

        self.assertTrue(self.except_called)
예제 #7
0
    def test_finally_with_subtask(self):
        @task
        def count():
            self.counter += 1

        @count.do_finally
        def count():
            self.counter += 1

            @task
            def count():
                self.counter += 1

            @count.do_finally
            def count():
                self.counter += 1

            count()

        ev = AsyncEventLoop()
        with ev:
            count()
        ev.execute_all_tasks()

        self.assertEqual(4, self.counter)
예제 #8
0
    def test_finally_with_err_subtask(self):
        @task
        def count():
            self.counter += 1

        @count.do_finally
        def count():
            self.counter += 1

            @task
            def err():
                raise RuntimeError("Test")

            @err.do_finally
            def err():
                self.counter += 1

            err()

        ev = AsyncEventLoop()
        with ev:
            count()
        ev.execute_all_tasks()

        self.assertEqual(3, self.counter)
예제 #9
0
    def test_catch(self):

        @coroutine
        def raises():
            raise RuntimeError("TestErr")

        @coroutine
        def count():
            self.counter += 1

        @coroutine
        def echo(inp):
            return inp

        @coroutine
        def main():
            try:
                future = count()
                yield raises()
            except RuntimeError:
                yield count()
                future = echo(1)
                result = yield echo(2)
                if result == 2:
                    self.counter += 1
                result = yield future
                if result == 1:
                    self.counter += 1

        ev = AsyncEventLoop()
        with ev:
            main()
        ev.execute_all_tasks()

        self.assertEqual(4, self.counter)
예제 #10
0
    def test_simple_class(self):

        class Main(object):

            def __init__(self):
                self.counter = 0

            @coroutine
            def count(self):
                self.counter += 1

            @coroutine
            def count_generator(self):
                if False: yield
                self.counter += 1

            @coroutine
            def main(self):
                future = self.count_generator()
                for i in range(3):
                    yield self.count()
                yield future

        ev = AsyncEventLoop()
        main = Main()
        with ev:
            future = main.main()

        ev.execute_all_tasks()
        self.assertTrue(isinstance(future, BaseFuture))
        self.assertEqual(4, main.counter)
예제 #11
0
    def test_except_and_finally_raise(self):
        @task
        def raises():
            raise RuntimeError("Error")
        @raises.do_except
        def raises(err):
            raise err
        @raises.do_finally
        def raises():
            raise ValueError("Finally wins")

        @task
        def main():
            raises()
        @main.do_except
        def main(err):
            if isinstance(err, ValueError):
                self.except_called = True
            elif isinstance(err, RuntimeError):
                self.except_called = False

        ev = AsyncEventLoop()
        with ev:
            main()
        ev.execute_all_tasks()

        self.assertTrue(self.except_called)
예제 #12
0
    def test_finally_reraise_catch_subtask(self):
        @task
        def count():
            self.counter += 1

        @count.do_finally
        def count():
            self.counter += 1

            @task
            def err():
                raise RuntimeError("Test")

            @err.do_except
            def err(err):
                if isinstance(err, RuntimeError):
                    self.counter += 1
                raise err

            err()

        @task
        def main():
            self.counter +=1
            count()
        @main.do_except
        def main(err):
            self.counter += 1

        ev = AsyncEventLoop()
        with ev:
            main()
        ev.execute_all_tasks()

        self.assertEqual(5, self.counter)
예제 #13
0
    def test_cancel_only_all(self):
        """
        Test that only futures "in" All get cancelled if one of them fails
        """
        @async
        def raises():
            raise RuntimeError()

        @async
        def count():
            self.counter += 1

        @async
        def main():
            count()
            try:
                yield (raises(), count())
            except RuntimeError:
                yield count()

        ev = AsyncEventLoop()
        with ev:
            future = main()
        ev.execute_all_tasks()

        self.assertEqual(2, self.counter)
예제 #14
0
 def test_two_tasks(self):
     ev = AsyncEventLoop()
     with ev:
         self.count()
         self.count()
     ev.execute_all_tasks()
     self.assertEqual(2, self.counter)
예제 #15
0
    def test_cancel_except_finally(self):
        @task
        def raises():
            raise RuntimeError("Error")

        @task
        def other():
            self.counter -= 1

        @other.do_except
        def other(err):
            if isinstance(err, CancellationError):
                self.counter += 1

        @other.do_finally
        def other():
            self.counter += 1

        @task
        def main():
            raises()
            other()

        @main.do_except
        def main(err):
            self.counter += 1

        ev = AsyncEventLoop()
        with ev:
            main()
        ev.execute_all_tasks()

        self.assertEqual(3, self.counter)
예제 #16
0
    def test_exception(self):
        future1 = BaseFuture()
        future2 = BaseFuture()

        ev = AsyncEventLoop()
        with ev:
            any_future = AnyFuture(future1, future2)
            future1.set_exception(RuntimeError())

        ev.execute_all_tasks()
        self.assertEqual(type(any_future.exception()), RuntimeError)
예제 #17
0
    def test_simple(self):
        future1 = BaseFuture()
        future2 = BaseFuture()

        ev = AsyncEventLoop()
        with ev:
            all_future = AllFuture(future1, future2)
            future2.set_result(2)
            future1.set_result(1)

        ev.execute_all_tasks()
        self.assertEqual(all_future.result(), (1, 2))
예제 #18
0
    def test_no_futures_yield_empty_tuple(self):
        @coroutine
        def main():
            results = yield AnyFuture()
            return_(results)

        ev = AsyncEventLoop()
        with ev:
            future = main()
        ev.execute_all_tasks()

        self.assertFalse(future.result())
예제 #19
0
    def test_simple(self):
        future1 = BaseFuture()
        future2 = BaseFuture()

        ev = AsyncEventLoop()
        with ev:
            any_future = AnyFuture(future1, future2)
            future2.set_result(3)

        ev.execute_all_tasks()
        self.assertEqual(any_future.result(), 3)
        self.assertFalse(future1.done())
예제 #20
0
    def test_recursive(self):
        ev = AsyncEventLoop()

        @task
        def recursive(ct=10):
            self.counter += 1
            if ct == 1:
                return
            ct -=1
            recursive(ct)

        with ev:
            recursive()
        ev.execute_all_tasks()
        self.assertEqual(10, self.counter)
예제 #21
0
    def test_recursive(self):
        ev = AsyncEventLoop()

        @task
        def recursive(ct=10):
            self.counter += 1
            if ct == 1:
                return
            ct -= 1
            recursive(ct)

        with ev:
            recursive()
        ev.execute_all_tasks()
        self.assertEqual(10, self.counter)
예제 #22
0
    def test_format(self):
        @task
        def task_func():
            raise RuntimeError("Test")

        @task_func.do_except
        def except_func(err):
            self.tb_str = "".join(format_exc())

        ev = AsyncEventLoop()
        with ev:
            task_func()
        ev.execute_all_tasks()

        self.assertTrue(self.tb_str)
        self.assertEqual(1, self.tb_str.count('---continuation---'))
예제 #23
0
    def test_format(self):
        @task
        def task_func():
            raise RuntimeError("Test")

        @task_func.do_except
        def except_func(err):
            self.tb_str = "".join(format_exc())

        ev = AsyncEventLoop()
        with ev:
            task_func()
        ev.execute_all_tasks()

        self.assertTrue(self.tb_str)
        self.assertEqual(1, self.tb_str.count('---continuation---'))
예제 #24
0
    def test_future(self):
        future = BaseFuture()
        @task
        def other():
            future.set_result(1)

        @task
        def main():
            other()

        ev = AsyncEventLoop()
        with ev:
            main()
        ev.execute_all_tasks()

        self.assertEqual(1, future.result())
예제 #25
0
    def test_raise_return(self):
        @coroutine
        def returns():
            return_("result")

        @coroutine
        def main():
            result = yield returns()
            if result == 'result':
                self.counter += 1

        ev = AsyncEventLoop()
        with ev:
            main()

        ev.execute_all_tasks()
        self.assertEqual(1, self.counter)
예제 #26
0
    def test_future(self):
        future = BaseFuture()

        @task
        def other():
            future.set_result(1)

        @task
        def main():
            other()

        ev = AsyncEventLoop()
        with ev:
            main()
        ev.execute_all_tasks()

        self.assertEqual(1, future.result())
예제 #27
0
    def test_external_future(self):
        future = BaseFuture()

        @coroutine
        def main():
            result = yield future
            self.counter += result

        ev = AsyncEventLoop()
        with ev:
            mainf = main()
        ev.execute_all_tasks()
        future.set_result(1)
        ev.execute_all_tasks()

        self.assertTrue(isinstance(future, BaseFuture))
        self.assertEqual(1, self.counter)
예제 #28
0
    def test_print(self):
        @task
        def task_func():
            raise RuntimeError("Test")

        @task_func.do_except
        def except_func(err):
            strfile = six.StringIO()
            print_exc(file=strfile)
            self.tb_str = strfile.getvalue()

        ev = AsyncEventLoop()
        with ev:
            task_func()
        ev.execute_all_tasks()

        self.assertTrue(self.tb_str)
        self.assertEqual(1, self.tb_str.count('---continuation---'))
예제 #29
0
    def test_print(self):
        @task
        def task_func():
            raise RuntimeError("Test")

        @task_func.do_except
        def except_func(err):
            strfile = six.StringIO()
            print_exc(file=strfile)
            self.tb_str = strfile.getvalue()

        ev = AsyncEventLoop()
        with ev:
            task_func()
        ev.execute_all_tasks()

        self.assertTrue(self.tb_str)
        self.assertEqual(1, self.tb_str.count('---continuation---'))
예제 #30
0
    def test_tasks_finally(self):
        @task
        def recursive(ct=10):
            if ct == 1:
                return
            ct -= 1
            recursive(ct)

        @recursive.do_finally
        def recursive():
            self.counter += 1

        ev = AsyncEventLoop()
        with ev:
            recursive()
        ev.execute_all_tasks()

        self.assertEqual(10, self.counter)
예제 #31
0
    def test_tasks_finally(self):
        @task
        def recursive(ct=10):
            if ct == 1:
                return
            ct -=1
            recursive(ct)

        @recursive.do_finally
        def recursive():
            self.counter += 1


        ev = AsyncEventLoop()
        with ev:
            recursive()
        ev.execute_all_tasks()

        self.assertEqual(10, self.counter)
예제 #32
0
    def test_async(self):
        @async
        def raises():
            raise RuntimeError("TestErr")

        @async
        def main():
            try:
                yield raises()
            except RuntimeError:
                self.tb_str = "".join(format_exc())

        ev = AsyncEventLoop()
        with ev:
            main()
        ev.execute_all_tasks()

        self.assertTrue(self.tb_str)
        self.assertEqual(2, self.tb_str.count('---continuation---'))
예제 #33
0
    def test_async(self):
        @async
        def raises():
            raise RuntimeError("TestErr")

        @async
        def main():
            try:
                yield raises()
            except RuntimeError:
                self.tb_str = "".join(format_exc())

        ev = AsyncEventLoop()
        with ev:
            main()
        ev.execute_all_tasks()

        self.assertTrue(self.tb_str)
        self.assertEqual(2, self.tb_str.count('---continuation---'))
예제 #34
0
    def test_async(self):
        # FIXME
        @coroutine
        def count():
            self.counter += 1

        @coroutine
        def raises():
            self.counter += 1
            raise RuntimeError("TestError")

        @coroutine
        def main():
            yield count()
            future = raises()
            other()
            try:
                result = yield future
                self.counter -= 1 # should not happen
            except RuntimeError:
                self.counter += 1

        @coroutine
        def other():
            import pdb; pdb.set_trace()
            assert 'Should not run, should be cancelled'

        @other.do_except
        def othererr(err):
            if isinstance(err, CancellationError):
                assert 'Should call cancel'
                self.counter += 1

        @other.do_finally
        def otherfin():
            self.counter += 1

        ev = AsyncEventLoop()
        with ev:
            future = main()
        ev.execute_all_tasks()
        self.assertTrue(isinstance(future, BaseFuture))
        self.assertEqual(4, self.counter)
예제 #35
0
    def test_cancel(self):
        @coroutine
        def raises():
            raise RuntimeError()

        @coroutine
        def count():
            self.counter += 1

        @coroutine
        def main():
            yield AnyFuture(raises(), count())

        ev = AsyncEventLoop()
        with ev:
            future = main()
        ev.execute_all_tasks()

        self.assertEqual(0, self.counter)
예제 #36
0
    def test_exceptions(self):
        @task
        def task_func():
            raise RuntimeError("Test")

        @task_func.do_except
        def except_func(err):
            self.except_called = True

        @task_func.do_finally
        def finally_func():
            self.finally_called = True

        ev = AsyncEventLoop()
        with ev:
            task_func()
        ev.execute_all_tasks()

        self.assertTrue(self.except_called)
        self.assertTrue(self.finally_called)
예제 #37
0
    def test_implicit_cancel(self):
        @async
        def raises():
            raise RuntimeError()

        @async
        def count():
            self.counter += 1

        @async
        def main():
            yield raises()
            yield count()

        ev = AsyncEventLoop()
        with ev:
            future = main()
        ev.execute_all_tasks()

        self.assertEqual(0, self.counter)
예제 #38
0
    def test_exceptions(self):
        @task
        def task_func():
            raise RuntimeError("Test")

        @task_func.do_except
        def except_func(err):
            self.except_called = True

        @task_func.do_finally
        def finally_func():
            self.finally_called = True

        ev = AsyncEventLoop()
        with ev:
            task_func()
        ev.execute_all_tasks()

        self.assertTrue(self.except_called)
        self.assertTrue(self.finally_called)
예제 #39
0
    def test_cancel_before_except(self):
        @task
        def raises():
            raise RuntimeError("Error")

        @task
        def main():
            raises()
            self.count()
            self.count()

        @main.do_except
        def main(err):
            self.counter += 1

        ev = AsyncEventLoop()
        with ev:
            main()
        ev.execute_all_tasks()

        self.assertEqual(1, self.counter)
예제 #40
0
    def test_two_futures(self):
        @coroutine
        def returns():
            if False: yield
            return_(1)

        @coroutine
        def count_generator():
            result = yield returns()
            self.counter += result

        @coroutine
        def main():
            yield count_generator()

        ev = AsyncEventLoop()
        with ev:
            future = main()
        ev.execute_all_tasks()
        self.assertTrue(isinstance(future, BaseFuture))
        self.assertEqual(1, self.counter)
예제 #41
0
    def test_cancel_before_except(self):
        @task
        def raises():
            raise RuntimeError("Error")

        @task
        def main():
            raises()
            self.count()
            self.count()

        @main.do_except
        def main(err):
            self.counter += 1

        ev = AsyncEventLoop()
        with ev:
            main()
        ev.execute_all_tasks()

        self.assertEqual(1, self.counter)
예제 #42
0
    def test_recursive(self):
        @task
        def task_raises_recursive(count=3):
            if not count:
                raise RuntimeError("Test")
            count -= 1
            task_raises_recursive(count)

        @task
        def task_func():
            task_raises_recursive()

        @task_func.do_except
        def except_func(err):
            self.tb_str = format_exc()

        ev = AsyncEventLoop()
        with ev:
            task_func()
        ev.execute_all_tasks()

        self.assertTrue(self.tb_str)
예제 #43
0
    def test_future_with_task(self):
        future = BaseFuture()

        def count():
            self.counter +=1

        @task
        def other():
            future.set_result(1)

        @task
        def main():
            other()

        ev = AsyncEventLoop()
        with ev:
            future.add_task(AsyncTask(count))
            main()
        ev.execute_all_tasks()

        self.assertEqual(1, future.result())
        self.assertEqual(1, self.counter)
예제 #44
0
    def test_future_with_task(self):
        future = BaseFuture()

        def count():
            self.counter += 1

        @task
        def other():
            future.set_result(1)

        @task
        def main():
            other()

        ev = AsyncEventLoop()
        with ev:
            future.add_task(AsyncTask(count))
            main()
        ev.execute_all_tasks()

        self.assertEqual(1, future.result())
        self.assertEqual(1, self.counter)
예제 #45
0
    def test_recursive(self):
        @task
        def task_raises_recursive(count=3):
            if not count:
                raise RuntimeError("Test")
            count -= 1
            task_raises_recursive(count)

        @task
        def task_func():
            task_raises_recursive()

        @task_func.do_except
        def except_func(err):
            self.tb_str = format_exc()

        ev = AsyncEventLoop()
        with ev:
            task_func()
        ev.execute_all_tasks()

        self.assertTrue(self.tb_str)
예제 #46
0
    def test_task_finally(self):
        @task
        def recursive(ct=1):
            self.counter += 1
            if ct == 1:
                return
            ct -=1
            recursive(ct)

        @task
        def recursive_caller():
            recursive()

        @recursive_caller.do_finally
        def finally_func():
            self.finally_called = True

        ev = AsyncEventLoop()
        with ev:
            recursive_caller()
        ev.execute_all_tasks()

        self.assertTrue(self.finally_called)
예제 #47
0
    def test_simple_async_with_parens(self):
        @coroutine()
        def count():
            self.counter += 1

        @coroutine()
        def count_generator():
            if False: yield
            self.counter += 1

        @coroutine()
        def main():
            future = count_generator()
            for i in range(3):
                yield count()
            yield future

        ev = AsyncEventLoop()
        with ev:
            future = main()
        ev.execute_all_tasks()
        self.assertTrue(isinstance(future, BaseFuture))
        self.assertEqual(4, self.counter)