Skip to content
Merged
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
6 changes: 5 additions & 1 deletion lib/split/experiment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def initialize(name, options = {})
set_alternatives_and_options(options)
end

def self.finished_key(key)
"#{key}:finished"
end

def set_alternatives_and_options(options)
self.alternatives = options[:alternatives]
self.goals = options[:goals]
Expand Down Expand Up @@ -221,7 +225,7 @@ def goals_key
end

def finished_key
"#{key}:finished"
self.class.finished_key(key)
end

def metadata_key
Expand Down
7 changes: 6 additions & 1 deletion lib/split/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ def initialize(context)
end

def cleanup_old_experiments!
user.keys.each do |key|
keys_without_finished(user.keys).each do |key|
experiment = ExperimentCatalog.find key_without_version(key)
if experiment.nil? || experiment.has_winner? || experiment.start_time.nil?
user.delete key
user.delete Experiment.finished_key(key)
end
end
end
Expand Down Expand Up @@ -45,6 +46,10 @@ def keys_without_experiment(keys, experiment_key)
keys.reject { |k| k.match(Regexp.new("^#{experiment_key}(:finished)?$")) }
end

def keys_without_finished(keys)
keys.reject { |k| k.include?(":finished") }
end

def key_without_version(key)
key.split(/\:\d(?!\:)/)[0]
end
Expand Down
18 changes: 16 additions & 2 deletions spec/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@
allow(experiment).to receive(:has_winner?).and_return(false)
@subject.cleanup_old_experiments!
expect(@subject.keys).to be_empty
end
end

context 'with finished key' do
let(:user_keys) { { 'link_color' => 'blue', 'link_color:finished' => true } }

it 'does not remove finished key for experiment without a winner' do
allow(Split::ExperimentCatalog).to receive(:find).with('link_color').and_return(experiment)
allow(Split::ExperimentCatalog).to receive(:find).with('link_color:finished').and_return(nil)
allow(experiment).to receive(:start_time).and_return(Date.today)
allow(experiment).to receive(:has_winner?).and_return(false)
@subject.cleanup_old_experiments!
expect(@subject.keys).to include("link_color")
expect(@subject.keys).to include("link_color:finished")
end
end
end
end
end