Ejemplo n.º 1
0
    def test_ctor_loop(self):
        loop = mock.Mock()
        q = asyncio.Queue(loop=loop)
        self.assertIs(q._loop, loop)

        q = asyncio.Queue(loop=self.loop)
        self.assertIs(q._loop, self.loop)
Ejemplo n.º 2
0
    def test_full(self):
        q = asyncio.Queue(loop=self.loop)
        self.assertFalse(q.full())

        q = asyncio.Queue(maxsize=1, loop=self.loop)
        q.put_nowait(1)
        self.assertTrue(q.full())
Ejemplo n.º 3
0
    def _test_repr_or_str(self, fn, expect_id):
        """Test Queue's repr or str.

        fn is repr or str. expect_id is True if we expect the Queue's id to
        appear in fn(Queue()).
        """
        def gen():
            when = yield
            self.assertAlmostEqual(0.1, when)
            when = yield 0.1
            self.assertAlmostEqual(0.2, when)
            yield 0.1

        loop = self.new_test_loop(gen)

        q = asyncio.Queue(loop=loop)
        self.assertTrue(fn(q).startswith('<Queue'), fn(q))
        id_is_present = ("%x" % id(q)) in fn(q)
        self.assertEqual(expect_id, id_is_present)

        @asyncio.coroutine
        def add_getter():
            q = asyncio.Queue(loop=loop)
            # Start a task that waits to get.
            asyncio.Task(q.get(), loop=loop)
            # Let it start waiting.
            yield From(asyncio.sleep(0.1, loop=loop))
            self.assertTrue('_getters[1]' in fn(q))
            # resume q.get coroutine to finish generator
            q.put_nowait(0)

        loop.run_until_complete(add_getter())

        @asyncio.coroutine
        def add_putter():
            q = asyncio.Queue(maxsize=1, loop=loop)
            q.put_nowait(1)
            # Start a task that waits to put.
            asyncio.Task(q.put(2), loop=loop)
            # Let it start waiting.
            yield From(asyncio.sleep(0.1, loop=loop))
            self.assertTrue('_putters[1]' in fn(q))
            # resume q.put coroutine to finish generator
            q.get_nowait()

        loop.run_until_complete(add_putter())

        q = asyncio.Queue(loop=loop)
        q.put_nowait(1)
        self.assertTrue('_queue=[1]' in fn(q))
Ejemplo n.º 4
0
    def test_get_cancel_drop_many_pending_readers(self):
        def gen():
            yield 0.01
            yield 0.1

        loop = self.new_test_loop(gen)
        loop.set_debug(True)

        q = asyncio.Queue(loop=loop)

        reader1 = loop.create_task(q.get())
        reader2 = loop.create_task(q.get())
        reader3 = loop.create_task(q.get())

        loop.run_until_complete(asyncio.sleep(0.01, loop=loop))

        q.put_nowait(1)
        q.put_nowait(2)
        reader1.cancel()

        try:
            loop.run_until_complete(reader1)
        except asyncio.CancelledError:
            pass

        loop.run_until_complete(reader3)

        # reader2 will receive `2`, because it was added to the
        # queue of pending readers *before* put_nowaits were called.
        self.assertEqual(reader2.result(), 2)
        # reader3 will receive `1`, because reader1 was cancelled
        # before is had a chance to execute, and `2` was already
        # pushed to reader2 by second `put_nowait`.
        self.assertEqual(reader3.result(), 1)
Ejemplo n.º 5
0
    def test_get_cancel_drop_one_pending_reader(self):
        def gen():
            yield 0.01
            yield 0.1

        loop = self.new_test_loop(gen)

        q = asyncio.Queue(loop=loop)

        reader = loop.create_task(q.get())

        loop.run_until_complete(asyncio.sleep(0.01, loop=loop))

        q.put_nowait(1)
        q.put_nowait(2)
        reader.cancel()

        try:
            loop.run_until_complete(reader)
        except asyncio.CancelledError:
            # try again
            reader = loop.create_task(q.get())
            loop.run_until_complete(reader)

        result = reader.result()
        # if we get 2, it means 1 got dropped!
        self.assertEqual(1, result)
