Example #1
0
    def test_async_coroutine(self):
        @tasks.coroutine
        def notmuch():
            return 'ok'
        t = tasks.async(notmuch())
        self.loop.run_until_complete(t)
        self.assertTrue(t.done())
        self.assertEqual(t.result(), 'ok')
        self.assertIs(t._loop, self.loop)

        loop = events.new_event_loop()
        t = tasks.async(notmuch(), loop=loop)
        self.assertIs(t._loop, loop)
Example #2
0
    def test_async_future(self):
        f_orig = futures.Future()
        f_orig.set_result('ko')

        f = tasks.async(f_orig)
        self.loop.run_until_complete(f)
        self.assertTrue(f.done())
        self.assertEqual(f.result(), 'ko')
        self.assertIs(f, f_orig)

        with self.assertRaises(ValueError):
            loop = events.new_event_loop()
            f = tasks.async(f_orig, loop=loop)
        f = tasks.async(f_orig, loop=self.loop)
        self.assertIs(f, f_orig)
Example #3
0
    def test_async_task(self):
        @tasks.coroutine
        def notmuch():
            return 'ok'
        t_orig = tasks.Task(notmuch())
        t = tasks.async(t_orig)
        self.loop.run_until_complete(t)
        self.assertTrue(t.done())
        self.assertEqual(t.result(), 'ok')
        self.assertIs(t, t_orig)

        with self.assertRaises(ValueError):
            loop = events.new_event_loop()
            t = tasks.async(t_orig, loop=loop)
        t = tasks.async(t_orig, loop=self.loop)
        self.assertIs(t, t_orig)
Example #4
0
    def test_cancel_current_task(self):
        loop = events.new_event_loop()
        self.addCleanup(loop.close)

        @tasks.coroutine
        def task():
            t.cancel()
            self.assertTrue(t._must_cancel)  # White-box test.
            # The sleep should be cancelled immediately.
            yield from tasks.sleep(100, loop=loop)
            return 12

        t = tasks.Task(task(), loop=loop)
        self.assertRaises(
            futures.CancelledError, loop.run_until_complete, t)
        self.assertTrue(t.done())
        self.assertFalse(t._must_cancel)  # White-box test.
        self.assertFalse(t.cancel())
Example #5
0
 def setUp(self):
     self.loop = events.new_event_loop()
     events.set_event_loop(None)