async def test_merge_streams(): xs = AsyncStream() s1 = AsyncStream() s2 = AsyncStream() ys = merge(xs) obv = AsyncAnonymousObserver() await subscribe(ys, obv) await xs.asend(s1) await xs.asend(s2) await s1.asend_at(1, 10) await s1.asend_at(2, 20) await s1.asend_at(4, 30) await s1.aclose_at(6) await s2.asend_at(0, 40) await s2.asend_at(3, 50) await s2.asend_at(5, 60) await s2.aclose_at(6) await xs.aclose() await obv assert obv.values == [(0, 40), (1, 10), (2, 20), (3, 50), (4, 30), (5, 60), (6, )]
async def test_stream_chain_observer(): xs = AsyncStream() obv = AsyncAnonymousObserver() await chain(xs, obv) await xs.asend_later(1, 10) await xs.asend_later(1, 20) assert obv.values == [(1, 10), (2, 20)]
async def test_stream_chain_observer(): xs = AsyncStream() sink = AnonymousAsyncObserver() await chain(xs, sink) await xs.asend_later(1, 10) await xs.asend_later(1, 20) assert sink.values == [(1, 10), (2, 20)]
async def test_stream_happy(): xs = AsyncStream() sink = AnonymousAsyncObserver() await subscribe(xs, sink) await xs.asend_later(1, 10) await xs.asend_later(1, 20) await xs.asend_later(1, 30) assert sink.values == [(1, 10), (2, 20), (3, 30)]
async def test_stream_happy() -> None: xs = AsyncStream() obv = AsyncAnonymousObserver() await subscribe(xs, obv) await xs.asend_later(1, 10) await xs.asend_later(1, 20) await xs.asend_later(1, 30) assert obv.values == [(1, 10), (2, 20), (3, 30)]
async def test_stream_cancel_context(): xs = AsyncStream() sink = AnonymousAsyncObserver() with await subscribe(xs, sink): pass await xs.asend_later(1, 10) await xs.asend_later(1, 20) assert sink.values == []
async def test_stream_cancel_context(): xs = AsyncStream() obv = AsyncAnonymousObserver() async with subscribe(xs, obv): pass await xs.asend_later(1, 10) await xs.asend_later(1, 20) assert obv.values == []
async def test_delay_done(): xs = AsyncStream() ys = delay(0.5, xs) lis = AnonymousAsyncObserver() sub = await subscribe(ys, lis) await xs.asend_later(0, 10) await xs.asend_later(1, 20) await xs.aclose_later(1) await sub assert lis.values == [(0.5, 10), (1.5, 20), (2.5, )]
async def test_delay_done(): xs = AsyncStream() ys = delay(0.5, xs) obv = AsyncAnonymousObserver() async with subscribe(ys, obv): await xs.asend_later(0, 10) await xs.asend_later(1, 20) await xs.aclose_later(1) await obv assert obv.values == [(0.5, 10), (1.5, 20), (2.5, )]
async def test_stream_send_after_close(): xs = AsyncStream() sink = AnonymousAsyncObserver() await subscribe(xs, sink) await xs.asend_later(1, 10) await xs.asend_later(1, 20) await xs.asend_later(1, 30) await xs.aclose_later(2) await xs.asend_later(1, 40) assert sink.values == [(1, 10), (2, 20), (3, 30), (5, )]
async def test_merge_done(): xs = AsyncStream() ys = merge(xs) obv = AsyncAnonymousObserver() await subscribe(ys, obv) await xs.asend(from_iterable([10])) await xs.asend(from_iterable([20])) await xs.aclose() await obv assert obv.values == [(0, 10), (0, 20), (0, )]
async def test_merge_done(): xs = AsyncStream() ys = merge(xs) sink = AnonymousAsyncObserver() sub = await subscribe(ys, sink) await xs.asend(from_iterable([10])) await xs.asend(from_iterable([20])) await xs.aclose() await sub assert sink.values == [(0, 10), (0, 20), (0, )]
async def test_stream_send_after_close() -> None: xs = AsyncStream() obv = AsyncAnonymousObserver() await subscribe(xs, obv) await xs.asend_later(1, 10) await xs.asend_later(1, 20) await xs.asend_later(1, 30) await xs.aclose_later(2) await xs.asend_later(1, 40) assert obv.values == [(1, 10), (2, 20), (3, 30), (5, )]
async def test_stream_throws(): ex = MyException("ex") xs = AsyncStream() sink = AnonymousAsyncObserver() with pytest.raises(MyException): sub = await subscribe(xs, sink) await xs.asend_later(1, 10) await xs.asend_later(1, 20) await xs.asend_later(1, 30) await xs.athrow_later(1, ex) await xs.asend_later(1, 40) await sub assert sink.values == [(1, 10), (2, 20), (3, 30), (4, ex)]
async def test_stream_cancel(): xs = AsyncStream() stream = None def mapper(value): return value * 10 ys = map(mapper, xs) sink = AnonymousAsyncObserver() stream = await subscribe(ys, sink) await xs.asend_later(1, 10) stream.cancel() await xs.asend_later(1, 20) assert sink.values == [(1, 100)]
async def test_stream_cancel() -> None: xs = AsyncStream() subscription = None def mapper(value) -> int: return value * 10 ys = map(mapper, xs) obv = AsyncAnonymousObserver() subscription = await subscribe(ys, obv) await xs.asend_later(1, 10) await subscription.adispose() await xs.asend_later(1, 20) assert obv.values == [(1, 100)]
async def test_delay_cancel_before_done(): xs = AsyncStream() result = [] async def asend(value): nonlocal result result.append(value) ys = delay(0.3, xs) async with subscribe(ys, AsyncAnonymousObserver(asend)): await xs.asend(10) await asyncio.sleep(1.5) await xs.asend(20) await asyncio.sleep(1) assert result == [10]
async def test_delay_throw(): xs = AsyncStream() result = [] async def asend(value): nonlocal result result.append(value) ys = delay(0.3, xs) await subscribe(ys, AsyncAnonymousObserver(asend)) await xs.asend(10) await asyncio.sleep(1.5) await xs.asend(20) await xs.athrow(Exception('ex')) await asyncio.sleep(1) assert result == [10]
async def test_stream_throws() -> None: ex = MyException("ex") xs = AsyncStream() obv = AsyncAnonymousObserver() with pytest.raises(MyException): await (xs > obv) await xs.asend_later(1, 10) await xs.asend_later(1, 20) await xs.asend_later(1, 30) await xs.athrow_later(1, ex) await xs.asend_later(1, 40) await obv assert obv.values == [(1, 10), (2, 20), (3, 30), (4, ex)]
async def test_stream_cancel_asend(): xs = AsyncStream() stream = None async def asend(value): stream.cancel() await asyncio.sleep(0) def mapper(value): return value * 10 ys = map(mapper, xs) sink = AnonymousAsyncObserver(asend) async with subscribe(ys, sink) as stream: await xs.asend_later(1, 10) await xs.asend_later(1, 20) assert sink.values == [(1, 100)]
async def test_merge_streams_concat(): s1 = AsyncStream() s2 = from_iterable([1, 2, 3]) xs = from_iterable([s1, s2]) ys = merge(xs, 1) obv = AsyncAnonymousObserver() await subscribe(ys, obv) await s1.asend_at(1, 10) await s1.asend_at(2, 20) await s1.asend_at(4, 30) await s1.aclose_at(6) await obv assert obv.values == [(1, 10), (2, 20), (4, 30), (6, 1), (6, 2), (6, 3), (6, )]
async def test_stream_cancel_asend() -> None: xs = AsyncStream() subscription = None async def asend(value) -> None: await subscription.adispose() await asyncio.sleep(0) def mapper(value) -> int: return value * 10 ys = map(mapper, xs) obv = AsyncAnonymousObserver(asend) async with subscribe(ys, obv) as sub: subscription = sub await xs.asend_later(1, 10) await xs.asend_later(1, 20) assert obv.values == [(1, 100)]
async def test_stream_cancel_mapper(): xs = AsyncStream() subscription = None def mapper(value): asyncio.ensure_future(subscription.adispose()) return value * 10 ys = map(mapper, xs) obv = AsyncAnonymousObserver() async with subscribe(ys, obv) as subscription: await xs.asend_later(100, 10) await xs.asend_later(100, 20) await xs.asend_later(100, 30) await xs.asend_later(100, 40) await xs.asend_later(100, 50) await xs.asend_later(100, 60) assert obv.values == [(100, 100)]