コード例 #1
0
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, )]
コード例 #2
0
ファイル: test_stream.py プロジェクト: tr11/aioreactive
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)]
コード例 #3
0
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)]
コード例 #4
0
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)]
コード例 #5
0
ファイル: test_stream.py プロジェクト: tr11/aioreactive
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)]
コード例 #6
0
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 == []
コード例 #7
0
ファイル: test_stream.py プロジェクト: tr11/aioreactive
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 == []
コード例 #8
0
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, )]
コード例 #9
0
ファイル: test_delay.py プロジェクト: tr11/aioreactive
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, )]
コード例 #10
0
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, )]
コード例 #11
0
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, )]
コード例 #12
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, )]
コード例 #13
0
ファイル: test_stream.py プロジェクト: tr11/aioreactive
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, )]
コード例 #14
0
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)]
コード例 #15
0
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)]
コード例 #16
0
ファイル: test_stream.py プロジェクト: tr11/aioreactive
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)]
コード例 #17
0
ファイル: test_delay.py プロジェクト: tr11/aioreactive
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]
コード例 #18
0
ファイル: test_delay.py プロジェクト: tr11/aioreactive
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]
コード例 #19
0
ファイル: test_stream.py プロジェクト: tr11/aioreactive
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)]
コード例 #20
0
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)]
コード例 #21
0
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, )]
コード例 #22
0
ファイル: test_stream.py プロジェクト: tr11/aioreactive
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)]
コード例 #23
0
ファイル: test_stream.py プロジェクト: tr11/aioreactive
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)]