Skip to content

Commit e1c7d89

Browse files
committed
Fix endless loop on repeat control
Signed-off-by: Sylvain Hellegouarch <sh@defuze.org>
1 parent bf62b1c commit e1c7d89

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
[Unreleased]: https://github.com/chaostoolkit/chaostoolkit-addons/compare/0.8.6...HEAD
66

7+
### Fixed
8+
9+
* Make sure the `repeat` control doesn't run when it count is less than 2
10+
711
### Changed
812

913
- Switched to ruff to lint the code

chaosaddons/controls/repeat.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,21 @@ def after_activity_control(context: Activity, experiment: Experiment,
4343
You may set `break_if_previous_iteration_failed` to stop iterating if
4444
an activity failed to run. Note it's not about the return value of
4545
the activity but if it actually executed as planned.
46+
47+
Note, if `repeat_count` is less than 2, then this is a noop.
4648
"""
4749
activity = context
4850
activity_name = activity["name"]
51+
activity_type = activity["type"]
52+
4953
repeat_count = repeat_count - 1
54+
if repeat_count <= 0:
55+
logger.debug(
56+
f"Do not repeat {activity_type} '{activity_name}' when "
57+
"`repeat_count` is less than 2"
58+
)
59+
return None
60+
5061
last_status = state.get("status")
5162

5263
if break_if_previous_iteration_failed and last_status != "succeeded":

tests/test_repeat.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,58 @@ def test_repeat_n_times_in_hypothesis():
5252
)
5353

5454
assert len(x.get("steady-state-hypothesis").get("probes")) == 4
55+
56+
57+
def test_repeat_once_in_hypothesis():
58+
a = {
59+
"name": "probe-B",
60+
"type": "probe",
61+
"provider": {
62+
"type": "python",
63+
"module": "builtins",
64+
"func": "sum",
65+
"arguments": {
66+
"iterable": [6, 7]
67+
}
68+
}
69+
}
70+
x = {
71+
"title": "hello",
72+
"description": "n/a",
73+
"method": [
74+
{
75+
"name": "probe-A",
76+
"type": "probe",
77+
"provider": {
78+
"type": "python",
79+
"module": "builtins",
80+
"func": "sum",
81+
"arguments": {
82+
"iterable": [2, 3]
83+
}
84+
}
85+
},
86+
a,
87+
{
88+
"name": "probe-C",
89+
"type": "probe",
90+
"provider": {
91+
"type": "python",
92+
"module": "builtins",
93+
"func": "sum",
94+
"arguments": {
95+
"iterable": [2, 3]
96+
}
97+
}
98+
},
99+
]
100+
}
101+
102+
assert len(x.get("method")) == 3
103+
104+
after_activity_control(
105+
context=a, experiment=x, state={},
106+
repeat_count=1
107+
)
108+
109+
assert len(x.get("method")) == 4

0 commit comments

Comments
 (0)