How to use the janus.current_loop function in janus

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_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_mixed.py View on Github external
async def test_async_put_sync_get(self):
        loop = janus.current_loop()
        q = janus.Queue()

        def threaded():
            for i in range(5):
                val = q.sync_q.get()
                self.assertEqual(val, i)

        async def go():
            f = loop.run_in_executor(None, threaded)
            for i in range(5):
                await q.async_q.put(i)

            await f
            self.assertTrue(q.async_q.empty())

        for i in range(3):
github aio-libs / janus / tests / test_async.py View on Github external
async def test_put_with_waiting_getters(self):
        loop = janus.current_loop()
        fut = loop.create_future()

        async def go():
            fut.set_result(None)
            ret = await q.get()
            return ret

        async def put():
            await q.put('a')

        _q = janus.Queue()
        q = _q.async_q
        t = loop.create_task(go())
        await fut
        await put()
        self.assertEqual(await t, 'a')
github aio-libs / janus / tests / test_async.py View on Github external
async def test_put_cancelled_race(self):
        loop = janus.current_loop()
        _q = janus.Queue(maxsize=1)
        q = _q.async_q

        put_a = loop.create_task(q.put('a'))
        put_b = loop.create_task(q.put('b'))
        put_c = loop.create_task(q.put('X'))

        await put_a
        self.assertFalse(put_b.done())

        put_c.cancel()

        with self.assertRaises(asyncio.CancelledError):
            await put_c

        async def go():
github aio-libs / janus / tests / test_mixed.py View on Github external
async def test_sync_put_async_get(self):
        loop = janus.current_loop()
        q = janus.Queue()

        def threaded():
            for i in range(5):
                q.sync_q.put(i)

        async def go():
            f = loop.run_in_executor(None, threaded)
            for i in range(5):
                val = await q.async_q.get()
                self.assertEqual(val, i)

            self.assertTrue(q.async_q.empty())

            await f
github aio-libs / janus / tests / test_mixed.py View on Github external
async def test_sync_put_async_join(self):
        loop = janus.current_loop()
        q = janus.Queue()

        for i in range(5):
            q.sync_q.put(i)

        async def do_work():
            await asyncio.sleep(1)
            while True:
                await q.async_q.get()
                q.async_q.task_done()

        task = loop.create_task(do_work())

        async def wait_for_empty_queue():
            await q.async_q.join()
            task.cancel()