Ejemplo n.º 1
0
    def test_task_done(self):
        q = queues.JoinableQueue(loop=self.loop)
        for i in range(100):
            q.put_nowait(i)

        accumulator = 0

        # Two workers get items from the queue and call task_done after each.
        # Join the queue and assert all items have been processed.

        @tasks.coroutine
        def worker():
            nonlocal accumulator

            while True:
                item = yield from q.get()
                accumulator += item
                q.task_done()

        @tasks.coroutine
        def test():
            for _ in range(2):
                tasks.Task(worker(), loop=self.loop)

            yield from q.join()

        self.loop.run_until_complete(test())
        self.assertEqual(sum(range(100)), accumulator)
Ejemplo n.º 2
0
    def test_join_timeout(self):
        q = queues.JoinableQueue(loop=self.loop)
        q.put_nowait(1)

        @tasks.coroutine
        def join():
            yield from q.join(0.1)

        # Join completes in ~ 0.1 seconds, although no one calls task_done().
        self.loop.run_until_complete(join())
Ejemplo n.º 3
0
    def test_join_empty_queue(self):
        q = queues.JoinableQueue(loop=self.loop)

        # Test that a queue join()s successfully, and before anything else
        # (done twice for insurance).

        @tasks.coroutine
        def join():
            yield from q.join()
            yield from q.join()

        self.loop.run_until_complete(join())
Ejemplo n.º 4
0
    def test_format(self):
        q = queues.JoinableQueue(loop=self.loop)
        self.assertEqual(q._format(), 'maxsize=0')

        q._unfinished_tasks = 2
        self.assertEqual(q._format(), 'maxsize=0 tasks=2')
Ejemplo n.º 5
0
 def test_task_done_underflow(self):
     q = queues.JoinableQueue(loop=self.loop)
     self.assertRaises(ValueError, q.task_done)