File tree Expand file tree Collapse file tree 4 files changed +60
-0
lines changed Expand file tree Collapse file tree 4 files changed +60
-0
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 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+
313324ExperimentWithControlsRequiringSecrets = deepcopy (ExperimentWithControls )
314325ExperimentWithControlsRequiringSecrets ["secrets" ] = {
315326 "mystuff" : {
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments