Exemple #1
0
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())
Exemple #2
0
async def test_timeout():
    p = Publisher()
    future = p | OnEmitFuture(timeout=0.01)
    await asyncio.sleep(0.05)

    with pytest.raises(asyncio.TimeoutError):
        future.result()
Exemple #3
0
 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)
Exemple #4
0
async def test_cancel():
    p = Publisher()
    future = p | OnEmitFuture(timeout=0.01)
    future.cancel()
    p.notify(1)

    with pytest.raises(asyncio.CancelledError):
        future.result()
Exemple #5
0
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
Exemple #6
0
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
Exemple #7
0
 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__()