Skip to content

Commit ebfc6c9

Browse files
committed
Make Metronome tests fast
1 parent d0fead2 commit ebfc6c9

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

tests/test_producers.py

+10-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import trio
33

44
from slurry import Pipeline
5-
from slurry.sections import Repeat, Metronome, InsertValue
5+
from slurry.sections import Repeat, Metronome, InsertValue, _producers
66

77
from .fixtures import produce_alphabet
88

@@ -51,32 +51,29 @@ async def test_repeat_input(autojump_clock):
5151
break
5252
assert results == [('a', 1), ('a', 2), ('b', 2.5), ('b', 3.5), ('c', 4)]
5353

54-
async def test_metronome():
54+
async def test_metronome(autojump_clock, monkeypatch):
55+
monkeypatch.setattr(_producers, "time", trio.current_time)
5556
async with Pipeline.create(
56-
produce_alphabet(5, max=3),
57+
produce_alphabet(5, max=6, delay=1),
5758
Metronome(5)
5859
) as pipeline, pipeline.tap() as aiter:
5960
results = []
6061
start_time = trio.current_time()
6162
async for item in aiter:
6263
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")]
6765

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)
6968
async with Pipeline.create(
7069
Metronome(5, "a")
7170
) as pipeline, pipeline.tap() as aiter:
7271
results = []
7372
start_time = trio.current_time()
74-
async for item in aiter:
73+
for _ in range(5):
74+
item = await aiter.__anext__()
7575
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)]
8077

8178
async def test_insert_value(autojump_clock):
8279
async with Pipeline.create(

0 commit comments

Comments
 (0)