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