Skip to content

Commit b559e84

Browse files
committed
Merge pull request #8 from Springest/thread-safety
Store AbPanel env and condition variables in the current thread
2 parents 580e2f9 + a84b221 commit b559e84

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
language: ruby
22
script: 'bundle exec rspec spec'
33
rvm:
4-
- 1.9.3
5-
- 2.0.0
4+
- 2.1.5

ab_panel.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
2323
spec.add_development_dependency "rake"
2424
spec.add_development_dependency "fakeweb"
2525
spec.add_development_dependency "rspec"
26-
spec.add_development_dependency "debugger"
26+
spec.add_development_dependency "byebug"
2727

2828
spec.add_runtime_dependency "mixpanel"
2929
end

lib/ab_panel.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def identify(distinct_id)
1919
end
2020

2121
def conditions
22-
@conditions ||= assign_conditions!
22+
Thread.current[:ab_panel_conditions] ||= assign_conditions!
2323
end
2424

2525
# Set the experiment's conditions.
@@ -28,7 +28,7 @@ def conditions
2828
# the session.
2929
def conditions=(custom_conditions)
3030
return conditions unless custom_conditions
31-
@conditions = assign_conditions! custom_conditions
31+
Thread.current[:ab_panel_conditions] = assign_conditions! custom_conditions
3232
end
3333

3434
def experiments
@@ -44,18 +44,18 @@ def weights(experiment)
4444
end
4545

4646
def properties
47-
@env[:properties]
47+
env[:properties]
4848
end
4949

5050
def env
51-
@env ||= {
51+
Thread.current[:ab_panel_env] ||= {
5252
'conditions' => conditions
5353
}
5454
end
5555

5656
def reset!
57-
@env = nil
58-
@conditions = nil
57+
Thread.current[:ab_panel_env] = nil
58+
Thread.current[:ab_panel_conditions] = nil
5959
end
6060

6161
def set_env(key, value)
@@ -67,7 +67,7 @@ def funnels
6767
end
6868

6969
def funnels=(funnels)
70-
env['funnels'] = funnels
70+
Thread.current['ab_panel_env']['funnels'] = funnels
7171
end
7272

7373
def add_funnel(funnel)

spec/ab_panel_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,12 @@
8888
AbPanel.funnels.to_a.should == funnels.to_a
8989
end
9090
end
91+
92+
describe 'thread-safety' do
93+
it 'should set be safe' do
94+
AbPanel.set_env(:test, 'a')
95+
Thread.new { AbPanel.set_env(:test, 'b') }.join
96+
expect(AbPanel.env[:test]).to eq 'a'
97+
end
98+
end
9199
end

0 commit comments

Comments
 (0)