diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-07-29 06:08:28 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-07-29 06:08:28 +0300 |
commit | a412f4f58679c9d9120da6dc69ff02a6fcec0c5a (patch) | |
tree | b7291e74be60c903021263a05cefa74df7c616a4 /spec/lib | |
parent | aa02d34e841f2e01820aae1e27233fe655bdf90c (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib')
-rw-r--r-- | spec/lib/gitlab/avatar_cache_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/gitlab/cache/json_cache_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/gitlab/metrics/subscribers/active_record_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/gitlab/middleware/webhook_recursion_detection_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/gitlab/null_request_store_spec.rb | 75 | ||||
-rw-r--r-- | spec/lib/gitlab/safe_request_store_spec.rb | 257 | ||||
-rw-r--r-- | spec/lib/gitlab/with_request_store_spec.rb | 30 |
7 files changed, 4 insertions, 366 deletions
diff --git a/spec/lib/gitlab/avatar_cache_spec.rb b/spec/lib/gitlab/avatar_cache_spec.rb index c959c5d80b2..65cde195a61 100644 --- a/spec/lib/gitlab/avatar_cache_spec.rb +++ b/spec/lib/gitlab/avatar_cache_spec.rb @@ -47,7 +47,7 @@ RSpec.describe Gitlab::AvatarCache, :clean_gitlab_redis_cache do it "finds the cached value in the request store and doesn't execute the block" do expect(thing).to receive(:avatar_path).once - Gitlab::WithRequestStore.with_request_store do + Gitlab::SafeRequestStore.ensure_request_store do described_class.by_email("foo@bar.com", 20, 2, true) do thing.avatar_path end diff --git a/spec/lib/gitlab/cache/json_cache_spec.rb b/spec/lib/gitlab/cache/json_cache_spec.rb index 05126319ef9..1904e42f937 100644 --- a/spec/lib/gitlab/cache/json_cache_spec.rb +++ b/spec/lib/gitlab/cache/json_cache_spec.rb @@ -15,7 +15,7 @@ RSpec.describe Gitlab::Cache::JsonCache, feature_category: :shared do describe '#active?' do context 'when backend respond to active? method' do it 'delegates to the underlying cache implementation' do - backend = instance_double(Gitlab::NullRequestStore, active?: false) + backend = instance_double(Gitlab::SafeRequestStore::NullStore, active?: false) cache = described_class.new(namespace: namespace, backend: backend) diff --git a/spec/lib/gitlab/metrics/subscribers/active_record_spec.rb b/spec/lib/gitlab/metrics/subscribers/active_record_spec.rb index afb029a96cb..2ec31a5cc3e 100644 --- a/spec/lib/gitlab/metrics/subscribers/active_record_spec.rb +++ b/spec/lib/gitlab/metrics/subscribers/active_record_spec.rb @@ -384,7 +384,7 @@ RSpec.describe Gitlab::Metrics::Subscribers::ActiveRecord do end it 'does not store DB roles into into RequestStore' do - Gitlab::WithRequestStore.with_request_store do + Gitlab::SafeRequestStore.ensure_request_store do subscriber.sql(event) expect(described_class.db_counter_payload).to include( diff --git a/spec/lib/gitlab/middleware/webhook_recursion_detection_spec.rb b/spec/lib/gitlab/middleware/webhook_recursion_detection_spec.rb index c8dbc990f8c..5394cea64af 100644 --- a/spec/lib/gitlab/middleware/webhook_recursion_detection_spec.rb +++ b/spec/lib/gitlab/middleware/webhook_recursion_detection_spec.rb @@ -11,7 +11,7 @@ RSpec.describe Gitlab::Middleware::WebhookRecursionDetection do let(:env) { Rack::MockRequest.env_for("/").merge(headers) } around do |example| - Gitlab::WithRequestStore.with_request_store { example.run } + Gitlab::SafeRequestStore.ensure_request_store { example.run } end describe '#call' do diff --git a/spec/lib/gitlab/null_request_store_spec.rb b/spec/lib/gitlab/null_request_store_spec.rb deleted file mode 100644 index f68f478c73e..00000000000 --- a/spec/lib/gitlab/null_request_store_spec.rb +++ /dev/null @@ -1,75 +0,0 @@ -# frozen_string_literal: true - -require 'fast_spec_helper' - -RSpec.describe Gitlab::NullRequestStore do - let(:null_store) { described_class.new } - - describe '#store' do - it 'returns an empty hash' do - expect(null_store.store).to eq({}) - end - end - - describe '#active?' do - it 'returns falsey' do - expect(null_store.active?).to be_falsey - end - end - - describe '#read' do - it 'returns nil' do - expect(null_store.read('foo')).to be nil - end - end - - describe '#[]' do - it 'returns nil' do - expect(null_store['foo']).to be nil - end - end - - describe '#write' do - it 'returns the same value' do - expect(null_store.write('key', 'value')).to eq('value') - end - end - - describe '#[]=' do - it 'returns the same value' do - expect(null_store['key'] = 'value').to eq('value') - end - end - - describe '#exist?' do - it 'returns falsey' do - expect(null_store.exist?('foo')).to be_falsey - end - end - - describe '#fetch' do - it 'returns the block result' do - expect(null_store.fetch('key') { 'block result' }).to eq('block result') # rubocop:disable Style/RedundantFetchBlock - end - end - - describe '#delete' do - context 'when a block is given' do - it 'yields the key to the block' do - expect do |b| - null_store.delete('foo', &b) - end.to yield_with_args('foo') - end - - it 'returns the block result' do - expect(null_store.delete('foo') { |key| 'block result' }).to eq('block result') - end - end - - context 'when a block is not given' do - it 'returns nil' do - expect(null_store.delete('foo')).to be nil - end - end - end -end diff --git a/spec/lib/gitlab/safe_request_store_spec.rb b/spec/lib/gitlab/safe_request_store_spec.rb deleted file mode 100644 index accc491fbb7..00000000000 --- a/spec/lib/gitlab/safe_request_store_spec.rb +++ /dev/null @@ -1,257 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Gitlab::SafeRequestStore do - describe '.store' do - context 'when RequestStore is active', :request_store do - it 'uses RequestStore' do - expect(described_class.store).to eq(RequestStore) - end - end - - context 'when RequestStore is NOT active' do - it 'does not use RequestStore' do - expect(described_class.store).to be_a(Gitlab::NullRequestStore) - end - end - end - - describe '.begin!' do - context 'when RequestStore is active', :request_store do - it 'uses RequestStore' do - expect(RequestStore).to receive(:begin!) - - described_class.begin! - end - end - - context 'when RequestStore is NOT active' do - it 'uses RequestStore' do - expect(RequestStore).to receive(:begin!) - - described_class.begin! - end - end - end - - describe '.clear!' do - context 'when RequestStore is active', :request_store do - it 'uses RequestStore' do - expect(RequestStore).to receive(:clear!).once.and_call_original - - described_class.clear! - end - end - - context 'when RequestStore is NOT active' do - it 'uses RequestStore' do - expect(RequestStore).to receive(:clear!).and_call_original - - described_class.clear! - end - end - end - - describe '.end!' do - context 'when RequestStore is active', :request_store do - it 'uses RequestStore' do - expect(RequestStore).to receive(:end!).once.and_call_original - - described_class.end! - end - end - - context 'when RequestStore is NOT active' do - it 'uses RequestStore' do - expect(RequestStore).to receive(:end!).and_call_original - - described_class.end! - end - end - end - - describe '.write' do - context 'when RequestStore is active', :request_store do - it 'uses RequestStore' do - expect do - described_class.write('foo', true) - end.to change { described_class.read('foo') }.from(nil).to(true) - end - - it 'does not pass the options hash to the underlying store implementation' do - expect(described_class.store).to receive(:write).with('foo', true) - - described_class.write('foo', true, expires_in: 15.seconds) - end - end - - context 'when RequestStore is NOT active' do - it 'does not use RequestStore' do - expect do - described_class.write('foo', true) - end.not_to change { described_class.read('foo') }.from(nil) - end - - it 'does not pass the options hash to the underlying store implementation' do - expect(described_class.store).to receive(:write).with('foo', true) - - described_class.write('foo', true, expires_in: 15.seconds) - end - end - end - - describe '.[]=' do - context 'when RequestStore is active', :request_store do - it 'uses RequestStore' do - expect do - described_class['foo'] = true - end.to change { described_class.read('foo') }.from(nil).to(true) - end - end - - context 'when RequestStore is NOT active' do - it 'does not use RequestStore' do - expect do - described_class['foo'] = true - end.not_to change { described_class.read('foo') }.from(nil) - end - end - end - - describe '.read' do - context 'when RequestStore is active', :request_store do - it 'uses RequestStore' do - expect do - RequestStore.write('foo', true) - end.to change { described_class.read('foo') }.from(nil).to(true) - end - end - - context 'when RequestStore is NOT active' do - it 'does not use RequestStore' do - expect do - RequestStore.write('foo', true) - end.not_to change { described_class.read('foo') }.from(nil) - - RequestStore.clear! # Clean up - end - end - end - - describe '.[]' do - context 'when RequestStore is active', :request_store do - it 'uses RequestStore' do - expect do - RequestStore.write('foo', true) - end.to change { described_class['foo'] }.from(nil).to(true) - end - end - - context 'when RequestStore is NOT active' do - it 'does not use RequestStore' do - expect do - RequestStore.write('foo', true) - end.not_to change { described_class['foo'] }.from(nil) - - RequestStore.clear! # Clean up - end - end - end - - describe '.exist?' do - context 'when RequestStore is active', :request_store do - it 'uses RequestStore' do - expect do - RequestStore.write('foo', 'not nil') - end.to change { described_class.exist?('foo') }.from(false).to(true) - end - end - - context 'when RequestStore is NOT active' do - it 'does not use RequestStore' do - expect do - RequestStore.write('foo', 'not nil') - end.not_to change { described_class.exist?('foo') }.from(false) - - RequestStore.clear! # Clean up - end - end - end - - describe '.fetch' do - context 'when RequestStore is active', :request_store do - it 'uses RequestStore' do - expect do - described_class.fetch('foo') { 'block result' } # rubocop:disable Style/RedundantFetchBlock - end.to change { described_class.read('foo') }.from(nil).to('block result') - end - end - - context 'when RequestStore is NOT active' do - it 'does not use RequestStore' do - RequestStore.clear! # Ensure clean - - expect do - described_class.fetch('foo') { 'block result' } # rubocop:disable Style/RedundantFetchBlock - end.not_to change { described_class.read('foo') }.from(nil) - - RequestStore.clear! # Clean up - end - end - end - - describe '.delete' do - context 'when RequestStore is active', :request_store do - it 'uses RequestStore' do - described_class.write('foo', true) - - expect do - described_class.delete('foo') - end.to change { described_class.read('foo') }.from(true).to(nil) - end - - context 'when given a block and the key exists' do - it 'does not execute the block' do - described_class.write('foo', true) - - expect do |b| - described_class.delete('foo', &b) - end.not_to yield_control - end - end - - context 'when given a block and the key does not exist' do - it 'yields the key and returns the block result' do - result = described_class.delete('foo') { |key| "#{key} block result" } - - expect(result).to eq('foo block result') - end - end - end - - context 'when RequestStore is NOT active' do - before do - RequestStore.write('foo', true) - end - - after do - RequestStore.clear! # Clean up - end - - it 'does not use RequestStore' do - expect do - described_class.delete('foo') - end.not_to change { RequestStore.read('foo') }.from(true) - end - - context 'when given a block' do - it 'yields the key and returns the block result' do - result = described_class.delete('foo') { |key| "#{key} block result" } - - expect(result).to eq('foo block result') - end - end - end - end -end diff --git a/spec/lib/gitlab/with_request_store_spec.rb b/spec/lib/gitlab/with_request_store_spec.rb deleted file mode 100644 index 353ad02fbd8..00000000000 --- a/spec/lib/gitlab/with_request_store_spec.rb +++ /dev/null @@ -1,30 +0,0 @@ -# frozen_string_literal: true - -require 'fast_spec_helper' -require 'request_store' - -RSpec.describe Gitlab::WithRequestStore do - let(:fake_class) { Class.new { include Gitlab::WithRequestStore } } - - subject(:object) { fake_class.new } - - describe "#with_request_store" do - it 'starts a request store and yields control' do - expect(RequestStore).to receive(:begin!).ordered - expect(RequestStore).to receive(:end!).ordered - expect(RequestStore).to receive(:clear!).ordered - - expect { |b| object.with_request_store(&b) }.to yield_control - end - - it 'only starts a request store once when nested' do - expect(RequestStore).to receive(:begin!).ordered.once.and_call_original - expect(RequestStore).to receive(:end!).ordered.once.and_call_original - expect(RequestStore).to receive(:clear!).ordered.once.and_call_original - - object.with_request_store do - expect { |b| object.with_request_store(&b) }.to yield_control - end - end - end -end |