How to use janus - 10 common examples

To help you get started, we’ve selected a few janus examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github aio-libs / janus / tests / test_async.py View on Github external
async def test_blocking_get(self):
        _q = janus.Queue()
        q = _q.async_q
        q.put_nowait(1)

        res = await q.get()
        self.assertEqual(1, res)

        self.assertFalse(_q._sync_mutex.locked())
        _q.close()
        await _q.wait_closed()
github aio-libs / janus / tests / test_async.py View on Github external
async def test_nonblocking_get(self):
        _q = janus.Queue()
        q = _q.async_q
        q.put_nowait(1)
        self.assertEqual(1, q.get_nowait())

        _q.close()
        await _q.wait_closed()
github aio-libs / janus / tests / test_async.py View on Github external
async def test_float_maxsize(self):
        _q = janus.Queue(maxsize=1.3)
        q = _q.async_q
        q.put_nowait(1)
        q.put_nowait(2)
        self.assertTrue(q.full())
        self.assertRaises(asyncio.QueueFull, q.put_nowait, 3)

        _q.close()
        await _q.wait_closed()

        _q = janus.Queue(maxsize=1.3)
        q = _q.async_q

        async def queue_put():
            await q.put(1)
            await q.put(2)
            self.assertTrue(q.full())

        await queue_put()

        self.assertFalse(_q._sync_mutex.locked())
        _q.close()
        await _q.wait_closed()
github aio-libs / janus / tests / test_sync.py View on Github external
async def test_closed_loop_non_failing(self):
        loop = janus.current_loop()
        _q = janus.Queue(QUEUE_SIZE)
        q = _q.sync_q
        # we are pacthing loop to follow setUp/tearDown agreement
        with patch.object(loop, 'call_soon_threadsafe') as func:
            func.side_effect = RuntimeError()
            q.put_nowait(1)
            self.assertEqual(func.call_count, 1)
        _q.close()
        await _q.wait_closed()
github aio-libs / janus / tests / test_async.py View on Github external
async def test_get_with_putters(self):
        loop = janus.current_loop()
        _q = janus.Queue(1)
        q = _q.async_q
        q.put_nowait(1)

        fut = loop.create_future()

        async def put():
            t = asyncio.ensure_future(q.put(2))
            await asyncio.sleep(0.01)
            fut.set_result(None)
            return t

        t = await put()

        res = await q.get()
        self.assertEqual(1, res)
github aio-libs / janus / tests / test_async.py View on Github external
async def test_blocking_get_wait(self):
        loop = janus.current_loop()
        _q = janus.Queue()
        q = _q.async_q
        started = asyncio.Event()
        finished = False

        async def queue_get():
            nonlocal finished
            started.set()
            res = await q.get()
            finished = True
            return res

        async def queue_put():
            loop.call_later(0.01, q.put_nowait, 1)
            queue_get_task = loop.create_task(queue_get())
            await started.wait()
github aio-libs / janus / tests / test_async.py View on Github external
async def test_get_cancelled(self):
        loop = janus.current_loop()
        _q = janus.Queue()
        q = _q.async_q

        async def queue_get():
            return await asyncio.wait_for(q.get(), 0.051)

        async def test():
            get_task = loop.create_task(queue_get())
            await asyncio.sleep(0.01)  # let the task start
            q.put_nowait(1)
            return await get_task

        self.assertEqual(1, await test())

        self.assertFalse(_q._sync_mutex.locked())
        _q.close()
github aio-libs / janus / tests / test_sync.py View on Github external
    @pytest.mark.asyncio
    async def test_maxsize(self):
        # Test to make sure a queue task completed successfully.
        _q = self.type2test(5)
        q = _q.sync_q
        self.assertEqual(q.maxsize, 5)
        _q.close()
        await _q.wait_closed()


class QueueTest(BaseQueueTestMixin, unittest.TestCase):
    type2test = janus.Queue


class LifoQueueTest(BaseQueueTestMixin, unittest.TestCase):
    type2test = janus.LifoQueue


class PriorityQueueTest(BaseQueueTestMixin, unittest.TestCase):
    type2test = janus.PriorityQueue


# A Queue subclass that can provoke failure at a moment's notice :)
class FailingQueueException(Exception):
    pass


class FailingQueue(janus.Queue):
    def __init__(self, *args, **kwargs):
        self.fail_next_put = False
        self.fail_next_get = False
        super().__init__(*args, **kwargs)
github aio-libs / janus / tests / test_async.py View on Github external
async def test_order(self):
        _q = janus.LifoQueue()
        q = _q.async_q
        for i in [1, 3, 2]:
            q.put_nowait(i)

        items = [q.get_nowait() for _ in range(3)]
        self.assertEqual([2, 3, 1], items)

        self.assertFalse(_q._sync_mutex.locked())
        _q.close()
        await _q.wait_closed()
github aio-libs / janus / tests / test_async.py View on Github external
async def test_order(self):
        _q = janus.PriorityQueue()
        q = _q.async_q
        for i in [1, 3, 2]:
            q.put_nowait(i)

        items = [q.get_nowait() for _ in range(3)]
        self.assertEqual([1, 2, 3], items)

        self.assertFalse(_q._sync_mutex.locked())
        _q.close()
        await _q.wait_closed()