Beispiel #1
0
def test_zip(c, s, a, b):
    a = Stream(asynchronous=True)
    b = Stream(asynchronous=True)
    c = scatter(a).zip(scatter(b))

    L = c.gather().sink_to_list()

    yield a.emit(1)
    yield b.emit("a")
    yield a.emit(2)
    yield b.emit("b")

    assert L == [(1, "a"), (2, "b")]
Beispiel #2
0
def test_map(backend):
    source = Stream(asynchronous=True)
    futures = scatter(source, backend=backend).map(inc)
    futures_L = futures.sink_to_list()
    L = futures.gather().sink_to_list()

    for i in range(5):
        yield source.emit(i)

    assert L == [1, 2, 3, 4, 5]
    assert all(isinstance(f, Future) for f in futures_L)
Beispiel #3
0
def test_scan(c, s, a, b):
    source = Stream(asynchronous=True)
    futures = scatter(source).map(inc).scan(add)
    futures_L = futures.sink_to_list()
    L = futures.gather().sink_to_list()

    for i in range(5):
        yield source.emit(i)

    assert L == [1, 3, 6, 10, 15]
    assert all(isinstance(f, Future) for f in futures_L)
Beispiel #4
0
def test_filter_map(c, s, a, b):
    source = Stream(asynchronous=True)
    futures = scatter(source).filter(lambda x: x % 2 == 0).map(inc)
    futures_L = futures.sink_to_list()
    L = futures.gather().sink_to_list()

    for i in range(5):
        yield source.emit(i)

    assert L == [1, 3, 5]
    assert all(isinstance(f, Future) for f in futures_L)
Beispiel #5
0
def test_filter(backend):
    source = Stream(asynchronous=True)
    futures = scatter(source, backend=backend).filter(lambda x: x % 2 == 0)
    futures_L = futures.sink_to_list()
    L = futures.gather().sink_to_list()

    for i in range(5):
        yield source.emit(i)

    assert L == [0, 2, 4]
    assert all(isinstance(f, Future) for f in futures_L)
Beispiel #6
0
def test_scan_state(c, s, a, b):
    source = Stream(asynchronous=True)

    def f(acc, i):
        acc = acc + i
        return acc, acc

    L = scatter(source).scan(f, returns_state=True).gather().sink_to_list()
    for i in range(3):
        yield source.emit(i)

    assert L == [0, 1, 3]
Beispiel #7
0
def test_filter_zip(c, s, a, b):
    source = Stream(asynchronous=True)
    s = scatter(source)
    futures = s.filter(lambda x: x % 2 == 0).zip(s)
    futures_L = futures.sink_to_list()
    L = futures.gather().sink_to_list()

    for i in range(5):
        yield source.emit(i)

    assert L == [(a, a) for a in [0, 2, 4]]
    assert all(isinstance(f[0], Future) for f in futures_L)
Beispiel #8
0
def test_filter_starmap(backend):
    source = Stream(asynchronous=True)
    futures1 = scatter(source, backend=backend).filter(lambda x: x[1] % 2 == 0)
    futures = futures1.starmap(add)
    futures_L = futures.sink_to_list()
    L = futures.gather().sink_to_list()

    for i in range(5):
        yield source.emit((i, i))

    assert L == [0, 4, 8]
    assert all(isinstance(f, Future) for f in futures_L)
Beispiel #9
0
def test_starmap_args(c, s, a, b):
    def add(x, y, z=0):
        return x + y + z

    source = Stream(asynchronous=True)
    futures = scatter(source).starmap(add, 10)
    futures_L = futures.sink_to_list()
    L = futures.gather().sink_to_list()

    for i in range(5):
        yield source.emit(i)

    assert len(L) == len(futures_L)
    assert L == [i + 10 for i in range(5)]
Beispiel #10
0
def test_starmap(backend):
    def add(x, y, z=0):
        return x + y + z

    source = Stream(asynchronous=True)
    futures = scatter(source, backend=backend).starmap(add, z=10)
    futures_L = futures.sink_to_list()
    L = futures.gather().sink_to_list()

    for i in range(5):
        yield source.emit((i, i))

    assert len(L) == len(futures_L)
    assert L == [10, 12, 14, 16, 18]
Beispiel #11
0
def test_buffer2(backend):
    source = Stream(asynchronous=True)
    futures = scatter(source, backend=backend)
    futures_L = futures.sink_to_list()
    L = futures.buffer(10).gather().sink_to_list()

    for i in range(5):
        yield source.emit(i)
    assert len(futures_L) == 5
    while len(L) < len(futures_L):
        yield gen.sleep(.01)

    assert L == [0, 1, 2, 3, 4]
    assert all(isinstance(f, Future) for f in futures_L)