コード例 #1
0
    def test_wait_timeout_completed(self):
        s1 = operations.sleep(0.25, obj)
        s2 = operations.sleep(0.5, obj2)
        s3 = operations.sleep(0.75, obj3)

        result = operations.wait([s1, s2, s3], timeout=1).result()
        self.assertEqual(len(result.done), 3)
        self.assertEqual(len(result.not_done), 0)
コード例 #2
0
    def test_wait_timeout_premature(self):
        s1 = operations.sleep(0.25, obj)
        s2 = operations.sleep(0.75, obj2)
        s3 = operations.sleep(1, obj3)

        result = operations.wait([s1, s2, s3], timeout=0.5).result()
        self.assertEqual(len(result.done), 1)
        self.assertEqual(len(result.not_done), 2)
コード例 #3
0
    def test_gather_simple(self):
        s1 = operations.sleep(0.25, obj)
        s2 = operations.sleep(0.5, obj2)
        s3 = operations.sleep(0.75, obj3)

        r1, r2, r3 = operations.gather(s1, s2, s3).result()
        self.assertIs(r1, obj)
        self.assertIs(r2, obj2)
        self.assertIs(r3, obj3)
コード例 #4
0
    def test_gather_return(self):
        s1 = operations.sleep(0.25, obj)
        s2 = operations.sleep(0.5, obj2)
        s3 = operations.sleep(0.75, obj3)

        r1, r2, r3 = operations.gather(s1, s2, s3,
                                       return_exceptions=True).result()
        self.assertIs(r1, obj)
        self.assertIs(r2, obj2)
        self.assertIs(r3, obj3)
コード例 #5
0
    def test_gather_propagate_cancel(self):
        s1 = operations.sleep(0.5, obj)
        s2 = operations.sleep(0.75, obj2)
        s3 = operations.sleep(1, obj3)

        g = operations.gather(s1, s2, s3)
        time.sleep(0.25)
        g.cancel()
        self.assertTrue(s1.cancelled())
        self.assertTrue(s2.cancelled())
        self.assertTrue(s3.cancelled())
コード例 #6
0
    def test_gather_propagate_partial_complete(self):
        s1 = operations.sleep(0.25, obj)
        s2 = operations.sleep(0.75, obj2)
        s3 = operations.sleep(1, obj3)

        g = operations.gather(s1, s2, s3)
        time.sleep(0.5)
        g.cancel()
        self.assertFalse(s1.cancelled())
        self.assertIs(s1.result(), obj)
        self.assertTrue(s2.cancelled())
        self.assertTrue(s3.cancelled())
コード例 #7
0
    def test_wait_first_exception_no_error(self):
        s1 = operations.sleep(0.25, obj)
        s2 = operations.sleep(0.5, obj2)
        s3 = operations.sleep(0.75, obj3)

        result = operations.wait([s1, s2, s3],
                                 return_when=FIRST_EXCEPTION).result()
        self.assertEqual(len(result.done), 3)
        self.assertEqual(len(result.not_done), 0)

        self.assertTrue(s1.done())
        self.assertTrue(s2.done())
        self.assertTrue(s3.done())
コード例 #8
0
    def test_wait_shield_parent_cancel(self):
        s1 = operations.sleep(0.5, obj)
        s2 = operations.sleep(0.75, obj2)
        s3 = operations.sleep(1, obj3)

        w = operations.wait([s1, s2, s3])
        time.sleep(0.25)
        w.cancel()

        self.assertFalse(s1.cancelled())
        self.assertFalse(s2.cancelled())
        self.assertFalse(s3.cancelled())
        s1.cancel()
        s2.cancel()
        s3.cancel()
コード例 #9
0
    def test_gather_error_return(self):
        s1 = operations.sleep(0.25, obj)

        @operations.futurize
        def _s2():
            yield operations.sleep(0.5)
            raise exc

        s2 = _s2()
        s3 = operations.sleep(0.75, obj3)

        r1, r2, r3 = operations.gather(s1, s2, s3,
                                       return_exceptions=True).result()
        self.assertIs(r1, obj)
        self.assertIs(r2, exc)
        self.assertIs(r3, obj3)
