コード例 #1
0
async def test_merge_streams():
    xs = AsyncStream()
    s1 = AsyncStream()
    s2 = AsyncStream()

    ys = merge(xs)

    sink = AnonymousAsyncObserver()
    sub = await subscribe(ys, sink)
    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 sub

    assert sink.values == [(0, 40), (1, 10), (2, 20), (3, 50), (4, 30),
                           (5, 60), (6, )]
コード例 #2
0
async def test_unit_happy_resolved_future():
    fut = asyncio.Future()
    xs = unit(fut)
    fut.set_result(42)

    lis = AnonymousAsyncObserver()
    await run(xs, lis)
    assert lis.values == [(0, 42), (0, )]
コード例 #3
0
async def test_unit_happy():
    xs = unit(42)
    result = []

    async def asend(value):
        result.append(value)

    await run(xs, AnonymousAsyncObserver(asend))
    assert result == [42]
コード例 #4
0
async def test_unit_happy_future_resolve():
    fut = asyncio.Future()
    xs = unit(fut)

    lis = AnonymousAsyncObserver()
    sub = await subscribe(xs, lis)
    fut.set_result(42)
    await sub
    assert lis.values == [(0, 42), (0, )]
コード例 #5
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)]
コード例 #6
0
async def test_unit_future_cancel():
    fut = asyncio.Future()
    xs = unit(fut)

    lis = AnonymousAsyncObserver()
    sub = await subscribe(xs, lis)
    fut.cancel()
    with pytest.raises(asyncio.CancelledError):
        await sub
    assert lis.values == [(0, )]
コード例 #7
0
async def test_stream_happy():
    xs = AsyncSingleStream()

    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)]
コード例 #8
0
async def test_unit_future_exception():
    fut = asyncio.Future()
    ex = Exception("ex")
    xs = unit(fut)

    lis = AnonymousAsyncObserver()
    sub = await subscribe(xs, lis)
    fut.set_exception(ex)
    with pytest.raises(Exception):
        await sub
    assert lis.values == [(0, ex)]
コード例 #9
0
async def test_stream_cancel_context():
    xs = AsyncSingleStream()

    sink = AnonymousAsyncObserver()
    with await subscribe(xs, sink):
        pass

    await xs.asend_later(1, 10)
    await xs.asend_later(1, 20)

    assert sink.values == []
コード例 #10
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, )]
コード例 #11
0
async def test_stream_send_after_close():
    xs = AsyncSingleStream()

    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, )]
コード例 #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
async def test_stream_cold_close():
    xs = AsyncSingleStream()

    sink = AnonymousAsyncObserver()

    async def aclose():
        await xs.aclose()

    asyncio.ensure_future(aclose())
    await asyncio.sleep(10)
    with await subscribe(xs, sink):
        await xs.asend_later(1, 20)

    assert sink.values == [(10, )]
コード例 #14
0
async def test_stream_throws():
    ex = MyException("ex")
    xs = AsyncSingleStream()

    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 = AsyncSingleStream()
    sub = None

    def mapper(value):
        return value * 10

    ys = map(mapper, xs)

    sink = AnonymousAsyncObserver()
    sub = await subscribe(ys, sink)
    await xs.asend_later(1, 10)
    sub.cancel()
    await xs.asend_later(1, 20)

    assert sink.values == [(1, 100)]
コード例 #16
0
async def test_unit_observer_throws():
    error = Exception("error")
    xs = unit(42)
    result = []

    def asend(value):
        result.append(value)
        raise error

    sub = await subscribe(xs, AnonymousAsyncObserver(asend))

    try:
        await sub
    except Exception as ex:
        assert ex == error
    assert result == [42]
コード例 #17
0
async def test_delay_cancel_before_done():
    xs = AsyncStream()
    result = []

    async def asend(value):
        print("Send: %d" % value)
        nonlocal result
        result.append(value)

    ys = delay(0.3, xs)
    async with subscribe(ys, AnonymousAsyncObserver(asend)):
        await xs.asend(10)
        await asyncio.sleep(1.5)
        await xs.asend(20)

    await asyncio.sleep(1)
    assert result == [10]
コード例 #18
0
async def test_stream_cancel_mapper():
    xs = AsyncStream()
    stream = None

    def mapper(value):
        stream.cancel()
        return value * 10

    ys = map(mapper, xs)

    sink = AnonymousAsyncObserver()
    async with subscribe(ys, sink) as stream:

        await xs.asend_later(1, 10)
        await xs.asend_later(1, 20)

    assert sink.values == []
コード例 #19
0
async def test_delay_throw():
    xs = AsyncStream()
    result = []

    async def asend(value):
        print("Send: %d" % value)
        nonlocal result
        result.append(value)

    ys = delay(0.3, xs)
    await subscribe(ys, AnonymousAsyncObserver(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]
コード例 #20
0
async def test_unit_close():
    xs = unit(42)
    result = []
    sub = None

    async def asend(value):
        result.append(value)
        sub.cancel()
        await asyncio.sleep(0)

    sub = await subscribe(xs, AnonymousAsyncObserver(asend))

    try:
        await sub
    except asyncio.CancelledError:
        pass

    assert result == [42]
コード例 #21
0
async def test_stream_cancel_asend():
    xs = AsyncSingleStream()
    sub = None

    async def asend(value):
        sub.cancel()
        await asyncio.sleep(0)

    def mapper(value):
        return value * 10

    ys = map(mapper, xs)

    sink = AnonymousAsyncObserver(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)]
コード例 #22
0
async def test_merge_streams_concat():
    s1 = AsyncStream()
    s2 = from_iterable([1, 2, 3])

    xs = from_iterable([s1, s2])

    ys = merge(xs, 1)

    sink = AnonymousAsyncObserver()
    sub = await subscribe(ys, sink)

    await s1.asend_at(1, 10)
    await s1.asend_at(2, 20)
    await s1.asend_at(4, 30)
    await s1.aclose_at(6)

    await sub

    assert sink.values == [(1, 10), (2, 20), (4, 30), (6, 1), (6, 2), (6, 3),
                           (6, )]