|
| 1 | +from time import time |
| 2 | + |
1 | 3 | import pytest
|
2 | 4 | import trio
|
3 | 5 |
|
4 | 6 | from slurry import Pipeline
|
5 |
| -from slurry.sections import Repeat, Metronome, InsertValue |
| 7 | +from slurry.sections import Repeat, Metronome, InsertValue, _producers |
6 | 8 |
|
7 | 9 | from .fixtures import produce_alphabet
|
8 | 10 |
|
@@ -51,32 +53,31 @@ async def test_repeat_input(autojump_clock):
|
51 | 53 | break
|
52 | 54 | assert results == [('a', 1), ('a', 2), ('b', 2.5), ('b', 3.5), ('c', 4)]
|
53 | 55 |
|
54 |
| -async def test_metronome(): |
| 56 | +async def test_metronome(autojump_clock, monkeypatch): |
| 57 | + monkeypatch.setattr(_producers, "time", trio.current_time) |
55 | 58 | async with Pipeline.create(
|
56 |
| - produce_alphabet(5, max=3), |
| 59 | + produce_alphabet(5, max=6, delay=1), |
57 | 60 | Metronome(5)
|
58 | 61 | ) as pipeline, pipeline.tap() as aiter:
|
59 | 62 | results = []
|
60 | 63 | start_time = trio.current_time()
|
61 |
| - async for item in aiter: |
62 |
| - results.append((item, trio.current_time() - start_time)) |
63 |
| - if len(results) == 2: |
| 64 | + while True: |
| 65 | + try: |
| 66 | + results.append((await aiter.__anext__(), trio.current_time() - start_time)) |
| 67 | + except StopAsyncIteration: |
64 | 68 | break
|
65 |
| - assert [x[0] for x in results] == ['a', 'b'] |
66 |
| - assert 5 - results[1][1] + results[0][1] < 0.1 |
| 69 | + assert results == [(letter, 5.0 * (i + 1)) for i, letter in enumerate("abcde")] |
67 | 70 |
|
68 |
| -async def test_metronome_no_input(): |
| 71 | +async def test_metronome_no_input(autojump_clock, monkeypatch): |
| 72 | + monkeypatch.setattr(_producers, "time", trio.current_time) |
69 | 73 | async with Pipeline.create(
|
70 | 74 | Metronome(5, "a")
|
71 | 75 | ) as pipeline, pipeline.tap() as aiter:
|
72 | 76 | results = []
|
73 | 77 | start_time = trio.current_time()
|
74 |
| - async for item in aiter: |
75 |
| - results.append((item, trio.current_time() - start_time)) |
76 |
| - if len(results) == 2: |
77 |
| - break |
78 |
| - assert [x[0] for x in results] == ['a', 'a'] |
79 |
| - assert 5 - results[1][1] + results[0][1] < 0.1 |
| 78 | + for _ in range(5): |
| 79 | + results.append((await aiter.__anext__(), trio.current_time() - start_time)) |
| 80 | + assert results == [("a", 5.0 * (i + 1)) for i in range(5)] |
80 | 81 |
|
81 | 82 | async def test_insert_value(autojump_clock):
|
82 | 83 | async with Pipeline.create(
|
|
0 commit comments