コード例 #10
0
    def test_wait_first_complete(self):
        s1 = operations.sleep(0.25, obj)
        s2 = operations.sleep(0.5, obj2)
        s3 = operations.sleep(0.75, obj3)

        result = operations.wait([s1, s2, s3],
                                 return_when=FIRST_COMPLETED).result()
        self.assertEqual(len(result.done), 1)
        self.assertEqual(len(result.not_done), 2)

        self.assertTrue(s1.done())
        self.assertFalse(s2.done())
        self.assertFalse(s3.done())

        s2.cancel()
        s3.cancel()
コード例 #11
0
 def test_shield_not_cancelled(self):
     s = operations.sleep(0.5, obj)
     fut = operations.shield(s)
     fut.cancel()
     self.assertFalse(s.cancelled())
     self.assertTrue(fut.cancelled())
     s.cancel()
コード例 #12
0
    def test_gather_cancel_fater_first_completion(self):
        s1 = operations.sleep(0.25, obj)
        s2 = operations.sleep(0.75, obj2)
        s3 = operations.sleep(1, obj3)

        g = operations.gather(s1, s2, s3, return_exceptions=True)
        time.sleep(0.5)
        s2.cancel()

        self.assertFalse(g.cancelled())
        self.assertFalse(s1.cancelled())
        self.assertTrue(s2.cancelled())
        self.assertFalse(s3.cancelled())

        r1, r2, r3 = g.result()
        self.assertIs(r1, obj)
        self.assertIsInstance(r2, CancelledError)
        self.assertIs(r3, obj3)
コード例 #13
0
    def test_aio_await_future(self):
        s = sleep(0.5, obj)

        x = self.loop.run_until_complete(s)

        self.assertTrue(s.done())
        self.assertFalse(s.cancelled())
        self.assertIsNone(s.exception())
        self.assertIs(s.result(), obj)
        self.assertIs(x, obj)
コード例 #14
0
    def test_gather_shield_child_cancel(self):
        s1 = operations.sleep(0.5, obj)
        s2 = operations.sleep(0.75, obj2)
        s3 = operations.sleep(1, obj3)

        g = operations.gather(s1, s2, s3)
        time.sleep(0.25)
        s1.cancel()
        self.assertFalse(g.cancelled())
        self.assertTrue(s1.cancelled())
        self.assertFalse(s2.cancelled())
        self.assertFalse(s3.cancelled())

        try:
            g.result()
        except CancelledError:
            pass
        except:
            self.fail("Incorrect error")
コード例 #15
0
    def test_wait_first_exception(self):
        @operations.futurize
        async def _func():
            await operations.sleep(.5)
            raise exc

        s1 = operations.sleep(0.25, obj)
        s2 = _func()
        s3 = operations.sleep(0.75, obj3)

        result = operations.wait([s1, s2, s3],
                                 return_when=FIRST_EXCEPTION).result()
        self.assertEqual(len(result.done), 2)
        self.assertEqual(len(result.not_done), 1)

        self.assertTrue(s1.done())
        self.assertTrue(s2.done())
        self.assertFalse(s3.done())

        s3.cancel()
コード例 #16
0
    def test_wait_graceful_child_cancel_exception(self):
        s1 = operations.sleep(0.25, obj)
        s2 = operations.sleep(0.75, obj2)
        s3 = operations.sleep(1, obj3)

        w = operations.wait([s1, s2, s3], return_when=FIRST_EXCEPTION)
        time.sleep(0.5)
        s2.cancel()

        self.assertFalse(w.cancelled())
        self.assertFalse(s1.cancelled())
        self.assertTrue(s2.cancelled())
        self.assertFalse(s3.cancelled())

        done, not_done = w.result()
        self.assertEqual(len(done), 2)
        self.assertEqual(len(not_done), 1)

        self.assertTrue(
            any(isinstance(d.exception(), CancelledError) for d in done))
