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_distinct_until_changed_different(): xs = from_iterable([1, 2, 3]) obv = AsyncAnonymousObserver() ys = distinct_until_changed(xs) await run(ys, obv) assert obv.values == [(0, 1), (0, 2), (0, 3), (0, )]
async def test_unit_happy_resolved_future(): fut = asyncio.Future() xs = unit(fut) fut.set_result(42) obv = AsyncAnonymousObserver() await run(xs, obv) assert obv.values == [(0, 42), (0, )]
async def test_unit_happy(): xs = unit(42) result = [] async def asend(value): result.append(value) await run(xs, AsyncAnonymousObserver(asend)) assert result == [42]
async def test_unit_happy_future_resolve(): fut = asyncio.Future() xs = unit(fut) obv = AsyncAnonymousObserver() async with subscribe(xs, obv): fut.set_result(42) await obv assert obv.values == [(0, 42), (0, )]
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_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_happy(): xs = AsyncSingleStream() sink = AsyncAnonymousObserver() 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_cancel_context(): xs = AsyncSingleStream() sink = AsyncAnonymousObserver() async with 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_unit_future_exception(): fut = asyncio.Future() ex = Exception("ex") xs = unit(fut) obv = AsyncAnonymousObserver() async with subscribe(xs, obv): fut.set_exception(ex) with pytest.raises(Exception): await obv assert obv.values == [(0, ex)]
async def test_unit_future_cancel(): fut = asyncio.Future() xs = unit(fut) obv = AsyncAnonymousObserver() async with subscribe(xs, obv): fut.cancel() with pytest.raises(asyncio.CancelledError): await obv assert obv.values == [(0, )]
async def test_stream_send_after_close(): xs = AsyncSingleStream() sink = AsyncAnonymousObserver() 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_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() -> 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_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_stream_cold_close(): xs = AsyncSingleStream() sink = AsyncAnonymousObserver() async def aclose(): await xs.aclose() asyncio.ensure_future(aclose()) await asyncio.sleep(10) async with subscribe(xs, sink): await xs.asend_later(1, 20) assert sink.values == [(10, )]
async def test_stream_throws(): ex = MyException("ex") xs = AsyncSingleStream() sink = AsyncAnonymousObserver() with pytest.raises(MyException): 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 sink assert sink.values == [(1, 10), (2, 20), (3, 30), (4, ex)]
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_stream_cancel(): xs = AsyncSingleStream() sub = None def mapper(value): return value * 10 ys = map(mapper, xs) sink = AsyncAnonymousObserver() sub = await subscribe(ys, sink) await xs.asend_later(1, 10) await sub.adispose() 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_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_unit_observer_throws(): error = Exception("error") xs = unit(42) result = [] def asend(value): result.append(value) raise error obv = AsyncAnonymousObserver(asend) await subscribe(xs, obv) try: await obv except Exception as ex: assert ex == error assert result == [42]
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_unit_close(): xs = unit(42) result = [] sub = None async def asend(value): result.append(value) await sub.adispose() await asyncio.sleep(0) obv = AsyncAnonymousObserver(asend) sub = await subscribe(xs, obv) try: await obv except asyncio.CancelledError: pass assert result == [42]
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(): xs = AsyncSingleStream() sub = None async def asend(value): await sub.adispose() await asyncio.sleep(0) def mapper(value): return value * 10 ys = map(mapper, xs) sink = AsyncAnonymousObserver(asend) async with subscribe(ys, sink) as sub: await xs.asend_later(1, 10) await xs.asend_later(1, 20) assert sink.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)]
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_flap_map_done(): xs = AsyncStream() result = [] async def asend(value): nonlocal result result.append(value) async def mapper(value): return from_iterable([value]) ys = flat_map(mapper, xs) obv = AsyncAnonymousObserver() await subscribe(ys, obv) await xs.asend(10) await xs.asend(20) await xs.aclose() await obv assert obv.values == [(0, 10), (0, 20), (0, )]