Skip to content

Commit 7a0c6b5

Browse files
committed
Demo of decorating controls
Closes #197 Signed-off-by: Sylvain Hellegouarch <sh@defuze.org>
1 parent fc46243 commit 7a0c6b5

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
- Activities can be requested not to be executed on a per activity basis, not
1010
just as all or none. This can be used by a control to dynamically make
1111
a decision on what to run.
12+
- Test that demonstrate how to wrap controls into decorators [#197][197]
13+
14+
[197]: https://github.com/chaostoolkit/chaostoolkit-lib/issues/197
1215

1316
## [1.16.0][] - 2020-12-02
1417

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from functools import wraps
2+
from itertools import count
3+
from typing import Callable
4+
5+
from logzero import logger
6+
7+
from chaoslib.types import Journal
8+
9+
counter = None
10+
11+
12+
def initcounter(f: Callable) -> Callable:
13+
@wraps(f)
14+
def wrapped(*args, **kwargs) -> None:
15+
global counter
16+
counter = count()
17+
f(*args, **kwargs)
18+
return wrapped
19+
20+
21+
def keepcount(f: Callable) -> Callable:
22+
@wraps(f)
23+
def wrapped(*args, **kwargs) -> None:
24+
next(counter)
25+
f(*args, **kwargs)
26+
return wrapped
27+
28+
29+
@keepcount
30+
def after_activity_control(**kwargs):
31+
logger.info("Activity is called")
32+
33+
34+
@initcounter
35+
def configure_control(**kwargs):
36+
logger.info("configure is called")
37+
38+
39+
def after_experiment_control(state: Journal, **kwargs):
40+
state["counted_activities"] = next(counter)

tests/fixtures/experiments.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,17 @@
310310
}
311311
]
312312

313+
ExperimentWithDecoratedControls = deepcopy(ExperimentNoControls)
314+
ExperimentWithDecoratedControls["controls"] = [
315+
{
316+
"name": "dummy",
317+
"provider": {
318+
"type": "python",
319+
"module": "fixtures.controls.dummy_with_decorated_control"
320+
}
321+
}
322+
]
323+
313324
ExperimentWithControlsRequiringSecrets = deepcopy(ExperimentWithControls)
314325
ExperimentWithControlsRequiringSecrets["secrets"] = {
315326
"mystuff": {

tests/test_control.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,3 +580,9 @@ def test_secrets_are_passed_to_all_control_hookpoints():
580580
"after_hypothesis_control", "before_activity_control",
581581
"after_activity_control"):
582582
assert exp["{}_secrets".format(hookpoint)] == secrets, "{} was not provided the secrets".format(hookpoint)
583+
584+
585+
def test_control_can_be_decorated_functions():
586+
exp = deepcopy(experiments.ExperimentWithDecoratedControls)
587+
state = run_experiment(exp)
588+
assert state["counted_activities"] == 4

0 commit comments

Comments
 (0)