Ejemplo n.º 6
0
    def test_put_cancel_drop(self):
        def gen():
            yield 0.01
            yield 0.1

        loop = self.new_test_loop(gen)
        q = asyncio.Queue(1, loop=loop)

        q.put_nowait(1)

        # putting a second item in the queue has to block (qsize=1)
        writer = loop.create_task(q.put(2))
        loop.run_until_complete(asyncio.sleep(0.01, loop=loop))

        value1 = q.get_nowait()
        self.assertEqual(value1, 1)

        writer.cancel()
        try:
            loop.run_until_complete(writer)
        except asyncio.CancelledError:
            # try again
            writer = loop.create_task(q.put(2))
            loop.run_until_complete(writer)

        value2 = q.get_nowait()
        self.assertEqual(value2, 2)
        self.assertEqual(q.qsize(), 0)
Ejemplo n.º 7
0
 def test_get_with_waiting_putters(self):
     q = asyncio.Queue(loop=self.loop, maxsize=1)
     asyncio.Task(q.put('a'), loop=self.loop)
     asyncio.Task(q.put('b'), loop=self.loop)
     test_utils.run_briefly(self.loop)
     self.assertEqual(self.loop.run_until_complete(q.get()), 'a')
     self.assertEqual(self.loop.run_until_complete(q.get()), 'b')
Ejemplo n.º 8
0
    def test_order(self):
        q = asyncio.Queue(loop=self.loop)
        for i in [1, 3, 2]:
            q.put_nowait(i)

        items = [q.get_nowait() for _ in range(3)]
        self.assertEqual([1, 3, 2], items)
Ejemplo n.º 9
0
    def test_get_cancelled(self):
        def gen():
            when = yield
            self.assertAlmostEqual(0.01, when)
            when = yield 0.01
            self.assertAlmostEqual(0.061, when)
            yield 0.05

        loop = self.new_test_loop(gen)

        q = asyncio.Queue(loop=loop)

        @asyncio.coroutine
        def queue_get():
            result = (yield From(asyncio.wait_for(q.get(), 0.051, loop=loop)))
            raise Return(result)

        @asyncio.coroutine
        def test():
            get_task = asyncio.Task(queue_get(), loop=loop)
            yield From(asyncio.sleep(0.01, loop=loop))  # let the task start
            q.put_nowait(1)
            result = (yield From(get_task))
            raise Return(result)

        self.assertEqual(1, loop.run_until_complete(test()))
        self.assertAlmostEqual(0.06, loop.time())
Ejemplo n.º 10
0
    def test_blocking_put_wait(self):
        @asyncio.coroutine
        def gen():
            when = yield
            self.assertAlmostEqual(0.01, when)
            yield 0.01

        loop = self.new_test_loop(gen)

        q = asyncio.Queue(maxsize=1, loop=loop)
        started = asyncio.Event(loop=loop)
        non_local = {'finished': False}

        @asyncio.coroutine
        def queue_put():
            started.set()
            yield From(q.put(1))
            yield From(q.put(2))
            non_local['finished'] = True

        @asyncio.coroutine
        def queue_get():
            queue_put_task = asyncio.Task(queue_put(), loop=loop)
            yield From(None)
            loop.call_later(0.01, q.get_nowait)
            yield From(started.wait())
            self.assertFalse(non_local['finished'])
            yield From(queue_put_task)
            self.assertTrue(non_local['finished'])

        loop.run_until_complete(queue_get())
        self.assertAlmostEqual(0.01, loop.time())
Ejemplo n.º 11
0
    def test_blocking_get_wait(self):
        def gen():
            when = yield
            self.assertAlmostEqual(0.01, when)
            yield 0.01

        loop = self.new_test_loop(gen)

        q = asyncio.Queue(loop=loop)
        started = asyncio.Event(loop=loop)
        non_local = {'finished': False}

        @asyncio.coroutine
        def queue_get():
            started.set()
            res = yield From(q.get())
            non_local['finished'] = True
            raise Return(res)

        @asyncio.coroutine
        def queue_put():
            loop.call_later(0.01, q.put_nowait, 1)
            queue_get_task = asyncio.Task(queue_get(), loop=loop)
            yield From(started.wait())
            self.assertFalse(non_local['finished'])
            res = yield From(queue_get_task)
            self.assertTrue(non_local['finished'])
            raise Return(res)

        res = loop.run_until_complete(queue_put())
        self.assertEqual(1, res)
        self.assertAlmostEqual(0.01, loop.time())
Ejemplo n.º 12
0
    def test_float_maxsize(self):
        q = asyncio.Queue(maxsize=1.3, loop=self.loop)
        q.put_nowait(1)
        q.put_nowait(2)
        self.assertTrue(q.full())
        self.assertRaises(asyncio.QueueFull, q.put_nowait, 3)

        q = asyncio.Queue(maxsize=1.3, loop=self.loop)

        @asyncio.coroutine
        def queue_put():
            yield From(q.put(1))
            yield From(q.put(2))
            self.assertTrue(q.full())

        self.loop.run_until_complete(queue_put())
