diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-02-23 12:10:45 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-02-23 12:10:45 +0300 |
commit | cd40f83527ac4ed4751b4b7849f0416996589d18 (patch) | |
tree | 201a36d826fc98f14ee1a7a1dc806b32f51cac00 /spec/experiments | |
parent | d5f3372f10b9fefc8cf831515152eee7ae908f69 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/experiments')
-rw-r--r-- | spec/experiments/application_experiment/cache_spec.rb | 54 | ||||
-rw-r--r-- | spec/experiments/application_experiment_spec.rb | 31 |
2 files changed, 25 insertions, 60 deletions
diff --git a/spec/experiments/application_experiment/cache_spec.rb b/spec/experiments/application_experiment/cache_spec.rb deleted file mode 100644 index 4caa91e6ac4..00000000000 --- a/spec/experiments/application_experiment/cache_spec.rb +++ /dev/null @@ -1,54 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe ApplicationExperiment::Cache do - let(:key_name) { 'experiment_name' } - let(:field_name) { 'abc123' } - let(:key_field) { [key_name, field_name].join(':') } - let(:shared_state) { Gitlab::Redis::SharedState } - - around do |example| - shared_state.with { |r| r.del(key_name) } - example.run - shared_state.with { |r| r.del(key_name) } - end - - it "allows reading, writing and deleting", :aggregate_failures do - # we test them all together because they are largely interdependent - - expect(subject.read(key_field)).to be_nil - expect(shared_state.with { |r| r.hget(key_name, field_name) }).to be_nil - - subject.write(key_field, 'value') - - expect(subject.read(key_field)).to eq('value') - expect(shared_state.with { |r| r.hget(key_name, field_name) }).to eq('value') - - subject.delete(key_field) - - expect(subject.read(key_field)).to be_nil - expect(shared_state.with { |r| r.hget(key_name, field_name) }).to be_nil - end - - it "handles the fetch with a block behavior (which is read/write)" do - expect(subject.fetch(key_field) { 'value1' }).to eq('value1') # rubocop:disable Style/RedundantFetchBlock - expect(subject.fetch(key_field) { 'value2' }).to eq('value1') # rubocop:disable Style/RedundantFetchBlock - end - - it "can clear a whole experiment cache key" do - subject.write(key_field, 'value') - subject.clear(key: key_field) - - expect(shared_state.with { |r| r.get(key_name) }).to be_nil - end - - it "doesn't allow clearing a key from the cache that's not a hash (definitely not an experiment)" do - shared_state.with { |r| r.set(key_name, 'value') } - - expect { subject.clear(key: key_name) }.to raise_error( - ArgumentError, - 'invalid call to clear a non-hash cache key' - ) - end -end diff --git a/spec/experiments/application_experiment_spec.rb b/spec/experiments/application_experiment_spec.rb index 501d344e920..3803fa10ab3 100644 --- a/spec/experiments/application_experiment_spec.rb +++ b/spec/experiments/application_experiment_spec.rb @@ -191,15 +191,15 @@ RSpec.describe ApplicationExperiment, :experiment do end context "when caching" do - let(:cache) { ApplicationExperiment::Cache.new } + let(:cache) { Gitlab::Experiment::Configuration.cache } before do + allow(Gitlab::Experiment::Configuration).to receive(:cache).and_call_original + cache.clear(key: subject.name) subject.use { } # setup the control subject.try { } # setup the candidate - - allow(Gitlab::Experiment::Configuration).to receive(:cache).and_return(cache) end it "caches the variant determined by the variant resolver" do @@ -207,7 +207,7 @@ RSpec.describe ApplicationExperiment, :experiment do subject.run - expect(cache.read(subject.cache_key)).to eq('candidate') + expect(subject.cache.read).to eq('candidate') end it "doesn't cache a variant if we don't explicitly provide one" do @@ -222,7 +222,7 @@ RSpec.describe ApplicationExperiment, :experiment do subject.run - expect(cache.read(subject.cache_key)).to be_nil + expect(subject.cache.read).to be_nil end it "caches a control variant if we assign it specifically" do @@ -232,7 +232,26 @@ RSpec.describe ApplicationExperiment, :experiment do # write code that would specify a different variant. subject.run(:control) - expect(cache.read(subject.cache_key)).to eq('control') + expect(subject.cache.read).to eq('control') + end + + context "arbitrary attributes" do + before do + subject.cache.store.clear(key: subject.name + '_attrs') + end + + it "sets and gets attributes about an experiment" do + subject.cache.attr_set(:foo, :bar) + + expect(subject.cache.attr_get(:foo)).to eq('bar') + end + + it "increments a value for an experiment" do + expect(subject.cache.attr_get(:foo)).to be_nil + + expect(subject.cache.attr_inc(:foo)).to eq(1) + expect(subject.cache.attr_inc(:foo)).to eq(2) + end end end end |