Exemplo n.º 1
0
    def test_task_repr(self):
        self.loop.set_debug(False)

        @asyncio.coroutine
        def notmuch():
            yield from []
            return 'abc'

        # test coroutine function
        self.assertEqual(notmuch.__name__, 'notmuch')
        self.assertEqual(notmuch.__qualname__,
                         '_TestTasks.test_task_repr.<locals>.notmuch')
        self.assertEqual(notmuch.__module__, __name__)

        filename, lineno = test_utils.get_function_source(notmuch)
        src = "%s:%s" % (filename, lineno)

        # test coroutine object
        gen = notmuch()
        coro_qualname = '_TestTasks.test_task_repr.<locals>.notmuch'
        self.assertEqual(gen.__name__, 'notmuch')
        self.assertEqual(gen.__qualname__, coro_qualname)

        # test pending Task
        t = asyncio.Task(gen, loop=self.loop)
        t.add_done_callback(Dummy())

        coro = format_coroutine(coro_qualname, 'running', src,
                                t._source_traceback, generator=True)
        self.assertEqual(repr(t),
                         '<Task pending %s cb=[<Dummy>()]>' % coro)

        # test canceling Task
        t.cancel()  # Does not take immediate effect!
        self.assertEqual(repr(t),
                         '<Task cancelling %s cb=[<Dummy>()]>' % coro)

        # test canceled Task
        self.assertRaises(asyncio.CancelledError,
                          self.loop.run_until_complete, t)
        coro = format_coroutine(coro_qualname, 'done', src,
                                t._source_traceback)
        self.assertEqual(repr(t),
                         '<Task cancelled %s>' % coro)

        # test finished Task
        t = asyncio.Task(notmuch(), loop=self.loop)
        self.loop.run_until_complete(t)
        coro = format_coroutine(coro_qualname, 'done', src,
                                t._source_traceback)
        self.assertEqual(repr(t),
                         "<Task finished %s result='abc'>" % coro)
Exemplo n.º 2
0
    def test_task_repr(self):
        self.loop.set_debug(False)

        @asyncio.coroutine
        def notmuch():
            yield from []
            return 'abc'

        # test coroutine function
        self.assertEqual(notmuch.__name__, 'notmuch')
        self.assertEqual(notmuch.__qualname__,
                         '_TestTasks.test_task_repr.<locals>.notmuch')
        self.assertEqual(notmuch.__module__, __name__)

        filename, lineno = test_utils.get_function_source(notmuch)
        src = "%s:%s" % (filename, lineno)

        # test coroutine object
        gen = notmuch()
        coro_qualname = '_TestTasks.test_task_repr.<locals>.notmuch'
        self.assertEqual(gen.__name__, 'notmuch')
        self.assertEqual(gen.__qualname__, coro_qualname)

        # test pending Task
        t = asyncio.Task(gen, loop=self.loop)
        t.add_done_callback(Dummy())

        coro = format_coroutine(coro_qualname,
                                'running',
                                src,
                                t._source_traceback,
                                generator=True)
        self.assertEqual(repr(t), '<Task pending %s cb=[<Dummy>()]>' % coro)

        # test canceling Task
        t.cancel()  # Does not take immediate effect!
        self.assertEqual(repr(t), '<Task cancelling %s cb=[<Dummy>()]>' % coro)

        # test canceled Task
        self.assertRaises(asyncio.CancelledError, self.loop.run_until_complete,
                          t)
        coro = format_coroutine(coro_qualname, 'done', src,
                                t._source_traceback)
        self.assertEqual(repr(t), '<Task cancelled %s>' % coro)

        # test finished Task
        t = asyncio.Task(notmuch(), loop=self.loop)
        self.loop.run_until_complete(t)
        coro = format_coroutine(coro_qualname, 'done', src,
                                t._source_traceback)
        self.assertEqual(repr(t), "<Task finished %s result='abc'>" % coro)
Exemplo n.º 3
0
def test_task_repr(loop):
    loop.set_debug(False)

    @asyncio.coroutine
    def notmuch():
        yield from []
        return 'abc'

    # test coroutine function
    assert notmuch.__name__ == 'notmuch'
    assert notmuch.__qualname__ == 'test_task_repr.<locals>.notmuch'
    assert notmuch.__module__ == __name__

    filename, lineno = test_utils.get_function_source(notmuch)
    src = "%s:%s" % (filename, lineno)

    # test coroutine object
    gen = notmuch()
    coro_qualname = 'test_task_repr.<locals>.notmuch'
    assert gen.__name__ == 'notmuch'
    assert gen.__qualname__ == coro_qualname

    # test pending Task
    t = asyncio.Task(gen, loop=loop)
    t.add_done_callback(Dummy())

    coro = format_coroutine(coro_qualname,
                            'running',
                            src,
                            t._source_traceback,
                            generator=True)
    assert repr(t) == '<Task pending %s cb=[<Dummy>()]>' % coro

    # test canceling Task
    t.cancel()  # Does not take immediate effect!
    assert repr(t) == '<Task cancelling %s cb=[<Dummy>()]>' % coro

    # test canceled Task
    with pytest.raises(asyncio.CancelledError):
        loop.run_until_complete(t)

    coro = format_coroutine(coro_qualname, 'done', src, t._source_traceback)
    assert repr(t) == '<Task cancelled %s>' % coro

    # test finished Task
    t = asyncio.Task(notmuch(), loop=loop)
    loop.run_until_complete(t)
    coro = format_coroutine(coro_qualname, 'done', src, t._source_traceback)
    assert repr(t) == "<Task finished %s result='abc'>" % coro
Exemplo n.º 4
0
 def func_repr(func):
     filename, lineno = test_utils.get_function_source(func)
     text = "%s() at %s:%s" % (func.__qualname__, filename, lineno)
     return re.escape(text)
Exemplo n.º 5
0
 def func_repr(func):
     filename, lineno = test_utils.get_function_source(func)
     text = '%s() at %s:%s' % (func.__qualname__, filename, lineno)
     return re.escape(text)