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

Extract logic for rendering alternatives on Split::Helper #623

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion lib/split/experiment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def load_metadata_from_redis

def load_alternatives_from_configuration
alts = Split.configuration.experiment_for(@name)[:alternatives]
raise ArgumentError, "Experiment configuration is missing :alternatives array" unless alts
raise(Split::InvalidExperimentsFormatError, "Experiment configuration is missing :alternatives array") unless alts
if alts.is_a?(Hash)
alts.keys
else
Expand Down
53 changes: 33 additions & 20 deletions lib/split/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,36 @@ module Helper

module_function

def ab_test(metric_descriptor, control = nil, *alternatives)
begin
experiment = ExperimentCatalog.find_or_initialize(metric_descriptor, control, *alternatives)
alternative = if Split.configuration.enabled && !exclude_visitor?
def ab_test(metric_descriptor, control = nil, *alternatives, &block)
experiment = ExperimentCatalog.find_or_initialize(metric_descriptor, control, *alternatives)

if !Split.configuration.enabled || exclude_visitor? || split_generically_disabled?
render_alternative(experiment, &block)
else
begin
experiment.save
raise(Split::InvalidExperimentsFormatError) unless (Split.configuration.experiments || {}).fetch(experiment.name.to_sym, {})[:combined_experiments].nil?
trial = Trial.new(:user => ab_user, :experiment => experiment,
:override => override_alternative(experiment.name), :exclude => exclude_visitor?,
:disabled => split_generically_disabled?)
alt = trial.choose!(self)
alt ? alt.name : nil
else
control_variable(experiment.control)
end
rescue Errno::ECONNREFUSED, Redis::BaseError, SocketError => e
raise(e) unless Split.configuration.db_failover
Split.configuration.db_failover_on_db_error.call(e)

if Split.configuration.db_failover_allow_parameter_override
alternative = override_alternative(experiment.name) if override_present?(experiment.name)
alternative = control_variable(experiment.control) if split_generically_disabled?
trial = Trial.new(user: ab_user,
experiment: experiment,
override: override_alternative(experiment.name),
exclude: exclude_visitor?,
disabled: split_generically_disabled?)

alternative = trial.choose!(self)
render_alternative experiment, alternative.name, &block
rescue Errno::ECONNREFUSED, Redis::BaseError, SocketError => e
raise(e) unless Split.configuration.db_failover
Split.configuration.db_failover_on_db_error.call(e)

alternative = alternative_from_db_error(experiment)
render_alternative(experiment, alternative, &block)
end
ensure
alternative ||= control_variable(experiment.control)
end
end

def render_alternative(experiment, alternative = nil)
alternative ||= control_variable(experiment.control)

if block_given?
metadata = experiment.metadata[alternative] if experiment.metadata
Expand All @@ -39,6 +44,14 @@ def ab_test(metric_descriptor, control = nil, *alternatives)
end
end

def alternative_from_db_error(experiment)
if Split.configuration.db_failover_allow_parameter_override
alternative = override_alternative(experiment.name) if override_present?(experiment.name)
alternative = control_variable(experiment.control) if split_generically_disabled?
end
alternative
end

def reset!(experiment)
ab_user.delete(experiment.key)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ def should_finish_experiment(experiment_name, should_finish=true)

it "fails gracefully if config is missing alternatives" do
Split.configuration.experiments[:my_experiment] = { :foo => "Bar" }
expect(lambda { ab_test :my_experiment }).to raise_error(NoMethodError)
expect(lambda { ab_test :my_experiment }).to raise_error(Split::InvalidExperimentsFormatError)
end
end

Expand Down