Example #1
0
    async def test_get_nowait_throw_first_in_buffer(self):
        flow_control = FlowControlEvent(initially_suspended=False)
        queue = ThrowableQueue(flow_control=flow_control)

        await queue.put(1)
        await queue.put(2)
        assert queue.get_nowait() == 1
        assert queue.get_nowait() == 2
        await queue.put(3)
        await queue.put(4)
        await queue.throw(KeyError('foo'))
        with pytest.raises(KeyError):
            queue.get_nowait()
        assert queue.get_nowait() == 3
        assert queue.get_nowait() == 4
        await queue.throw(ValueError('bar'))
        with pytest.raises(ValueError):
            queue.get_nowait()
        queue.clear()
Example #2
0
    async def test_clear__cancels_waiting_putter(self):
        flow_control = FlowControlEvent(initially_suspended=False)
        queue = ThrowableQueue(flow_control=flow_control, maxsize=1)

        await queue.put(1)

        async def clear_queue():
            queue.clear()

        asyncio.ensure_future(clear_queue())
        with pytest.raises(asyncio.CancelledError):
            await queue.put(1)
Example #3
0
 def FlowControlQueue(
         self,
         maxsize: int = None,
         *,
         clear_on_resume: bool = False,
         loop: asyncio.AbstractEventLoop = None) -> ThrowableQueue:
     """Like :class:`asyncio.Queue`, but can be suspended/resumed."""
     return ThrowableQueue(
         maxsize=maxsize,
         flow_control=self.flow_control,
         clear_on_resume=clear_on_resume,
         loop=loop or self.loop,
     )
Example #4
0
    async def test_throw__notify_pending_waiters(self):
        flow_control = FlowControlEvent(initially_suspended=False)
        queue = ThrowableQueue(flow_control=flow_control, maxsize=1)
        raised = 0

        async def waiter():
            try:
                await queue.get()
            except KeyError:
                nonlocal raised
                raised += 1

        queue._getters.append(done_future())

        fut = asyncio.ensure_future(waiter())
        await asyncio.sleep(0.01)
        await queue.throw(KeyError())
        await asyncio.gather(fut)

        assert raised == 1
Example #5
0
    def test_get_nowait_empty(self):
        flow_control = FlowControlEvent(initially_suspended=False)
        queue = ThrowableQueue(flow_control=flow_control)

        with pytest.raises(asyncio.QueueEmpty):
            queue.get_nowait()