From cf87e73cd5c1f52b5d14da9149c72cf3c4c126da Mon Sep 17 00:00:00 2001 From: boyska Date: Sun, 7 Jan 2018 12:48:56 +0100 Subject: [PATCH] FIX frequency with long intervals --- larigira/tests/test_time_every.py | 17 +++++++++++++++++ larigira/timegen_every.py | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/larigira/tests/test_time_every.py b/larigira/tests/test_time_every.py index ae621c2..dfbdf70 100644 --- a/larigira/tests/test_time_every.py +++ b/larigira/tests/test_time_every.py @@ -61,6 +61,14 @@ def tenseconds(now, request): 'end': now + days(1)}) +@pytest.fixture(params=[1, 2, 3, 4, 5, 6, 7, 8]) +def manyweeks(request): + yield FrequencyAlarm({ + 'interval': '{}w'.format(request.param), + 'start': 0 + }) + + def days(n): return timedelta(days=n) @@ -155,6 +163,15 @@ def test_sunday_is_not_0(): assert 'Not a valid weekday:' in excinfo.value.args[0] +def test_long_interval(manyweeks): + t = datetime.fromtimestamp(1) + expected = manyweeks.interval + got = manyweeks.next_ring(t) + assert got is not None + assert int(got.strftime('%s')) == expected + assert manyweeks.next_ring(got) is not None + + def test_single_registered(): timegenerate({ 'kind': 'single', diff --git a/larigira/timegen_every.py b/larigira/timegen_every.py index 126180c..bd4f5dd 100644 --- a/larigira/timegen_every.py +++ b/larigira/timegen_every.py @@ -100,7 +100,7 @@ class FrequencyAlarm(Alarm): # fact, it is necessary to retry until a valid event/weekday is # found. a "while True" might have been more elegant (and maybe # fast), but this gives a clear upper bound to the cycle. - for _ in range(60*60*24*7 // self.interval): + for _ in range(max(60*60*24*7 // self.interval, 1)): n_interval = ( (current_time - self.start).total_seconds() // self.interval ) + 1