From 674b81e1f69bf1742c9a55bea0cceb86a4fb24ba Mon Sep 17 00:00:00 2001 From: Mike Nerone Date: Sat, 20 Jan 2024 00:01:39 -0600 Subject: [PATCH] Make Metronome tests fast --- tests/test_producers.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/tests/test_producers.py b/tests/test_producers.py index 20ec18d..bd79e84 100644 --- a/tests/test_producers.py +++ b/tests/test_producers.py @@ -1,8 +1,10 @@ +from time import time + import pytest import trio from slurry import Pipeline -from slurry.sections import Repeat, Metronome, InsertValue +from slurry.sections import Repeat, Metronome, InsertValue, _producers from .fixtures import produce_alphabet @@ -51,32 +53,31 @@ async def test_repeat_input(autojump_clock): break assert results == [('a', 1), ('a', 2), ('b', 2.5), ('b', 3.5), ('c', 4)] -async def test_metronome(): +async def test_metronome(autojump_clock, monkeypatch): + monkeypatch.setattr(_producers, "time", trio.current_time) async with Pipeline.create( - produce_alphabet(5, max=3), + produce_alphabet(5, max=6, delay=1), Metronome(5) ) as pipeline, pipeline.tap() as aiter: results = [] start_time = trio.current_time() - async for item in aiter: - results.append((item, trio.current_time() - start_time)) - if len(results) == 2: + while True: + try: + results.append((await aiter.__anext__(), trio.current_time() - start_time)) + except StopAsyncIteration: break - assert [x[0] for x in results] == ['a', 'b'] - assert 5 - results[1][1] + results[0][1] < 0.1 + assert results == [(letter, 5.0 * (i + 1)) for i, letter in enumerate("abcde")] -async def test_metronome_no_input(): +async def test_metronome_no_input(autojump_clock, monkeypatch): + monkeypatch.setattr(_producers, "time", trio.current_time) async with Pipeline.create( Metronome(5, "a") ) as pipeline, pipeline.tap() as aiter: results = [] start_time = trio.current_time() - async for item in aiter: - results.append((item, trio.current_time() - start_time)) - if len(results) == 2: - break - assert [x[0] for x in results] == ['a', 'a'] - assert 5 - results[1][1] + results[0][1] < 0.1 + for _ in range(5): + results.append((await aiter.__anext__(), trio.current_time() - start_time)) + assert results == [("a", 5.0 * (i + 1)) for i in range(5)] async def test_insert_value(autojump_clock): async with Pipeline.create(