コード例 #1
0
    def test_callback_throws_exception(self):
        f = Future()

        def invalid_func(_):
            raise RuntimeError("error!")

        f.add_done_callback(invalid_func)
        f.set_result("done")  # should not throw exception
コード例 #2
0
    def test_callback_throws_exception(self):
        f = Future()

        def invalid_func():
            raise RuntimeError("error!")

        f.add_done_callback(invalid_func)
        f.set_result("done")  # should not throw exception
コード例 #3
0
    def test_add_callback_after_completion_with_success(self):
        f = Future()
        f.set_result("done")

        counter = [0]

        def callback(future):
            counter[0] += 1
            self.assertEqual(future.result(), "done")

        f.add_done_callback(callback)
        self.assertEqual(counter[0], 1)
コード例 #4
0
    def test_add_callback_after_completion_with_success(self):
        f = Future()
        f.set_result("done")

        counter = [0]

        def callback(future):
            counter[0] += 1
            self.assertEqual(future.result(), "done")

        f.add_done_callback(callback)
        self.assertEqual(counter[0], 1)
コード例 #5
0
    def test_add_callback_after_completion_with_error(self):
        f = Future()
        error = RuntimeError("error")
        f.set_exception(error, None)

        counter = [0]

        def callback(future):
            counter[0] += 1
            self.assertEqual(future.exception(), error)

        f.add_done_callback(callback)
        self.assertEqual(counter[0], 1)
コード例 #6
0
    def test_add_callback_after_completion_with_error(self):
        f = Future()
        error = RuntimeError("error")
        f.set_exception(error, None)

        counter = [0]

        def callback(future):
            counter[0] += 1
            self.assertEqual(future.exception(), error)

        f.add_done_callback(callback)
        self.assertEqual(counter[0], 1)
コード例 #7
0
    def test_add_callback_with_success(self):
        f = Future()
        e = Event()

        def set_result():
            f.set_result("done")

        def callback(future):
            self.assertEqual(future.result(), "done")
            e.set()

        f.add_done_callback(callback)

        Thread(target=set_result).start()

        self.assertTrue(e.wait(timeout=5))
コード例 #8
0
    def test_add_callback_with_success(self):
        f = Future()
        e = Event()

        def set_result():
            f.set_result("done")

        def callback(future):
            self.assertEqual(future.result(), "done")
            e.set()

        f.add_done_callback(callback)

        Thread(target=set_result).start()

        self.assertTrue(e.wait(timeout=5))
コード例 #9
0
    def test_callback_called_exactly_once_when_exception(self):
        for _ in range(0, 10000):
            f = Future()

            def set_exception():
                f.set_exception(RuntimeError("error"))

            t = Thread(target=set_exception)
            t.start()

            i = [0]

            def callback(c):
                i[0] += 1

            f.add_done_callback(callback)
            t.join()
            self.assertEqual(i[0], 1)
コード例 #10
0
    def test_callback_called_exactly_once(self):
        for _ in range(0, 10000):
            f = Future()

            def set_result():
                f.set_result("done")

            t = Thread(target=set_result)
            t.start()

            i = [0]

            def callback(c):
                i[0] += 1

            f.add_done_callback(callback)
            t.join()
            self.assertEqual(i[0], 1)
コード例 #11
0
    def test_callback_called_exactly_once_when_exception(self):
        for _ in xrange(0, 10000):
            f = Future()

            def set_exception():
                f.set_exception(RuntimeError("error"))

            t = Thread(target=set_exception)
            t.start()

            i = [0]

            def callback(c):
                i[0] += 1

            f.add_done_callback(callback)
            t.join()
            self.assertEqual(i[0], 1)
コード例 #12
0
    def test_callback_called_exactly_once(self):
        for _ in xrange(0, 10000):
            f = Future()

            def set_result():
                f.set_result("done")

            t = Thread(target=set_result)
            t.start()

            i = [0]

            def callback(c):
                i[0] += 1

            f.add_done_callback(callback)
            t.join()
            self.assertEqual(i[0], 1)
コード例 #13
0
    def test_add_callback_with_failure(self):
        f = Future()
        e = Event()
        exc = []

        def set_exception():
            map = {}
            try:
                a = map["invalid_key"]
            except KeyError as e:
                exc.append(sys.exc_info())
                f.set_exception(e, sys.exc_info()[2])

        def callback(future):
            exc_info = exc[0]
            self.assertEqual(exc_info[1], future.exception())
            self.assertEqual(exc_info[2], future.traceback())
            e.set()

        f.add_done_callback(callback)

        Thread(target=set_exception).start()

        self.assertTrue(e.wait(timeout=5))
コード例 #14
0
    def test_add_callback_with_failure(self):
        f = Future()
        e = Event()
        exc = []

        def set_exception():
            map = {}
            try:
                a = map["invalid_key"]
            except KeyError as e:
                exc.append(sys.exc_info())
                f.set_exception(e, sys.exc_info()[2])

        def callback(future):
            exc_info = exc[0]
            self.assertEqual(exc_info[1], future.exception())
            self.assertEqual(exc_info[2], future.traceback())
            e.set()

        f.add_done_callback(callback)

        Thread(target=set_exception).start()

        self.assertTrue(e.wait(timeout=5))