示例#1
0
    def test_wait_first_exception_in_wait(self):

        def gen():
            when = yield
            self.assertAlmostEqual(10.0, when)
            when = yield 0
            self.assertAlmostEqual(0.01, when)
            yield 0.01

        loop = test_utils.TestLoop(gen)
        self.addCleanup(loop.close)

        # first_exception, exception during waiting
        a = tasks.Task(tasks.sleep(10.0, loop=loop), loop=loop)

        @tasks.coroutine
        def exc():
            yield from tasks.sleep(0.01, loop=loop)
            raise ZeroDivisionError('err')

        b = tasks.Task(exc(), loop=loop)
        task = tasks.wait([b, a], return_when=tasks.FIRST_EXCEPTION,
                          loop=loop)

        done, pending = loop.run_until_complete(task)
        self.assertEqual({b}, done)
        self.assertEqual({a}, pending)
        self.assertAlmostEqual(0.01, loop.time())

        # move forward to close generator
        loop.advance_time(10)
        loop.run_until_complete(tasks.wait([a, b], loop=loop))
示例#2
0
    def test_wait_first_completed(self):
        def gen():
            when = yield
            self.assertAlmostEqual(10.0, when)
            when = yield 0
            self.assertAlmostEqual(0.1, when)
            yield 0.1

        loop = test_utils.TestLoop(gen)
        self.addCleanup(loop.close)

        a = tasks.Task(tasks.sleep(10.0, loop=loop), loop=loop)
        b = tasks.Task(tasks.sleep(0.1, loop=loop), loop=loop)
        task = tasks.Task(tasks.wait([b, a],
                                     return_when=tasks.FIRST_COMPLETED,
                                     loop=loop),
                          loop=loop)

        done, pending = loop.run_until_complete(task)
        self.assertEqual({b}, done)
        self.assertEqual({a}, pending)
        self.assertFalse(a.done())
        self.assertTrue(b.done())
        self.assertIsNone(b.result())
        self.assertAlmostEqual(0.1, loop.time())

        # move forward to close generator
        loop.advance_time(10)
        loop.run_until_complete(tasks.wait([a, b], loop=loop))
示例#3
0
    def test_wait_first_completed(self):

        def gen():
            when = yield
            self.assertAlmostEqual(10.0, when)
            when = yield 0
            self.assertAlmostEqual(0.1, when)
            yield 0.1

        loop = test_utils.TestLoop(gen)
        self.addCleanup(loop.close)

        a = tasks.Task(tasks.sleep(10.0, loop=loop), loop=loop)
        b = tasks.Task(tasks.sleep(0.1, loop=loop), loop=loop)
        task = tasks.Task(
            tasks.wait([b, a], return_when=tasks.FIRST_COMPLETED,
                       loop=loop),
            loop=loop)

        done, pending = loop.run_until_complete(task)
        self.assertEqual({b}, done)
        self.assertEqual({a}, pending)
        self.assertFalse(a.done())
        self.assertTrue(b.done())
        self.assertIsNone(b.result())
        self.assertAlmostEqual(0.1, loop.time())

        # move forward to close generator
        loop.advance_time(10)
        loop.run_until_complete(tasks.wait([a, b], loop=loop))
示例#4
0
    def test_wait_concurrent_complete(self):
        def gen():
            when = yield
            self.assertAlmostEqual(0.1, when)
            when = yield 0
            self.assertAlmostEqual(0.15, when)
            when = yield 0
            self.assertAlmostEqual(0.1, when)
            yield 0.1

        loop = test_utils.TestLoop(gen)
        self.addCleanup(loop.close)

        a = tasks.Task(tasks.sleep(0.1, loop=loop), loop=loop)
        b = tasks.Task(tasks.sleep(0.15, loop=loop), loop=loop)

        done, pending = loop.run_until_complete(
            tasks.wait([b, a], timeout=0.1, loop=loop))

        self.assertEqual(done, set([a]))
        self.assertEqual(pending, set([b]))
        self.assertAlmostEqual(0.1, loop.time())

        # move forward to close generator
        loop.advance_time(10)
        loop.run_until_complete(tasks.wait([a, b], loop=loop))
