-
Notifications
You must be signed in to change notification settings - Fork 13
/
test_error.py
47 lines (41 loc) · 1.57 KB
/
test_error.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import unittest
from pyiron_base.utils.error import retry
class TestRetry(unittest.TestCase):
def test_return_value(self):
"""retry should return the exact value that the function returns."""
def func():
return 42
self.assertEqual(func(), retry(func, error=ValueError, msg=""),
"retry returned a different value!")
def test_unrelated_exception(self):
"""retry should not catch exception that are not explicitely passed."""
def func():
raise ValueError()
with self.assertRaises(
ValueError,
msg="retry caught an exception it was not supposed to!"
):
retry(func, error=TypeError, msg="")
def test_exception(self):
"""retry should catch explicitely passed exceptions."""
class Func():
"""Small helper to simulate a stateful function."""
def __init__(self):
self.n = 0
def __call__(self):
self.n += 1
if self.n < 4:
raise ValueError(self.n)
else:
return self.n
func = Func()
try:
retry(func, error=ValueError, msg="", delay=1e-6)
except ValueError:
self.fail("retry did not catch exception!")
func = Func()
with self.assertRaises(
ValueError,
msg="retry did re-raise exception after insufficient tries!"
):
retry(func, error=ValueError, msg="", at_most=2, delay=1e-6)