Ejemplo n.º 13
0
 def test_empty(self):
     q = asyncio.Queue(loop=self.loop)
     self.assertTrue(q.empty())
     q.put_nowait(1)
     self.assertFalse(q.empty())
     self.assertEqual(1, q.get_nowait())
     self.assertTrue(q.empty())
Ejemplo n.º 14
0
 def set_event_loop(self):
     self.loop = asyncio.new_event_loop()
     asyncio.set_event_loop(self.loop)
     self.events = asyncio.Queue(loop=self.loop)
     self.set_exception_handler()
     if self.loglevel is not None and self.loglevel <= logging.DEBUG:
         self.loop.set_debug(True)
     return self.loop
Ejemplo n.º 15
0
Archivo: worker.py Proyecto: gridl/dist
    def __init__(self,
                 ip,
                 port,
                 bind_ip,
                 metadata_addr,
                 context=context,
                 loop=None,
                 start=False):
        self.ip = ip
        self.port = port
        self.bind_ip = bind_ip
        self.metadata_addr = metadata_addr
        self.context = context
        self.data = dict()
        self.loop = loop or asyncio.get_event_loop()
        self.work_q = asyncio.Queue(loop=self.loop)
        self.send_q = asyncio.Queue(loop=self.loop)
        self.data_q = asyncio.Queue(loop=self.loop)
        self.pingpong_q = asyncio.Queue(loop=self.loop)
        self.outgoing_q = asyncio.Queue(loop=self.loop)
        self.control_q = asyncio.Queue(loop=self.loop)
        self.signal_q = asyncio.Queue(loop=self.loop)

        self.status = 'running'

        if start:
            self.start()
Ejemplo n.º 16
0
 def __init__(self, trans, loop=None):
     self._loop = loop or asyncio.get_event_loop()
     self._queue = asyncio.Queue(
         maxsize=self.MAX_QUEUE_SIZE,
         loop=self._loop,
     )
     self._trans = trans
     self._consumer = asyncio.Task(self._send(), loop=self._loop)
     self._producers = []
Ejemplo n.º 17
0
    def test_blocking_put(self):
        q = asyncio.Queue(loop=self.loop)

        @asyncio.coroutine
        def queue_put():
            # No maxsize, won't block.
            yield From(q.put(1))

        self.loop.run_until_complete(queue_put())
Ejemplo n.º 18
0
 def add_getter():
     q = asyncio.Queue(loop=loop)
     # Start a task that waits to get.
     asyncio.Task(q.get(), loop=loop)
     # Let it start waiting.
     yield From(asyncio.sleep(0.1, loop=loop))
     self.assertTrue('_getters[1]' in fn(q))
     # resume q.get coroutine to finish generator
     q.put_nowait(0)
Ejemplo n.º 19
0
    def test_blocking_get(self):
        q = asyncio.Queue(loop=self.loop)
        q.put_nowait(1)

        @asyncio.coroutine
        def queue_get():
            result = (yield From(q.get()))
            raise Return(result)

        res = self.loop.run_until_complete(queue_get())
        self.assertEqual(1, res)
Ejemplo n.º 20
0
    def test_get_with_putters(self):
        q = asyncio.Queue(1, loop=self.loop)
        q.put_nowait(1)

        waiter = asyncio.Future(loop=self.loop)
        q._putters.append((2, waiter))

        res = self.loop.run_until_complete(q.get())
        self.assertEqual(1, res)
        self.assertTrue(waiter.done())
        self.assertIsNone(waiter.result())
Ejemplo n.º 21
0
    def __init__(self):
        super().__init__()
        self.__concurrent = 1
        self._running = False
        self._item_queue = trollius.PriorityQueue()
        try:
            self._token_queue = trollius.JoinableQueue()
        except AttributeError:
            self._token_queue = trollius.Queue()
        self._item_get_semaphore = trollius.BoundedSemaphore(value=1)

        self._producer_task = None
        self._worker_tasks = set()
Ejemplo n.º 22
0
    def test_get_cancelled_race(self):
        q = asyncio.Queue(loop=self.loop)

        t1 = asyncio.Task(q.get(), loop=self.loop)
        t2 = asyncio.Task(q.get(), loop=self.loop)

        test_utils.run_briefly(self.loop)
        t1.cancel()
        test_utils.run_briefly(self.loop)
        self.assertTrue(t1.done())
        q.put_nowait('a')
        test_utils.run_briefly(self.loop)
        self.assertEqual(t2.result(), 'a')
