예제 #1
0
    def flow_control(self) -> FlowControlEvent:
        """Internal flow control.

        This object controls flow into stream queues,
        and can also clear all buffers.
        """
        return FlowControlEvent(loop=self.loop)
예제 #2
0
 async def test_suspend_resume__initially_suspended(self):
     flow_control = FlowControlEvent(initially_suspended=True)
     queue = FlowControlQueue(flow_control=flow_control)
     asyncio.ensure_future(self._resume_soon(0.2, flow_control))
     time_now = monotonic()
     await queue.put(1)
     assert await queue.get() == 1
     assert monotonic() - time_now > 0.1
예제 #3
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)
예제 #4
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=FlowControlEvent(initially_suspended=False, loop=self.loop),
            clear_on_resume=clear_on_resume,
            loop=loop or self.loop,
        )
예제 #5
0
 async def test_suspend_resume(self):
     flow_control = FlowControlEvent()
     queue = FlowControlQueue(flow_control=flow_control)
     flow_control.resume()
     await queue.put(1)
     flow_control.suspend()
     asyncio.ensure_future(self._resume_soon(0.2, flow_control))
     time_now = monotonic()
     await queue.put(2)
     assert monotonic() - time_now > 0.1
     assert await queue.get() == 1
     assert await queue.get() == 2
예제 #6
0
    async def test_get__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 await queue.get() == 1
        assert await queue.get() == 2
        await queue.put(3)
        await queue.put(4)
        await queue.throw(KeyError('foo'))
        with pytest.raises(KeyError):
            await queue.get()
        assert await queue.get() == 3
        assert await queue.get() == 4
        await queue.throw(ValueError('bar'))
        with pytest.raises(ValueError):
            await queue.get()
예제 #7
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
예제 #8
0
 def flow_control(self) -> FlowControlEvent:
     return FlowControlEvent(loop=self.loop)
예제 #9
0
 async def test_suspend_resume__initially_resumed(self):
     flow_control = FlowControlEvent(initially_suspended=False)
     queue = FlowControlQueue(flow_control=flow_control)
     await queue.put(1)
     assert await queue.get() == 1
예제 #10
0
 def test_loop__custom(self):
     loop = Mock(name='loop')
     assert FlowControlEvent(loop=loop).loop is loop
예제 #11
0
 def test_loop__default(self):
     assert FlowControlEvent().loop is None
예제 #12
0
 def test_constructor(self):
     assert not FlowControlEvent(initially_suspended=True).is_active()
     assert FlowControlEvent(initially_suspended=False).is_active()
예제 #13
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()