コード例 #17
0
    def test_wait_first_complete_with_error(self):
        @operations.futurize
        async def _func():
            await operations.sleep(.25)
            raise exc

        s1 = _func()
        s2 = operations.sleep(0.5, obj2)
        s3 = operations.sleep(0.75, obj3)

        result = operations.wait([s1, s2, s3],
                                 return_when=FIRST_COMPLETED).result()
        self.assertEqual(len(result.done), 1)
        self.assertEqual(len(result.not_done), 2)

        self.assertTrue(s1.done())
        self.assertFalse(s2.done())
        self.assertFalse(s3.done())

        s2.cancel()
        s3.cancel()
コード例 #18
0
    def test_gather_error(self):
        s1 = operations.sleep(0.25, obj)

        @operations.futurize
        def _s2():
            yield operations.sleep(0.5)
            raise exc

        s2 = _s2()
        s3 = operations.sleep(0.75, obj3)

        try:
            operations.gather(s1, s2, s3).result()
        except Exception as e:
            self.assertIs(e, exc)
        else:
            self.fail("Did not trigger exception")

        self.assertTrue(s1.done())
        self.assertFalse(s1.cancelled())
        self.assertTrue(s2.done())
        self.assertFalse(s2.cancelled())
        self.assertFalse(s3.done())
        self.assertFalse(s3.cancelled())
コード例 #19
0
    def test_coro_child_cancel_bubble(self):
        timeout = sleep(2)
        err = None

        async def _func():
            nonlocal err
            try:
                await timeout
            except CancelledError as e:
                err = e

        fut = run_coroutine(_func())
        time.sleep(.5)
        timeout.cancel()
        fut.result()
        self.assertIsInstance(err, CancelledError)
コード例 #20
0
    def test_aio_future_in_futurized(self):
        s = asleep(0.5, obj, loop=self.loop)

        @futurize
        async def sleeper():
            return await s

        async def runner():
            await s

        s: AbstractFuture[object] = sleeper()
        self.loop.create_task(runner())
        self.loop.run_until_complete(sleep(0.75))

        self.assertTrue(s.done())
        self.assertIs(s.result(), obj)
コード例 #21
0
    def test_coro_parent_cancel_exit(self):
        timeout = sleep(2)
        err = None

        async def _func():
            nonlocal err
            try:
                await timeout
            except GeneratorExit as e:
                err = e

        fut = run_coroutine(_func())
        time.sleep(.5)
        fut.cancel()
        self.assertTrue(timeout.cancelled())
        self.assertIsInstance(err, GeneratorExit)
コード例 #22
0
 def test_waitfor_within_time(self):
     fut = operations.wait_for(operations.sleep(0.5, obj), 1)
     self.assertIs(fut.result(), obj)
コード例 #23
0
 def test_shield_child_cancel_bubbles(self):
     s = operations.sleep(0.5, obj)
     fut = operations.shield(s)
     s.cancel()
     self.assertFalse(fut.cancelled())
     self.assertIsInstance(fut.exception(), CancelledError)
コード例 #24
0
 def test_waitfor_endless(self):
     fut = operations.wait_for(operations.sleep(0.5, obj), 0)
     self.assertIs(fut.result(), obj)
コード例 #25
0
 def test_sleep(self):
     start = time.time()
     fut = operations.sleep(1, obj)
     self.assertIs(fut.result(), obj)
     self.assertGreaterEqual(time.time() - start, 1)
コード例 #26
0
 def test_shield_success(self):
     s = operations.sleep(0.5, obj)
     fut = operations.shield(s)
     self.assertIs(fut.result(), obj)
コード例 #27
0
 def test_sleep_0(self):
     fut, t = operations.sleep(0, obj, also_return_timer=True)
     self.assertIsNone(t)
     self.assertIsInstance(fut, operations._SleepForceThreadSwitch)
     self.assertIs(fut.result(), obj)
コード例 #28
0
 def _s2():
     yield operations.sleep(0.5)
     raise exc
コード例 #29
0
 def test_sleep_cancel(self):
     fut, t = operations.sleep(2, obj, also_return_timer=True)
     time.sleep(1)
     fut.cancel()
     self.assertTrue(t.finished.is_set())
コード例 #30
0
 def test_waitfor_timed_out(self):
     s = operations.sleep(0.5, obj)
     fut = operations.wait_for(s, 0.25)
     self.assertIsInstance(fut.exception(), TimeoutError)
     self.assertTrue(s.cancelled())