Skip to content

Commit cbf692e

Browse files
add test code for retry
Signed-off-by: hirokuni-kitahara <[email protected]>
1 parent 1925ed3 commit cbf692e

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

tests/test_retry.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import io
2+
from contextlib import redirect_stdout
3+
import traceback
4+
from pdl.pdl import exec_dict
5+
6+
7+
def repeat_retry_data(n: int):
8+
return {
9+
"description": "Example of retry code within a repeat block",
10+
"repeat": {
11+
"text": [
12+
"Hello, ",
13+
{
14+
"lang": "python",
15+
"code": {
16+
"text": ["raise ValueError('dummy exception')\n", "result = 'World'"]
17+
},
18+
},
19+
"!\n",
20+
],
21+
"retry": n
22+
},
23+
"max_iterations": 2
24+
}
25+
26+
27+
def repeat_retry(n: int, should_be_no_error: bool = False):
28+
err_msg = ""
29+
# catch stdout string
30+
with io.StringIO() as buf, redirect_stdout(buf):
31+
try:
32+
_ = exec_dict(repeat_retry_data(n))
33+
except Exception:
34+
pass
35+
err_msg = buf.getvalue()
36+
37+
if should_be_no_error:
38+
assert err_msg == ""
39+
else:
40+
assert f"[Retry 1/{n}]" in err_msg
41+
42+
43+
def test_repeat_retry_negative():
44+
repeat_retry(-1, should_be_no_error=True)
45+
46+
47+
def test_repeat_retry0():
48+
repeat_retry(0, should_be_no_error=True)
49+
50+
51+
def test_repeat_retry1():
52+
repeat_retry(1)
53+
54+
55+
def test_repeat_retry2():
56+
repeat_retry(2)
57+
58+
59+
def test_repeat_retry3():
60+
repeat_retry(3)
61+
62+
63+
def code_retry_data(n: int):
64+
return {
65+
"description": "Example of retry code within a code block",
66+
"text": [
67+
{
68+
"lang": "python",
69+
"code": {
70+
"text": ["raise ValueError('dummy exception')\n", "result = 'hello, world!'"]
71+
},
72+
},
73+
],
74+
"retry": n
75+
}
76+
77+
78+
def code_retry(n: int, should_be_no_error: bool = False):
79+
err_msg = ""
80+
# catch stdout string
81+
with io.StringIO() as buf, redirect_stdout(buf):
82+
try:
83+
_ = exec_dict(code_retry_data(n))
84+
except Exception:
85+
pass
86+
err_msg = buf.getvalue()
87+
if should_be_no_error:
88+
assert err_msg == ""
89+
else:
90+
assert f"[Retry 1/{n}]" in err_msg
91+
92+
93+
def test_code_retry_negative():
94+
code_retry(-1, should_be_no_error=True)
95+
96+
97+
def test_code_retry0():
98+
code_retry(0, should_be_no_error=True)
99+
100+
101+
def test_code_retry1():
102+
code_retry(1)
103+
104+
105+
def test_code_retry2():
106+
code_retry(2)
107+
108+
109+
def test_code_retry3():
110+
code_retry(3)

0 commit comments

Comments
 (0)