示例#5
0
    def test_wait_concurrent_complete(self):

        def gen():
            when = yield
            self.assertAlmostEqual(0.1, when)
            when = yield 0
            self.assertAlmostEqual(0.15, when)
            when = yield 0
            self.assertAlmostEqual(0.1, when)
            yield 0.1

        loop = test_utils.TestLoop(gen)
        self.addCleanup(loop.close)

        a = tasks.Task(tasks.sleep(0.1, loop=loop), loop=loop)
        b = tasks.Task(tasks.sleep(0.15, loop=loop), loop=loop)

        done, pending = loop.run_until_complete(
            tasks.wait([b, a], timeout=0.1, loop=loop))

        self.assertEqual(done, set([a]))
        self.assertEqual(pending, set([b]))
        self.assertAlmostEqual(0.1, loop.time())

        # move forward to close generator
        loop.advance_time(10)
        loop.run_until_complete(tasks.wait([a, b], loop=loop))
示例#6
0
    def test_wait_first_exception_in_wait(self):
        def gen():
            when = yield
            self.assertAlmostEqual(10.0, when)
            when = yield 0
            self.assertAlmostEqual(0.01, when)
            yield 0.01

        loop = test_utils.TestLoop(gen)
        self.addCleanup(loop.close)

        # first_exception, exception during waiting
        a = tasks.Task(tasks.sleep(10.0, loop=loop), loop=loop)

        @tasks.coroutine
        def exc():
            yield from tasks.sleep(0.01, loop=loop)
            raise ZeroDivisionError('err')

        b = tasks.Task(exc(), loop=loop)
        task = tasks.wait([b, a], return_when=tasks.FIRST_EXCEPTION, loop=loop)

        done, pending = loop.run_until_complete(task)
        self.assertEqual({b}, done)
        self.assertEqual({a}, pending)
        self.assertAlmostEqual(0.01, loop.time())

        # move forward to close generator
        loop.advance_time(10)
        loop.run_until_complete(tasks.wait([a, b], loop=loop))
示例#7
0
文件: tasks_test.py 项目: sah/tulip
    def test_wait_errors(self):
        self.assertRaises(
            ValueError, self.loop.run_until_complete,
            tasks.wait(set()))

        self.assertRaises(
            ValueError, self.loop.run_until_complete,
            tasks.wait([tasks.sleep(10.0)], return_when=-1))
示例#8
0
    def test_wait_errors(self):
        self.assertRaises(ValueError, self.loop.run_until_complete,
                          tasks.wait(set(), loop=self.loop))

        self.assertRaises(
            ValueError, self.loop.run_until_complete,
            tasks.wait([tasks.sleep(10.0, loop=self.loop)],
                       return_when=-1,
                       loop=self.loop))
示例#9
0
    def test_create_connection_mutiple_errors(self, m_socket):

        class MyProto(protocols.Protocol):
            pass

        @tasks.coroutine
        def getaddrinfo(*args, **kw):
            yield from []
            return [(2, 1, 6, '', ('107.6.106.82', 80)),
                    (2, 1, 6, '', ('107.6.106.82', 80))]

        def getaddrinfo_task(*args, **kwds):
            return tasks.Task(getaddrinfo(*args, **kwds), loop=self.loop)

        idx = -1
        errors = ['err1', 'err2']

        def _socket(*args, **kw):
            nonlocal idx, errors
            idx += 1
            raise socket.error(errors[idx])

        m_socket.socket = _socket
        m_socket.error = socket.error

        self.loop.getaddrinfo = getaddrinfo_task

        task = tasks.Task(
            self.loop.create_connection(MyProto, 'example.com', 80))
        yield from tasks.wait(task)
        exc = task.exception()
        self.assertEqual("Multiple exceptions: err1, err2", str(exc))
