|
| 1 | +import asyncio |
| 2 | +import typing |
| 3 | +import unittest |
| 4 | + |
| 5 | +from functools import wraps |
| 6 | + |
| 7 | +from tenacity import RetryCallState, retry |
| 8 | + |
| 9 | + |
| 10 | +def asynctest( |
| 11 | + callable_: typing.Callable[..., typing.Any], |
| 12 | +) -> typing.Callable[..., typing.Any]: |
| 13 | + @wraps(callable_) |
| 14 | + def wrapper(*a: typing.Any, **kw: typing.Any) -> typing.Any: |
| 15 | + loop = asyncio.get_event_loop() |
| 16 | + return loop.run_until_complete(callable_(*a, **kw)) |
| 17 | + |
| 18 | + return wrapper |
| 19 | + |
| 20 | + |
| 21 | +MAX_RETRY_FIX_ATTEMPTS = 2 |
| 22 | + |
| 23 | + |
| 24 | +class TestIssue478(unittest.TestCase): |
| 25 | + def test_issue(self) -> None: |
| 26 | + results = [] |
| 27 | + |
| 28 | + def do_retry(retry_state: RetryCallState) -> bool: |
| 29 | + outcome = retry_state.outcome |
| 30 | + assert outcome |
| 31 | + ex = outcome.exception() |
| 32 | + _subject_: str = retry_state.args[0] |
| 33 | + |
| 34 | + if _subject_ == "Fix": # no retry on fix failure |
| 35 | + return False |
| 36 | + |
| 37 | + if retry_state.attempt_number >= MAX_RETRY_FIX_ATTEMPTS: |
| 38 | + return False |
| 39 | + |
| 40 | + if ex: |
| 41 | + do_fix_work() |
| 42 | + return True |
| 43 | + |
| 44 | + return False |
| 45 | + |
| 46 | + @retry(reraise=True, retry=do_retry) |
| 47 | + def _do_work(subject: str) -> None: |
| 48 | + if subject == "Error": |
| 49 | + results.append(f"{subject} is not working") |
| 50 | + raise Exception(f"{subject} is not working") |
| 51 | + results.append(f"{subject} is working") |
| 52 | + |
| 53 | + def do_any_work(subject: str) -> None: |
| 54 | + _do_work(subject) |
| 55 | + |
| 56 | + def do_fix_work() -> None: |
| 57 | + _do_work("Fix") |
| 58 | + |
| 59 | + try: |
| 60 | + do_any_work("Error") |
| 61 | + except Exception as exc: |
| 62 | + assert str(exc) == "Error is not working" |
| 63 | + else: |
| 64 | + assert False, "No exception caught" |
| 65 | + |
| 66 | + assert results == [ |
| 67 | + "Error is not working", |
| 68 | + "Fix is working", |
| 69 | + "Error is not working", |
| 70 | + ] |
| 71 | + |
| 72 | + @asynctest |
| 73 | + async def test_async(self) -> None: |
| 74 | + results = [] |
| 75 | + |
| 76 | + async def do_retry(retry_state: RetryCallState) -> bool: |
| 77 | + outcome = retry_state.outcome |
| 78 | + assert outcome |
| 79 | + ex = outcome.exception() |
| 80 | + _subject_: str = retry_state.args[0] |
| 81 | + |
| 82 | + if _subject_ == "Fix": # no retry on fix failure |
| 83 | + return False |
| 84 | + |
| 85 | + if retry_state.attempt_number >= MAX_RETRY_FIX_ATTEMPTS: |
| 86 | + return False |
| 87 | + |
| 88 | + if ex: |
| 89 | + await do_fix_work() |
| 90 | + return True |
| 91 | + |
| 92 | + return False |
| 93 | + |
| 94 | + @retry(reraise=True, retry=do_retry) |
| 95 | + async def _do_work(subject: str) -> None: |
| 96 | + if subject == "Error": |
| 97 | + results.append(f"{subject} is not working") |
| 98 | + raise Exception(f"{subject} is not working") |
| 99 | + results.append(f"{subject} is working") |
| 100 | + |
| 101 | + async def do_any_work(subject: str) -> None: |
| 102 | + await _do_work(subject) |
| 103 | + |
| 104 | + async def do_fix_work() -> None: |
| 105 | + await _do_work("Fix") |
| 106 | + |
| 107 | + try: |
| 108 | + await do_any_work("Error") |
| 109 | + except Exception as exc: |
| 110 | + assert str(exc) == "Error is not working" |
| 111 | + else: |
| 112 | + assert False, "No exception caught" |
| 113 | + |
| 114 | + assert results == [ |
| 115 | + "Error is not working", |
| 116 | + "Fix is working", |
| 117 | + "Error is not working", |
| 118 | + ] |
0 commit comments