Skip to content

Commit e8df7bf

Browse files
committed
Avoid fully loading the experiment if its not needed
1 parent 396b9c3 commit e8df7bf

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

lib/split/user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def initialize(context, adapter = nil)
1616
def cleanup_old_experiments!
1717
return if @cleaned_up
1818
keys_without_finished(user.keys).each do |key|
19-
experiment = ExperimentCatalog.find key_without_version(key)
19+
experiment = Experiment.new key_without_version(key)
2020
if experiment.nil? || experiment.has_winner? || experiment.start_time.nil?
2121
user.delete key
2222
user.delete Experiment.finished_key(key)

spec/user_spec.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,17 @@
4848
end
4949

5050
it "removes key if experiment has a winner" do
51-
allow(Split::ExperimentCatalog).to receive(:find).with("link_color").and_return(experiment)
52-
allow(experiment).to receive(:start_time).and_return(Date.today)
53-
allow(experiment).to receive(:has_winner?).and_return(true)
51+
experiment = Split::ExperimentCatalog.find_or_create("link_color", "red", "blue")
52+
experiment.start
53+
experiment.winner = "red"
54+
55+
expect(experiment.has_winner?).to be_truthy
5456
@subject.cleanup_old_experiments!
5557
expect(@subject.keys).to be_empty
5658
end
5759

5860
it "removes key if experiment has not started yet" do
59-
allow(Split::ExperimentCatalog).to receive(:find).with("link_color").and_return(experiment)
60-
allow(experiment).to receive(:has_winner?).and_return(false)
61+
expect(Split::ExperimentCatalog.find("link_color")).to be_nil
6162
@subject.cleanup_old_experiments!
6263
expect(@subject.keys).to be_empty
6364
end
@@ -66,11 +67,12 @@
6667
let(:user_keys) { { "link_color" => "blue", "link_color:finished" => true } }
6768

6869
it "does not remove finished key for experiment without a winner" do
69-
allow(Split::ExperimentCatalog).to receive(:find).with("link_color").and_return(experiment)
70-
allow(Split::ExperimentCatalog).to receive(:find).with("link_color:finished").and_return(nil)
71-
allow(experiment).to receive(:start_time).and_return(Date.today)
72-
allow(experiment).to receive(:has_winner?).and_return(false)
70+
experiment = Split::ExperimentCatalog.find_or_create("link_color", "red", "blue")
71+
experiment.start
72+
73+
expect(experiment.has_winner?).to be_falsey
7374
@subject.cleanup_old_experiments!
75+
7476
expect(@subject.keys).to include("link_color")
7577
expect(@subject.keys).to include("link_color:finished")
7678
end

0 commit comments

Comments
 (0)