From c496c0044bdc3186b9fe7ad848d484f53c69d78d Mon Sep 17 00:00:00 2001 From: Mike Nerone Date: Wed, 3 Jan 2024 16:26:45 -0600 Subject: [PATCH 1/5] Bugfix: InsertValue input optional; shouldn't error on None --- slurry/sections/_producers.py | 5 +++-- tests/test_producers.py | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/slurry/sections/_producers.py b/slurry/sections/_producers.py index 852c3b3..9636840 100644 --- a/slurry/sections/_producers.py +++ b/slurry/sections/_producers.py @@ -127,5 +127,6 @@ def __init__(self, value: Any) -> None: async def refine(self, input, output): await output(self.value) - async for item in input: - await output(item) + if input: + async for item in input: + await output(item) diff --git a/tests/test_producers.py b/tests/test_producers.py index 0c90aa1..ae2987e 100644 --- a/tests/test_producers.py +++ b/tests/test_producers.py @@ -73,3 +73,10 @@ async def test_insert_value(autojump_clock): start_time = trio.current_time() results = [(v, trio.current_time() - start_time) async for v in aiter] assert results == [('n', 0), ('a', 1), ('b', 2), ('c', 3)] + +async def test_insert_value_no_input(autojump_clock): + async with Pipeline.create( + InsertValue('n') + ) as pipeline, pipeline.tap() as aiter: + results = [v async for v in aiter] + assert results == ['n'] From d0fead2347a81341f702961c524fbc69fdd8c0ac Mon Sep 17 00:00:00 2001 From: Mike Nerone Date: Sat, 20 Jan 2024 00:12:33 -0600 Subject: [PATCH 2/5] Add a no-input test for Metronome --- tests/test_producers.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/test_producers.py b/tests/test_producers.py index ae2987e..20ec18d 100644 --- a/tests/test_producers.py +++ b/tests/test_producers.py @@ -65,6 +65,19 @@ async def test_metronome(): assert [x[0] for x in results] == ['a', 'b'] assert 5 - results[1][1] + results[0][1] < 0.1 +async def test_metronome_no_input(): + 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 + async def test_insert_value(autojump_clock): async with Pipeline.create( produce_alphabet(1, max=3, delay=1), From 14176baf48951cf10f357782572a0f5fd016704c Mon Sep 17 00:00:00 2001 From: Mike Nerone Date: Sat, 20 Jan 2024 00:01:39 -0600 Subject: [PATCH 3/5] Make Metronome tests fast --- tests/test_producers.py | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/tests/test_producers.py b/tests/test_producers.py index 20ec18d..0d124f3 100644 --- a/tests/test_producers.py +++ b/tests/test_producers.py @@ -2,7 +2,7 @@ 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 +51,27 @@ 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: - break - assert [x[0] for x in results] == ['a', 'b'] - assert 5 - results[1][1] + results[0][1] < 0.1 + results.append((item, trio.current_time())) + 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): + item = await aiter.__anext__() + results.append((item, trio.current_time())) + assert results == [("a", 5.0 * (i + 1)) for i in range(5)] async def test_insert_value(autojump_clock): async with Pipeline.create( From d252c1f0b1957587b2b1ea79cfa1153d102f13b6 Mon Sep 17 00:00:00 2001 From: Mike Nerone Date: Fri, 26 Jan 2024 00:32:28 -0600 Subject: [PATCH 4/5] Indentation fix --- tests/test_producers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_producers.py b/tests/test_producers.py index 0d124f3..095691b 100644 --- a/tests/test_producers.py +++ b/tests/test_producers.py @@ -84,7 +84,7 @@ async def test_insert_value(autojump_clock): async def test_insert_value_no_input(autojump_clock): async with Pipeline.create( - InsertValue('n') + InsertValue('n') ) as pipeline, pipeline.tap() as aiter: results = [v async for v in aiter] assert results == ['n'] From 7f767622ecfd1760cbd88e6098f1def42d214cf7 Mon Sep 17 00:00:00 2001 From: Mike Nerone Date: Fri, 26 Jan 2024 00:32:43 -0600 Subject: [PATCH 5/5] Doc addition to explain InsertValue with no input --- slurry/sections/_producers.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/slurry/sections/_producers.py b/slurry/sections/_producers.py index 9636840..1f1272c 100644 --- a/slurry/sections/_producers.py +++ b/slurry/sections/_producers.py @@ -119,6 +119,8 @@ class InsertValue(TrioSection): """Inserts a single user supplied value into the pipeline on startup and then passes through any further received items unmodified. + If no input is used, the single value will be sent, and InsertValue will close. + :param value: Item to send on startup. :type value: Any """