File: test_combine.py

package info (click to toggle)
python-aiostream 0.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 416 kB
  • sloc: python: 2,816; makefile: 5
file content (275 lines) | stat: -rw-r--r-- 8,846 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
from functools import partial
from typing import Awaitable
import pytest
import asyncio

from aiostream import stream, pipe, async_, await_
from aiostream.test_utils import add_resource


@pytest.mark.asyncio
async def test_chain(assert_run, assert_cleanup):
    with assert_cleanup():
        xs = stream.range(5) + stream.range(5, 10)
        await assert_run(xs, list(range(10)))

    with assert_cleanup():
        xs = stream.range(10, 15) | add_resource.pipe(1)
        xs += stream.range(15, 20) | add_resource.pipe(1)
        await assert_run(xs, list(range(10, 20)))

    # Empty chain (issue #95)
    xs = stream.chain()
    await assert_run(xs, [])


class ThrowsInEquality:

    def __eq__(self, other):
        raise Exception("We should not reach this!")


@pytest.fixture(
    params=[stream.range, partial(stream.repeat, ThrowsInEquality())],
    ids=["range", "iterator_of_items_that_throw"],
)
def make_range(request):
    return request.param


@pytest.mark.asyncio
async def test_zip_strict(make_range):
    xs = make_range(3)
    ys = make_range(2)
    zs = stream.zip(xs, ys, strict=True)
    with pytest.raises(ValueError, match="The provided sources have different lengths"):
        await zs


@pytest.mark.asyncio
async def test_zip(assert_run):
    xs = stream.range(5) | add_resource.pipe(1.0)
    ys = xs | pipe.zip(xs, xs)
    expected = [(x,) * 3 for x in range(5)]
    await assert_run(ys, expected)

    # Exceptions from iterables are propagated
    xs = stream.zip(stream.range(2), stream.throw(AttributeError))
    with pytest.raises(AttributeError):
        await xs

    # Empty zip (issue #95)
    xs = stream.zip()
    await assert_run(xs, [])

    # Strict mode (issue #118): Iterable length mismatch raises
    xs = stream.zip(stream.range(2), stream.range(1), strict=True)
    with pytest.raises(ValueError):
        await xs

    # Strict mode (issue #118): No raise for matching-length iterables
    xs = stream.zip(stream.range(2), stream.range(2), strict=True)
    await assert_run(xs, [(0, 0), (1, 1)])

    # Strict mode (issue #118): Exceptions from iterables are propagated
    xs = stream.zip(stream.range(2), stream.throw(AttributeError), strict=True)
    with pytest.raises(AttributeError):
        await xs

    # Strict mode (issue #118): Non-strict mode works as before
    xs = stream.zip(stream.range(2), stream.range(1))
    await assert_run(xs, [(0, 0)])

    # Strict mode (issue #118): In particular, we stop immediately if any
    # one iterable is exhausted, not waiting for the others
    slow_iterable_continued_after_sleep = asyncio.Event()

    async def fast_iterable():
        yield 0
        await asyncio.sleep(1)

    async def slow_iterable():
        yield 0
        await asyncio.sleep(2)
        slow_iterable_continued_after_sleep.set()

    xs = stream.zip(fast_iterable(), slow_iterable())
    await assert_run(xs, [(0, 0)])
    assert not slow_iterable_continued_after_sleep.is_set()


