Skip to content

Commit 0cfd651

Browse files
authored
fix: Prevent multiple identical instantiations of application identity within calls to TestTrack.app_ab (#132)
* fix: Memoize identity within calls to TestTrack.app_ab * Bump version * Fix spec * Use more concise identity logic and fix specs
1 parent 0b66eee commit 0cfd651

File tree

7 files changed

+23
-5
lines changed

7 files changed

+23
-5
lines changed

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
test_track_rails_client (7.1.1)
4+
test_track_rails_client (7.1.2)
55
activejob (>= 6.0)
66
activemodel (>= 6.0)
77
faraday (>= 0.8)

app/models/test_track/application_identity.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def app_name
1212
end
1313

1414
def identity
15-
Identity.new(app_name)
15+
@identity ||= Identity.new(app_name)
1616
end
1717

1818
class Identity

gemfiles/rails_6_1.gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: ..
33
specs:
4-
test_track_rails_client (7.1.1)
4+
test_track_rails_client (7.1.2)
55
activejob (>= 6.0)
66
activemodel (>= 6.0)
77
faraday (>= 0.8)

gemfiles/rails_7_0.gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: ..
33
specs:
4-
test_track_rails_client (7.1.1)
4+
test_track_rails_client (7.1.2)
55
activejob (>= 6.0)
66
activemodel (>= 6.0)
77
faraday (>= 0.8)
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module TestTrackRailsClient
2-
VERSION = "7.1.1".freeze
2+
VERSION = "7.1.2".freeze
33
end

spec/rails_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,7 @@
5656
config.before(:each) do
5757
clear_enqueued_jobs
5858
clear_performed_jobs
59+
60+
TestTrack::ApplicationIdentity.instance.tap { |i| i.remove_instance_variable(:@identity) if i.instance_variable_defined?(:@identity) }
5961
end
6062
end

spec/test_track_spec.rb

+16
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,22 @@
163163
expect(TestTrack.app_ab(:dummy_feature, context: 'test_context')).to eq false
164164
end
165165
end
166+
167+
context "when called twice" do
168+
let(:identity) { TestTrack::ApplicationIdentity.instance }
169+
170+
before do
171+
stub_test_track_assignments(dummy_feature: 'false')
172+
allow(identity.send(:identity)).to receive(:test_track_ab).and_call_original
173+
end
174+
175+
it "uses the same instance of the ApplicationIdentity for each call" do
176+
TestTrack.app_ab(:dummy_feature, context: 'test_context')
177+
TestTrack.app_ab(:dummy_feature, context: 'test_context')
178+
179+
expect(identity.instance_variable_get(:@identity)).to have_received(:test_track_ab).twice
180+
end
181+
end
166182
end
167183

168184
context "when app_name is not specified" do

0 commit comments

Comments
 (0)