Skip to content

Commit 8c1ec7f

Browse files
authored
Merge pull request #168 from agentdon/marshalize_mismatch_errors
Marshalize mismatch errors
2 parents 51d4cdf + b874a39 commit 8c1ec7f

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

CONTRIBUTING.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ Contributions to this project are [released](https://help.github.com/articles/gi
1010
## Submitting a pull request
1111

1212
0. [Fork][fork] and clone the repository
13-
0. Create a new branch: `git checkout -b my-branch-name`
14-
0. Make your change, push to your fork and [submit a pull request][pr]
15-
0. Pat your self on the back and wait for your pull request to be reviewed and merged.
13+
1. Create a new branch: `git checkout -b my-branch-name`
14+
2. Make your change, push to your fork and [submit a pull request][pr]
15+
3. Pat your self on the back and wait for your pull request to be reviewed and merged.
1616

1717
Here are a few things you can do that will increase the likelihood of your pull request being accepted:
1818

lib/scientist/experiment.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def raise_on_mismatches?
8484
#
8585
# Returns the configured block.
8686
def before_run(&block)
87+
marshalize(block)
8788
@_scientist_before_run = block
8889
end
8990

@@ -99,6 +100,7 @@ def behaviors
99100
#
100101
# Returns the configured block.
101102
def clean(&block)
103+
marshalize(block)
102104
@_scientist_cleaner = block
103105
end
104106

@@ -131,6 +133,7 @@ def clean_value(value)
131133
#
132134
# Returns the block.
133135
def compare(*args, &block)
136+
marshalize(block)
134137
@_scientist_comparator = block
135138
end
136139

@@ -141,6 +144,7 @@ def compare(*args, &block)
141144
#
142145
# Returns the block.
143146
def compare_errors(*args, &block)
147+
marshalize(block)
144148
@_scientist_error_comparator = block
145149
end
146150

@@ -159,6 +163,7 @@ def context(context = nil)
159163
#
160164
# This can be called more than once with different blocks to use.
161165
def ignore(&block)
166+
marshalize(block)
162167
@_scientist_ignores ||= []
163168
@_scientist_ignores << block
164169
end
@@ -251,6 +256,7 @@ def run(name = nil)
251256

252257
# Define a block that determines whether or not the experiment should run.
253258
def run_if(&block)
259+
marshalize(block)
254260
@_scientist_run_if_block = block
255261
end
256262

@@ -276,6 +282,7 @@ def should_experiment_run?
276282

277283
# Register a named behavior for this experiment, default "candidate".
278284
def try(name = nil, &block)
285+
marshalize(block)
279286
name = (name || "candidate").to_s
280287

281288
if behaviors.include?(name)
@@ -287,6 +294,7 @@ def try(name = nil, &block)
287294

288295
# Register the control behavior for this experiment.
289296
def use(&block)
297+
marshalize(block)
290298
try "control", &block
291299
end
292300

@@ -318,4 +326,19 @@ def generate_result(name)
318326
control = observations.detect { |o| o.name == name }
319327
Scientist::Result.new(self, observations, control)
320328
end
329+
330+
private
331+
332+
# In order to support marshaling, we have to make the procs marshalable. Some
333+
# CI providers attempt to marshal Scientist mismatch errors so that they can
334+
# be sent out to different places (logs, etc.) The mismatch errors contain
335+
# code from the experiment. This code contains Procs - which can't be
336+
# marshaled until we run the following code.
337+
def marshalize(block)
338+
unless block.respond_to?(:_dump) || block.respond_to?(:_dump_data)
339+
def block._dump(_)
340+
to_s
341+
end
342+
end
343+
end
321344
end

test/scientist/experiment_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,27 @@ def @ex.raised(op, exception)
491491
assert_raises(Scientist::Experiment::MismatchError) { runner.call }
492492
end
493493

494+
it "can be marshaled" do
495+
Fake.raise_on_mismatches = true
496+
@ex.before_run { "some block" }
497+
@ex.clean { "some block" }
498+
@ex.compare_errors { "some block" }
499+
@ex.ignore { false }
500+
@ex.run_if { "some block" }
501+
@ex.try { "candidate" }
502+
@ex.use { "control" }
503+
@ex.compare { |control, candidate| control == candidate }
504+
505+
mismatch = nil
506+
begin
507+
@ex.run
508+
rescue Scientist::Experiment::MismatchError => e
509+
mismatch = e
510+
end
511+
512+
assert_kind_of(String, Marshal.dump(mismatch))
513+
end
514+
494515
describe "#raise_on_mismatches?" do
495516
it "raises when there is a mismatch if the experiment instance's raise on mismatches is enabled" do
496517
Fake.raise_on_mismatches = false

0 commit comments

Comments
 (0)