示例#10
0
    def test_wait_really_done(self):
        # there is possibility that some tasks in the pending list
        # became done but their callbacks haven't all been called yet

        @tasks.coroutine
        def coro1():
            yield

        @tasks.coroutine
        def coro2():
            yield
            yield

        a = tasks.Task(coro1(), loop=self.loop)
        b = tasks.Task(coro2(), loop=self.loop)
        task = tasks.Task(tasks.wait([b, a],
                                     return_when=tasks.FIRST_COMPLETED,
                                     loop=self.loop),
                          loop=self.loop)

        done, pending = self.loop.run_until_complete(task)
        self.assertEqual({a, b}, done)
        self.assertTrue(a.done())
        self.assertIsNone(a.result())
        self.assertTrue(b.done())
        self.assertIsNone(b.result())
示例#11
0
    def test_create_connection_mutiple_errors(self, m_socket):
        class MyProto(protocols.Protocol):
            pass

        @tasks.coroutine
        def getaddrinfo(*args, **kw):
            yield from []
            return [(2, 1, 6, '', ('107.6.106.82', 80)),
                    (2, 1, 6, '', ('107.6.106.82', 80))]

        def getaddrinfo_task(*args, **kwds):
            return tasks.Task(getaddrinfo(*args, **kwds), loop=self.loop)

        idx = -1
        errors = ['err1', 'err2']

        def _socket(*args, **kw):
            nonlocal idx, errors
            idx += 1
            raise socket.error(errors[idx])

        m_socket.socket = _socket
        m_socket.error = socket.error

        self.loop.getaddrinfo = getaddrinfo_task

        task = tasks.Task(
            self.loop.create_connection(MyProto, 'example.com', 80))
        yield from tasks.wait(task)
        exc = task.exception()
        self.assertEqual("Multiple exceptions: err1, err2", str(exc))
示例#12
0
    def test_wait_really_done(self):
        # there is possibility that some tasks in the pending list
        # became done but their callbacks haven't all been called yet

        @tasks.coroutine
        def coro1():
            yield

        @tasks.coroutine
        def coro2():
            yield
            yield

        a = tasks.Task(coro1(), loop=self.loop)
        b = tasks.Task(coro2(), loop=self.loop)
        task = tasks.Task(
            tasks.wait([b, a], return_when=tasks.FIRST_COMPLETED,
                       loop=self.loop),
            loop=self.loop)

        done, pending = self.loop.run_until_complete(task)
        self.assertEqual({a, b}, done)
        self.assertTrue(a.done())
        self.assertIsNone(a.result())
        self.assertTrue(b.done())
        self.assertIsNone(b.result())
示例#13
0
    def test_wait_concurrent_complete(self):
        a = tasks.Task(tasks.sleep(0.1, loop=self.loop), loop=self.loop)
        b = tasks.Task(tasks.sleep(0.15, loop=self.loop), loop=self.loop)

        done, pending = self.loop.run_until_complete(
            tasks.wait([b, a], timeout=0.1, loop=self.loop))

        self.assertEqual(done, set([a]))
        self.assertEqual(pending, set([b]))
示例#14
0
 def test_as_completed_concurrent(self):
     a = tasks.sleep(0.05, 'a', loop=self.loop)
     b = tasks.sleep(0.05, 'b', loop=self.loop)
     fs = {a, b}
     futs = list(tasks.as_completed(fs, loop=self.loop))
     self.assertEqual(len(futs), 2)
     waiter = tasks.wait(futs, loop=self.loop)
     done, pending = self.loop.run_until_complete(waiter)
     self.assertEqual(set(f.result() for f in done), {'a', 'b'})