Ejemplo n.º 23
0
    def test_put_cancelled(self):
        q = asyncio.Queue(loop=self.loop)

        @asyncio.coroutine
        def queue_put():
            yield From(q.put(1))
            raise Return(True)

        @asyncio.coroutine
        def test():
            result = (yield From(q.get()))
            raise Return(result)

        t = asyncio.Task(queue_put(), loop=self.loop)
        self.assertEqual(1, self.loop.run_until_complete(test()))
        self.assertTrue(t.done())
        self.assertTrue(t.result())
Ejemplo n.º 24
0
    def test_put_cancelled_race(self):
        q = asyncio.Queue(loop=self.loop, maxsize=1)

        put_a = asyncio.Task(q.put('a'), loop=self.loop)
        put_b = asyncio.Task(q.put('b'), loop=self.loop)
        put_c = asyncio.Task(q.put('X'), loop=self.loop)

        test_utils.run_briefly(self.loop)
        self.assertTrue(put_a.done())
        self.assertFalse(put_b.done())

        put_c.cancel()
        test_utils.run_briefly(self.loop)
        self.assertTrue(put_c.done())
        self.assertEqual(q.get_nowait(), 'a')
        self.assertEqual(q.get_nowait(), 'b')

        self.loop.run_until_complete(put_b)
Ejemplo n.º 25
0
    def test_maxsize(self):
        def gen():
            when = yield
            self.assertAlmostEqual(0.01, when)
            when = yield 0.01
            self.assertAlmostEqual(0.02, when)
            yield 0.01

        loop = self.new_test_loop(gen)

        q = asyncio.Queue(maxsize=2, loop=loop)
        self.assertEqual(2, q.maxsize)
        have_been_put = []

        @asyncio.coroutine
        def putter():
            for i in range(3):
                yield From(q.put(i))
                have_been_put.append(i)
            raise Return(True)

        @asyncio.coroutine
        def test():
            t = asyncio.Task(putter(), loop=loop)
            yield From(None)  # one extra iteration for the putter coroutine
            yield From(asyncio.sleep(0.01, loop=loop))

            # The putter is blocked after putting two items.
            self.assertEqual([0, 1], have_been_put)
            self.assertEqual(0, q.get_nowait())

            # Let the putter resume and put last item.
            yield From(asyncio.sleep(0.01, loop=loop))
            self.assertEqual([0, 1, 2], have_been_put)
            self.assertEqual(1, q.get_nowait())
            self.assertEqual(2, q.get_nowait())

            self.assertTrue(t.done())
            self.assertTrue(t.result())

        loop.run_until_complete(test())
        self.assertAlmostEqual(0.02, loop.time())
Ejemplo n.º 26
0
Archivo: center.py Proyecto: gridl/dist
    def __init__(self, ip, port, bind_ip,
                 context=context, loop=None, start=False):
        self.ip = ip
        self.port = port
        self.bind_ip = bind_ip
        self.context = context
        self.loop = loop or asyncio.get_event_loop()

        self.who_has = defaultdict(set)
        self.has_what = defaultdict(set)

        self.metadata_q = asyncio.Queue(loop=self.loop)
        self.control_q = asyncio.Queue(loop=self.loop)
        self.send_q = asyncio.Queue(loop=self.loop)
        self.pingpong_q = asyncio.Queue(loop=self.loop)
        self.outgoing_q = asyncio.Queue(loop=self.loop)
        self.signal_q = asyncio.Queue(loop=self.loop)

        self.status = 'running'

        if start:
            self.start()
Ejemplo n.º 27
0
 def test_ctor_noloop(self):
     asyncio.set_event_loop(self.loop)
     q = asyncio.Queue()
     self.assertIs(q._loop, self.loop)
Ejemplo n.º 28
0
 def test_nonblocking_put(self):
     q = asyncio.Queue(loop=self.loop)
     q.put_nowait(1)
     self.assertEqual(1, q.get_nowait())
Ejemplo n.º 29
0
 def test_put_with_waiting_getters(self):
     q = asyncio.Queue(loop=self.loop)
     t = asyncio.Task(q.get(), loop=self.loop)
     test_utils.run_briefly(self.loop)
     self.loop.run_until_complete(q.put('a'))
     self.assertEqual(self.loop.run_until_complete(t), 'a')
Ejemplo n.º 30
0
 def test_nonblocking_put_exception(self):
     q = asyncio.Queue(maxsize=1, loop=self.loop)
     q.put_nowait(1)
     self.assertRaises(asyncio.QueueFull, q.put_nowait, 2)