Skip to content

Commit 980f027

Browse files
authored
Handle fractional-second error rate timeouts (#187)
1 parent 26b1650 commit 980f027

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

funcy/flow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class ErrorRateExceeded(Exception):
136136
def limit_error_rate(fails, timeout, exception=ErrorRateExceeded):
137137
"""If function fails to complete fails times in a row,
138138
calls to it will be intercepted for timeout with exception raised instead."""
139-
if isinstance(timeout, int):
139+
if isinstance(timeout, (int, float)):
140140
timeout = timedelta(seconds=timeout)
141141

142142
def decorator(func):

tests/test_flow.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,19 @@ def run():
308308
assert not worker.is_alive(), 'reentrant call deadlocked'
309309
assert not errors
310310
assert calls == expected
311+
312+
313+
@pytest.mark.parametrize('timeout', [0.5, timedelta(seconds=0.5)])
314+
def test_limit_error_rate_fractional_timeout(timeout):
315+
@limit_error_rate(1, timeout)
316+
def fail():
317+
raise MyError
318+
319+
with pytest.raises(MyError):
320+
fail()
321+
with pytest.raises(ErrorRateExceeded):
322+
fail()
323+
324+
fail.blocked -= timedelta(seconds=1)
325+
with pytest.raises(MyError):
326+
fail()

0 commit comments

Comments
 (0)