示例#15
0
文件: tasks_test.py 项目: sah/tulip
    def test_wait_concurrent_complete(self):
        a = tasks.Task(tasks.sleep(0.1))
        b = tasks.Task(tasks.sleep(0.15))

        done, pending = self.loop.run_until_complete(
            tasks.wait([b, a], timeout=0.1))

        self.assertEqual(done, set([a]))
        self.assertEqual(pending, set([b]))
示例#16
0
文件: tasks_test.py 项目: sah/tulip
 def test_as_completed_concurrent(self):
     a = tasks.sleep(0.05, 'a')
     b = tasks.sleep(0.05, 'b')
     fs = {a, b}
     futs = list(tasks.as_completed(fs))
     self.assertEqual(len(futs), 2)
     waiter = tasks.wait(futs)
     done, pending = self.loop.run_until_complete(waiter)
     self.assertEqual(set(f.result() for f in done), {'a', 'b'})
示例#17
0
文件: tasks_test.py 项目: sah/tulip
    def test_wait_first_completed(self):
        a = tasks.Task(tasks.sleep(10.0))
        b = tasks.Task(tasks.sleep(0.1))
        task = tasks.Task(tasks.wait(
            [b, a], return_when=tasks.FIRST_COMPLETED))

        done, pending = self.loop.run_until_complete(task)
        self.assertEqual({b}, done)
        self.assertEqual({a}, pending)
        self.assertFalse(a.done())
        self.assertTrue(b.done())
        self.assertIsNone(b.result())
示例#18
0
    def test_exception_waiter(self):
        buffer = parsers.DataBuffer(loop=self.loop)

        @tasks.coroutine
        def set_err():
            buffer.set_exception(ValueError())

        t1 = tasks.Task(buffer.read(), loop=self.loop)
        t2 = tasks.Task(set_err(), loop=self.loop)

        self.loop.run_until_complete(tasks.wait([t1, t2], loop=self.loop))

        self.assertRaises(ValueError, t1.result)
示例#19
0
文件: parsers_test.py 项目: sah/tulip
    def test_exception_waiter(self):
        buffer = parsers.DataBuffer()

        @tasks.coroutine
        def set_err():
            buffer.set_exception(ValueError())

        t1 = tasks.Task(buffer.read())
        t2 = tasks.Task(set_err())

        self.loop.run_until_complete(tasks.wait([t1, t2]))

        self.assertRaises(ValueError, t1.result)
示例#20
0
    def test_wait_first_completed(self):
        a = tasks.Task(tasks.sleep(10.0, loop=self.loop), loop=self.loop)
        b = tasks.Task(tasks.sleep(0.1, loop=self.loop), loop=self.loop)
        task = tasks.Task(tasks.wait([b, a],
                                     return_when=tasks.FIRST_COMPLETED,
                                     loop=self.loop),
                          loop=self.loop)

        done, pending = self.loop.run_until_complete(task)
        self.assertEqual({b}, done)
        self.assertEqual({a}, pending)
        self.assertFalse(a.done())
        self.assertTrue(b.done())
        self.assertIsNone(b.result())
示例#21
0
文件: tasks_test.py 项目: sah/tulip
    def test_wait_first_exception_in_wait(self):
        # first_exception, exception during waiting
        a = tasks.Task(tasks.sleep(10.0))

        @tasks.coroutine
        def exc():
            yield from tasks.sleep(0.01)
            raise ZeroDivisionError('err')

        b = tasks.Task(exc())
        task = tasks.wait([b, a], return_when=tasks.FIRST_EXCEPTION)

        done, pending = self.loop.run_until_complete(task)
        self.assertEqual({b}, done)
        self.assertEqual({a}, pending)
示例#22
0
文件: tasks_test.py 项目: sah/tulip
    def test_wait_first_exception(self):
        # first_exception, task already has exception
        a = tasks.Task(tasks.sleep(10.0))

        @tasks.coroutine
        def exc():
            raise ZeroDivisionError('err')

        b = tasks.Task(exc())
        task = tasks.Task(tasks.wait(
            [b, a], return_when=tasks.FIRST_EXCEPTION))

        done, pending = self.loop.run_until_complete(task)
        self.assertEqual({b}, done)
        self.assertEqual({a}, pending)