@pytest.mark.asyncio
async def test_map(assert_run, assert_cleanup):
    def square_target(arg: int, *_) -> int:
        return arg**2

    def sum_target(a: int, b: int, *_) -> int:
        return a + b

    async def sleep_only(arg: float, *_) -> None:
        return await asyncio.sleep(arg)

    async def sleep_and_result(arg: float, result: int, *_) -> int:
        return await asyncio.sleep(arg, result)

    def not_a_coro_function(arg: int, *_) -> Awaitable[int]:
        return asyncio.sleep(arg, arg)

    # Synchronous/simple
    with assert_cleanup():
        xs = stream.range(5) | pipe.map(square_target)
        expected = [x**2 for x in range(5)]
        await assert_run(xs, expected)

    # Synchronous/multiple
    with assert_cleanup():
        xs = stream.range(5)
        ys = xs | pipe.map(sum_target, xs)
        expected = [x * 2 for x in range(5)]
        await assert_run(ys, expected)

    # Asynchronous/simple/concurrent
    with assert_cleanup() as loop:
        xs = stream.range(1, 4) | pipe.map(sleep_only)
        expected = [None] * 3
        await assert_run(xs, expected)
        assert loop.steps == [1, 1, 1]

    # Asynchronous/simple/sequential
    with assert_cleanup() as loop:
        xs = stream.range(1, 4) | pipe.map(sleep_only, task_limit=1)
        expected = [None] * 3
        await assert_run(xs, expected)
        assert loop.steps == [1, 2, 3]

    # Asynchronous/multiple/concurrent
    with assert_cleanup() as loop:
        xs = stream.range(1, 4)
        ys = xs | pipe.map(sleep_and_result, xs)
        await assert_run(ys, [1, 2, 3])
        assert loop.steps == [1, 1, 1]

    # Asynchronous/multiple/sequential
    with assert_cleanup() as loop:
        xs = stream.range(1, 4)
        ys = xs | pipe.map(sleep_and_result, xs, task_limit=1)
        await assert_run(ys, [1, 2, 3])
        assert loop.steps == [1, 2, 3]

    # As completed
    with assert_cleanup() as loop:
        xs = stream.iterate([2, 4, 1, 3, 5])
        ys = xs | pipe.map(sleep_and_result, xs, ordered=False)
        await assert_run(ys, [1, 2, 3, 4, 5])
        assert loop.steps == [1, 1, 1, 1, 1]

    # Invalid arguments
    with pytest.raises(ValueError):
        await (stream.range(1, 4) | pipe.map(sleep_only, task_limit=0))
    with pytest.raises(ValueError):
        await (stream.range(1, 4) | pipe.map(square_target, task_limit=3))
    with pytest.raises(ValueError):
        await (stream.range(1, 4) | pipe.map(square_target, ordered=False))

    # Break
    with assert_cleanup() as loop:
        xs = stream.count(1)
        ys = xs | pipe.map(sleep_and_result, xs, task_limit=10)
        await assert_run(ys[:3], [1, 2, 3])
        assert loop.steps == [1, 1, 1]

    # Stuck
    with assert_cleanup():
        xs = stream.count(1)
        ys = xs | pipe.map(sleep_and_result, xs, task_limit=1) | pipe.timeout(5)
        await assert_run(ys, [1, 2, 3, 4], asyncio.TimeoutError())

    # Force await
    with assert_cleanup():
        xs = stream.iterate([1, 2, 3])
        ys = xs | pipe.map(async_(not_a_coro_function))
        await assert_run(ys, [1, 2, 3])
        assert loop.steps == [1, 1, 1]

    # Map await_
    with assert_cleanup() as loop:
        xs = stream.iterate(map(lambda x: asyncio.sleep(x, x), [1, 2, 3]))
        ys = xs | pipe.map(await_)  # type: ignore
        await assert_run(ys, [1, 2, 3])
        assert loop.steps == [1, 1, 1]


@pytest.mark.asyncio
async def test_merge(assert_run, assert_cleanup):
    with assert_cleanup() as loop:
        xs = stream.range(1, 5, 2, interval=2) | pipe.delay(1)
        ys = stream.range(0, 5, 2, interval=2) | pipe.merge(xs)
        await assert_run(ys, [0, 1, 2, 3, 4])
        assert loop.steps == [1, 1, 1, 1]

    with assert_cleanup():
        xs = stream.range(1, 5, 2, interval=2) | pipe.delay(1)
        ys = stream.range(0, 5, 2, interval=2) | pipe.merge(xs)
        await assert_run(ys[:3], [0, 1, 2])
        assert loop.steps == [1, 1]

    with assert_cleanup():
        xs = stream.just(1) + stream.never()
        ys = xs | pipe.merge(xs) | pipe.timeout(1)
        await assert_run(ys, [1, 1], asyncio.TimeoutError())
        assert loop.steps == [1]

    # Reproduce issue #65
    with assert_cleanup():
        xs = stream.iterate([1, 2])
        ys = stream.iterate([3, 4])
        zs = stream.merge(xs, ys) | pipe.take(3)
        await assert_run(zs, [1, 2, 3])

    with assert_cleanup():
        xs = stream.iterate([1, 2, 3])
        ys = stream.throw(ZeroDivisionError())
        zs = stream.merge(xs, ys) | pipe.delay(1) | pipe.take(3)
        await assert_run(zs, [1, 2, 3])

    # Silencing of a CancelledError

    async def agen1():
        if False:
            yield
        try:
            await asyncio.sleep(2)
        except asyncio.CancelledError:
            return

    async def agen2():
        yield 1

    with assert_cleanup():
        xs = stream.merge(agen1(), agen2()) | pipe.delay(1) | pipe.take(1)
        await assert_run(xs, [1])

    # Empty merge (issue #95)
    xs = stream.merge()
    await assert_run(xs, [])


@pytest.mark.asyncio
async def test_ziplatest(assert_run, assert_cleanup):
    with assert_cleanup() as loop:
        xs = stream.range(0, 5, 2, interval=2)
        ys = stream.range(1, 5, 2, interval=2) | pipe.delay(1)
        zs = stream.ziplatest(xs, ys, default="▲")
        await assert_run(zs, [(0, "▲"), (0, 1), (2, 1), (2, 3), (4, 3)])
        assert loop.steps == [1, 1, 1, 1]

    with assert_cleanup() as loop:
        xs = stream.range(0, 5, 2, interval=2)
        ys = stream.range(1, 5, 2, interval=2) | pipe.delay(1)
        zs = stream.ziplatest(xs, ys, partial=False)
        await assert_run(zs, [(0, 1), (2, 1), (2, 3), (4, 3)])
        assert loop.steps == [1, 1, 1, 1]

    # Empty ziplatest (issue #95)
    xs = stream.ziplatest()
    await assert_run(xs, [])