Skip to content

Commit

Permalink
fix: Prevent multiple identical instantiations of application identit…
Browse files Browse the repository at this point in the history
…y 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
  • Loading branch information
willlockwood authored Jul 24, 2024
1 parent 0b66eee commit 0cfd651
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
test_track_rails_client (7.1.1)
test_track_rails_client (7.1.2)
activejob (>= 6.0)
activemodel (>= 6.0)
faraday (>= 0.8)
Expand Down
2 changes: 1 addition & 1 deletion app/models/test_track/application_identity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def app_name
end

def identity
Identity.new(app_name)
@identity ||= Identity.new(app_name)
end

class Identity
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_6_1.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
test_track_rails_client (7.1.1)
test_track_rails_client (7.1.2)
activejob (>= 6.0)
activemodel (>= 6.0)
faraday (>= 0.8)
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_7_0.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
test_track_rails_client (7.1.1)
test_track_rails_client (7.1.2)
activejob (>= 6.0)
activemodel (>= 6.0)
faraday (>= 0.8)
Expand Down
2 changes: 1 addition & 1 deletion lib/test_track_rails_client/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module TestTrackRailsClient
VERSION = "7.1.1".freeze
VERSION = "7.1.2".freeze
end
2 changes: 2 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,7 @@
config.before(:each) do
clear_enqueued_jobs
clear_performed_jobs

TestTrack::ApplicationIdentity.instance.tap { |i| i.remove_instance_variable(:@identity) if i.instance_variable_defined?(:@identity) }
end
end
16 changes: 16 additions & 0 deletions spec/test_track_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,22 @@
expect(TestTrack.app_ab(:dummy_feature, context: 'test_context')).to eq false
end
end

context "when called twice" do
let(:identity) { TestTrack::ApplicationIdentity.instance }

before do
stub_test_track_assignments(dummy_feature: 'false')
allow(identity.send(:identity)).to receive(:test_track_ab).and_call_original
end

it "uses the same instance of the ApplicationIdentity for each call" do
TestTrack.app_ab(:dummy_feature, context: 'test_context')
TestTrack.app_ab(:dummy_feature, context: 'test_context')

expect(identity.instance_variable_get(:@identity)).to have_received(:test_track_ab).twice
end
end
end

context "when app_name is not specified" do
Expand Down

0 comments on commit 0cfd651

Please sign in to comment.