Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve marshal implementation #169

Merged
merged 1 commit into from
Dec 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions lib/scientist/experiment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ def raise_on_mismatches?
#
# Returns the configured block.
def before_run(&block)
marshalize(block)
@_scientist_before_run = block
end

Expand All @@ -100,7 +99,6 @@ def behaviors
#
# Returns the configured block.
def clean(&block)
marshalize(block)
@_scientist_cleaner = block
end

Expand Down Expand Up @@ -133,7 +131,6 @@ def clean_value(value)
#
# Returns the block.
def compare(*args, &block)
marshalize(block)
@_scientist_comparator = block
end

Expand All @@ -144,7 +141,6 @@ def compare(*args, &block)
#
# Returns the block.
def compare_errors(*args, &block)
marshalize(block)
@_scientist_error_comparator = block
end

Expand All @@ -163,7 +159,6 @@ def context(context = nil)
#
# This can be called more than once with different blocks to use.
def ignore(&block)
marshalize(block)
@_scientist_ignores ||= []
@_scientist_ignores << block
end
Expand Down Expand Up @@ -256,7 +251,6 @@ def run(name = nil)

# Define a block that determines whether or not the experiment should run.
def run_if(&block)
marshalize(block)
@_scientist_run_if_block = block
end

Expand All @@ -282,7 +276,6 @@ def should_experiment_run?

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

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

# Register the control behavior for this experiment.
def use(&block)
marshalize(block)
try "control", &block
end

Expand Down Expand Up @@ -332,13 +324,14 @@ def generate_result(name)
# In order to support marshaling, we have to make the procs marshalable. Some
# CI providers attempt to marshal Scientist mismatch errors so that they can
# be sent out to different places (logs, etc.) The mismatch errors contain
# code from the experiment. This code contains Procs - which can't be
# marshaled until we run the following code.
def marshalize(block)
unless block.respond_to?(:_dump) || block.respond_to?(:_dump_data)
def block._dump(_)
to_s
end
end
# code from the experiment. This code contains procs. These procs prevent the
# error from being marshaled. To fix this, we simple exclude the procs from
# the data that we marshal.
def marshal_dump
[@name, @result, @raise_on_mismatches]
end

def marshal_load
@name, @result, @raise_on_mismatches = array
end
end