File tree 2 files changed +45
-0
lines changed
2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -87,6 +87,16 @@ def before_run(&block)
87
87
@_scientist_before_run = block
88
88
end
89
89
90
+ # Define a block of code to run after an experiment completes, if the experiment
91
+ # is enabled.
92
+ #
93
+ # The block takes one argument, the Scientist::Result containing experiment results.
94
+ #
95
+ # Returns the configured block.
96
+ def after_run ( &block )
97
+ @_scientist_after_run = block
98
+ end
99
+
90
100
# A Hash of behavior blocks, keyed by String name. Register behavior blocks
91
101
# with the `try` and `use` methods.
92
102
def behaviors
@@ -230,6 +240,10 @@ def run(name = nil)
230
240
231
241
result = generate_result ( name )
232
242
243
+ if @_scientist_after_run
244
+ @_scientist_after_run . call ( result )
245
+ end
246
+
233
247
begin
234
248
publish ( result )
235
249
rescue StandardError => ex
Original file line number Diff line number Diff line change @@ -635,6 +635,37 @@ def @ex.enabled?
635
635
end
636
636
end
637
637
638
+ describe "after run block" do
639
+ it "runs when an experiment is enabled" do
640
+ control_ok = candidate_ok = false
641
+ after_result = nil
642
+ @ex . after_run { |result | after_result = result }
643
+ @ex . use { control_ok = after_result . nil? }
644
+ @ex . try { candidate_ok = after_result . nil? }
645
+
646
+ @ex . run
647
+
648
+ assert after_result , "after_run should have run"
649
+ assert after_result . matched? , "after_run should be called with the result"
650
+ assert control_ok , "control should have run before after_run"
651
+ assert candidate_ok , "candidate should have run before after_run"
652
+ end
653
+
654
+ it "does not run when an experiment is disabled" do
655
+ after_result = nil
656
+
657
+ def @ex . enabled?
658
+ false
659
+ end
660
+ @ex . after_run { |result | after_result = result }
661
+ @ex . use { "value" }
662
+ @ex . try { "value" }
663
+ @ex . run
664
+
665
+ refute after_result , "after_run should not have run"
666
+ end
667
+ end
668
+
638
669
describe "testing hooks for extending code" do
639
670
it "allows a user to provide fabricated durations for testing purposes" do
640
671
@ex . use { true }
You can’t perform that action at this time.
0 commit comments