示例#23
0
文件: streams_test.py 项目: sah/tulip
    def test_exception_waiter(self):
        stream = streams.StreamReader()

        @tasks.coroutine
        def set_err():
            stream.set_exception(ValueError())

        @tasks.coroutine
        def readline():
            yield from stream.readline()

        t1 = tasks.Task(stream.readline())
        t2 = tasks.Task(set_err())

        self.loop.run_until_complete(tasks.wait([t1, t2]))

        self.assertRaises(ValueError, t1.result)
示例#24
0
    def test_exception_waiter(self):
        stream = streams.StreamReader(loop=self.loop)

        @tasks.coroutine
        def set_err():
            stream.set_exception(ValueError())

        @tasks.coroutine
        def readline():
            yield from stream.readline()

        t1 = tasks.Task(stream.readline(), loop=self.loop)
        t2 = tasks.Task(set_err(), loop=self.loop)

        self.loop.run_until_complete(tasks.wait([t1, t2], loop=self.loop))

        self.assertRaises(ValueError, t1.result)
示例#25
0
    def test_wait_first_exception_in_wait(self):
        # first_exception, exception during waiting
        a = tasks.Task(tasks.sleep(10.0, loop=self.loop), loop=self.loop)

        @tasks.coroutine
        def exc():
            yield from tasks.sleep(0.01, loop=self.loop)
            raise ZeroDivisionError('err')

        b = tasks.Task(exc(), loop=self.loop)
        task = tasks.wait([b, a],
                          return_when=tasks.FIRST_EXCEPTION,
                          loop=self.loop)

        done, pending = self.loop.run_until_complete(task)
        self.assertEqual({b}, done)
        self.assertEqual({a}, pending)
示例#26
0
    def test_as_completed_concurrent(self):
        def gen():
            when = yield
            self.assertAlmostEqual(0.05, when)
            when = yield 0
            self.assertAlmostEqual(0.05, when)
            yield 0.05

        loop = test_utils.TestLoop(gen)
        self.addCleanup(loop.close)

        a = tasks.sleep(0.05, 'a', loop=loop)
        b = tasks.sleep(0.05, 'b', loop=loop)
        fs = {a, b}
        futs = list(tasks.as_completed(fs, loop=loop))
        self.assertEqual(len(futs), 2)
        waiter = tasks.wait(futs, loop=loop)
        done, pending = loop.run_until_complete(waiter)
        self.assertEqual(set(f.result() for f in done), {'a', 'b'})
示例#27
0
    def test_as_completed_concurrent(self):

        def gen():
            when = yield
            self.assertAlmostEqual(0.05, when)
            when = yield 0
            self.assertAlmostEqual(0.05, when)
            yield 0.05

        loop = test_utils.TestLoop(gen)
        self.addCleanup(loop.close)

        a = tasks.sleep(0.05, 'a', loop=loop)
        b = tasks.sleep(0.05, 'b', loop=loop)
        fs = {a, b}
        futs = list(tasks.as_completed(fs, loop=loop))
        self.assertEqual(len(futs), 2)
        waiter = tasks.wait(futs, loop=loop)
        done, pending = loop.run_until_complete(waiter)
        self.assertEqual(set(f.result() for f in done), {'a', 'b'})
示例#28
0
    def test_as_completed_with_timeout(self):

        def gen():
            when = yield
            self.assertAlmostEqual(0.12, when)
            when = yield 0
            self.assertAlmostEqual(0.1, when)
            when = yield 0
            self.assertAlmostEqual(0.15, when)
            when = yield 0.1
            self.assertAlmostEqual(0.12, when)
            yield 0.02

        loop = test_utils.TestLoop(gen)
        self.addCleanup(loop.close)

        a = tasks.sleep(0.1, 'a', loop=loop)
        b = tasks.sleep(0.15, 'b', loop=loop)

        @tasks.coroutine
        def foo():
            values = []
            for f in tasks.as_completed([a, b], timeout=0.12, loop=loop):
                try:
                    v = yield from f
                    values.append((1, v))
                except futures.TimeoutError as exc:
                    values.append((2, exc))
            return values

        res = loop.run_until_complete(tasks.Task(foo(), loop=loop))
        self.assertEqual(len(res), 2, res)
        self.assertEqual(res[0], (1, 'a'))
        self.assertEqual(res[1][0], 2)
        self.assertTrue(isinstance(res[1][1], futures.TimeoutError))
        self.assertAlmostEqual(0.12, loop.time())

        # move forward to close generator
        loop.advance_time(10)
        loop.run_until_complete(tasks.wait([a, b], loop=loop))
示例#29
0
    def test_as_completed_with_timeout(self):
        def gen():
            when = yield
            self.assertAlmostEqual(0.12, when)
            when = yield 0
            self.assertAlmostEqual(0.1, when)
            when = yield 0
            self.assertAlmostEqual(0.15, when)
            when = yield 0.1
            self.assertAlmostEqual(0.12, when)
            yield 0.02

        loop = test_utils.TestLoop(gen)
        self.addCleanup(loop.close)

        a = tasks.sleep(0.1, 'a', loop=loop)
        b = tasks.sleep(0.15, 'b', loop=loop)

        @tasks.coroutine
        def foo():
            values = []
            for f in tasks.as_completed([a, b], timeout=0.12, loop=loop):
                try:
                    v = yield from f
                    values.append((1, v))
                except futures.TimeoutError as exc:
                    values.append((2, exc))
            return values

        res = loop.run_until_complete(tasks.Task(foo(), loop=loop))
        self.assertEqual(len(res), 2, res)
        self.assertEqual(res[0], (1, 'a'))
        self.assertEqual(res[1][0], 2)
        self.assertTrue(isinstance(res[1][1], futures.TimeoutError))
        self.assertAlmostEqual(0.12, loop.time())

        # move forward to close generator
        loop.advance_time(10)
        loop.run_until_complete(tasks.wait([a, b], loop=loop))
示例#30
0
 def foo():
     done, pending = yield from tasks.wait([b, a], timeout=0.11,
                                           loop=loop)
     self.assertEqual(done, set([a]))
     self.assertEqual(pending, set([b]))
示例#31
0
 def foo():
     done, pending = yield from tasks.wait([b, a],
                                           timeout=0.11,
                                           loop=self.loop)
     self.assertEqual(done, set([a]))
     self.assertEqual(pending, set([b]))
示例#32
0
 def outer():
     nonlocal proof
     d, p = yield from tasks.wait([inner()], loop=self.loop)
     proof += 100
示例#33
0
 def foo():
     done, pending = yield from tasks.wait([b, a])
     self.assertEqual(done, set([a, b]))
     self.assertEqual(pending, set())
     return 42
示例#34
0
 def outer():
     nonlocal proof
     d, p = yield from tasks.wait([inner()], loop=self.loop)
     proof += 100
示例#35
0
 def foo():
     done, pending = yield from tasks.wait([b, a], loop=self.loop)
     self.assertEqual(done, set([a, b]))
     self.assertEqual(pending, set())
     return 42
示例#36
0
 def foo():
     done, pending = yield from tasks.wait([b, a], loop=loop)
     self.assertEqual(len(done), 2)
     self.assertEqual(pending, set())
     errors = set(f for f in done if f.exception() is not None)
     self.assertEqual(len(errors), 1)
示例#37
0
 def foo():
     done, pending = yield from tasks.wait([b, a], loop=self.loop)
     self.assertEqual(len(done), 2)
     self.assertEqual(pending, set())
     errors = set(f for f in done if f.exception() is not None)
     self.assertEqual(len(errors), 1)