예제 #1
0
async def test_delay_done():
    xs: AsyncTestSubject[int] = AsyncTestSubject()

    ys = pipe(xs, rx.delay(1.0))
    obv = AsyncTestObserver()
    async with await ys.subscribe_async(obv):
        await xs.asend_later(0, 10)
        await xs.asend_later(1.0, 20)
        await xs.aclose_later(1.0)
        await obv

    assert obv.values == [
        (ca(1), OnNext(10)),
        (ca(2), OnNext(20)),
        (ca(3), OnCompleted),
    ]
예제 #2
0
async def test_debounce_filter():
    xs: AsyncTestSubject[int] = AsyncTestSubject()

    ys = pipe(xs, rx.debounce(0.5))
    obv: AsyncTestObserver[int] = AsyncTestObserver()
    await ys.subscribe_async(obv)

    await xs.asend(1)
    await asyncio.sleep(0.3)
    await xs.asend(2)
    await asyncio.sleep(0.6)
    await xs.aclose()
    await asyncio.sleep(0.6)
    await obv

    assert obv.values == [
        (ca(0.8), OnNext(2)),
        (ca(0.9), OnCompleted),
    ]
예제 #3
0
async def test_delay_throw():
    error = Exception("ex")
    xs: AsyncTestSubject[int] = AsyncTestSubject()

    ys = pipe(xs, rx.delay(0.3))

    obv = AsyncTestObserver()
    await ys.subscribe_async(obv)
    await xs.asend(10)
    await asyncio.sleep(1)
    await xs.asend(20)
    await xs.athrow(error)
    await asyncio.sleep(1)

    assert obv.values == [
        (ca(0.3), OnNext(10)),
        (ca(1.3), OnNext(20)),
        (ca(1.6), OnError(error)),
    ]
예제 #4
0
async def test_debounce():
    xs: AsyncTestSubject[int] = AsyncTestSubject()

    ys = pipe(xs, rx.debounce(0.5))

    obv: AsyncTestObserver[int] = AsyncTestObserver()
    await ys.subscribe_async(obv)

    await xs.asend(1)  # 0 -> 0.5
    await asyncio.sleep(0.6)  # 0.6
    await xs.asend(2)  # 0.6 -> 1.1
    await asyncio.sleep(0.6)  # 1.2
    await xs.aclose()  # 1.2
    await asyncio.sleep(0.6)
    await obv

    assert obv.values == [
        (ca(0.5), OnNext(1)),
        (ca(1.1), OnNext(2)),
        (ca(1.2), OnCompleted),
    ]
예제 #5
0
async def test_chain_simple_pipe():
    xs = AsyncRx.from_iterable([1, 2, 3])

    def mapper(value: int) -> int:
        return value * 10

    async def predicate(value: int) -> bool:
        await asyncio.sleep(0.1)
        return value > 1

    ys = xs.filter_async(predicate).map(mapper)

    obv: AsyncTestObserver[int] = AsyncTestObserver()
    await ys.subscribe_async(obv)
    await obv

    assert obv.values == [
        (ca(0.2), OnNext(20)),
        (ca(0.3), OnNext(30)),
        (ca(0.3), OnCompleted),
    ]
예제 #6
0
async def test_delay_cancel_before_done():
    xs: AsyncTestSubject[int] = AsyncTestSubject()

    ys = pipe(xs, rx.delay(0.3))
    obv = AsyncTestObserver()
    async with await ys.subscribe_async(obv):
        await xs.asend(10)
        await asyncio.sleep(1.5)
        await xs.asend(20)

    await asyncio.sleep(1)
    assert obv.values == [
        (ca(0.3), OnNext(10)),
    ]