Enhance WrapperHandler

- docstrings
- add try_call_all method
- update tests
This commit is contained in:
2025-09-30 11:46:52 +02:00
parent 912a9b9c8d
commit 40fb400a9c
2 changed files with 83 additions and 2 deletions

View File

@@ -58,3 +58,22 @@ class TestWrapperHandler:
assert result == "Success"
assert handler.index == 1 # Should return to the second wrapper after failure
assert handler.retry_count == 0
def test_try_call_all(self):
wrappers = [FailingWrapper, MockWrapper, FailingWrapper]
handler: WrapperHandler[MockWrapper] = WrapperHandler.build_wrappers(wrappers, try_per_wrapper=1, retry_delay=0)
results = handler.try_call_all(lambda w: w.do_something())
assert results == ["Success"] # Only the second wrapper should succeed
wrappers = [FailingWrapper, MockWrapper, FailingWrapper, MockWrapper]
handler: WrapperHandler[MockWrapper] = WrapperHandler.build_wrappers(wrappers, try_per_wrapper=1, retry_delay=0)
results = handler.try_call_all(lambda w: w.do_something())
assert results == ["Success", "Success"] # Only the second and fourth wrappers should succeed
# Test when all wrappers fail
handler_all_fail: WrapperHandler[MockWrapper] = WrapperHandler.build_wrappers([FailingWrapper, FailingWrapper], try_per_wrapper=1, retry_delay=0)
with pytest.raises(Exception) as exc_info:
handler_all_fail.try_call_all(lambda w: w.do_something())
assert "All wrappers failed" in str(exc_info.value)