Skip to content

Commit 4417370

Browse files
committed
Play controls once only after experiment
Signed-off-by: Sylvain Hellegouarch <[email protected]>
1 parent 3f80c1e commit 4417370

File tree

5 files changed

+62
-13
lines changed

5 files changed

+62
-13
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-lib/compare/1.27.2...HEAD
66

7+
### Changed
8+
9+
- Ensure experiment level controls are only played once
10+
711
## [1.27.2][] - 2022-03-23
812

913
[1.27.2]: https://github.com/chaostoolkit/chaostoolkit-lib/compare/1.27.1...1.27.2

chaoslib/control/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ def get_context_controls(
369369
for tc in reversed(top_level_controls):
370370
if c.get("name") == tc.get("name"):
371371
continue
372-
if tc.get("automatic", True):
372+
if (level != "experiment") and tc.get("automatic", True):
373373
controls.insert(0, deepcopy(tc))
374374

375375
return controls
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from typing import Sequence
2+
3+
from chaoslib.types import Experiment, Journal
4+
5+
6+
def before_experiment_control(context: Experiment,
7+
values: Sequence[int]) -> None:
8+
context["result_after"] = 0
9+
10+
11+
def after_experiment_control(context: Experiment, state: Journal,
12+
values: Sequence[int]) -> None:
13+
context["result_after"] += sum(values)

tests/fixtures/experiments.py

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -864,28 +864,25 @@
864864
"name": "tc1",
865865
"provider": {
866866
"type": "python",
867-
"module": "builtins",
868-
"func": "sum",
869-
"arguments": {"iterable": [4, 5]},
867+
"module": "fixtures.controls.dummy_sums",
868+
"arguments": {"values": [4, 5]},
870869
},
871870
},
872871
{
873872
"name": "tc2",
874873
"automatic": False,
875874
"provider": {
876875
"type": "python",
877-
"module": "builtins",
878-
"func": "sum",
879-
"arguments": {"iterable": [6, 7]},
876+
"module": "fixtures.controls.dummy_sums",
877+
"arguments": {"values": [6, 7]},
880878
},
881879
},
882880
{
883881
"name": "tc3",
884882
"provider": {
885883
"type": "python",
886-
"module": "builtins",
887-
"func": "sum",
888-
"arguments": {"iterable": [6, 7]},
884+
"module": "fixtures.controls.dummy_sums",
885+
"arguments": {"values": [6, 7]},
889886
},
890887
},
891888
],
@@ -905,12 +902,41 @@
905902
"name": "lc1",
906903
"provider": {
907904
"type": "python",
908-
"module": "builtins",
909-
"func": "sum",
910-
"arguments": {"iterable": [2, 3]},
905+
"module": "fixtures.controls.dummy_sums",
906+
"arguments": {"values": [2, 3]},
911907
},
912908
}
913909
],
914910
},
915911
],
916912
}
913+
914+
915+
ExperimentWithOnlyTopLevelControls = deepcopy(ExperimentNoControls)
916+
ExperimentWithOnlyTopLevelControls["controls"] = [
917+
{
918+
"name": "tc1",
919+
"provider": {
920+
"type": "python",
921+
"module": "fixtures.controls.dummy_sums",
922+
"arguments": {"values": [1, 2]},
923+
},
924+
},
925+
{
926+
"name": "tc2",
927+
"automatic": False,
928+
"provider": {
929+
"type": "python",
930+
"module": "fixtures.controls.dummy_sums",
931+
"arguments": {"values": [3, 4]},
932+
},
933+
},
934+
{
935+
"name": "tc3",
936+
"provider": {
937+
"type": "python",
938+
"module": "fixtures.controls.dummy_sums",
939+
"arguments": {"values": [5, 6]},
940+
},
941+
},
942+
]

tests/test_control.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,3 +593,9 @@ def test_activity_level_controls_are_merged_to_top_level_controls():
593593
assert controls[0]["name"] == "tc1"
594594
assert controls[1]["name"] == "tc3"
595595
assert controls[2]["name"] == "lc1"
596+
597+
598+
def test_experiment_level_controls_played_only_one_each_in_the_after_phase():
599+
x = deepcopy(experiments.ExperimentWithOnlyTopLevelControls)
600+
run_experiment(x)
601+
assert x["result_after"] == 21

0 commit comments

Comments
 (0)