def test_wrong_source(): p = Publisher() on_emit_future = OnEmitFuture() p | on_emit_future with pytest.raises(ValueError): on_emit_future.emit(0, who=Publisher())
async def test_timeout(): p = Publisher() future = p | OnEmitFuture(timeout=0.01) await asyncio.sleep(0.05) with pytest.raises(asyncio.TimeoutError): future.result()
def wait_for(self, timeout=None): """ When a timeout should be applied for awaiting use this method. :param timeout: optional timeout in seconds. :returns: a future returning the emitted value """ from broqer.op import OnEmitFuture # due circular dependency return self | OnEmitFuture(timeout=timeout)
async def test_cancel(): p = Publisher() future = p | OnEmitFuture(timeout=0.01) future.cancel() p.notify(1) with pytest.raises(asyncio.CancelledError): future.result()
def test_stateful_publisher(): p = StatefulPublisher(1) future = p | OnEmitFuture(timeout=1, loop=asyncio.get_event_loop()) assert future.result() == 1 p.notify(2) assert future.result() == 1
def test_publisher(): p = Publisher() future = p | OnEmitFuture() assert not future.done() p.notify(1) assert future.result() == 1 p.notify(2) assert future.result() == 1
def __await__(self): """ Publishers are awaitable and the future is done when the publisher emits a value """ from broqer.op import OnEmitFuture # due circular dependency return (self | OnEmitFuture(timeout=None)).__await__()