diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2017-07-04 22:31:11 +0800 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2017-07-04 22:31:11 +0800 |
commit | 090f034b480b8e8b6dee87765878d1746cc75bce (patch) | |
tree | 2450fbc2643dd3f9fc0878c68e30e5e3d6ed6da8 /spec | |
parent | a4dd3ea168d19d2b65b7e55ed0043c7e7dcac77c (diff) | |
download | gitlab-ce-090f034b480b8e8b6dee87765878d1746cc75bce.tar.gz |
Add test for RequestStoreWrap
Diffstat (limited to 'spec')
-rw-r--r-- | spec/lib/gitlab/cache/request_store_wrap_spec.rb | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/spec/lib/gitlab/cache/request_store_wrap_spec.rb b/spec/lib/gitlab/cache/request_store_wrap_spec.rb new file mode 100644 index 00000000000..82b47c3c7ae --- /dev/null +++ b/spec/lib/gitlab/cache/request_store_wrap_spec.rb @@ -0,0 +1,99 @@ +require 'spec_helper' + +describe Gitlab::Cache::RequestStoreWrap, :request_store do + class ExpensiveAlgorithm < Struct.new(:id, :name, :result) + extend Gitlab::Cache::RequestStoreWrap + + request_store_wrap_key do + [id, name] + end + + request_store_wrap def compute(arg) + result << arg + end + + request_store_wrap def repute(arg) + result << arg + end + end + + let(:algorithm) { ExpensiveAlgorithm.new('id', 'name', []) } + + context 'when RequestStore is active' do + it 'does not compute twice for the same argument' do + result = algorithm.compute(true) + + expect(result).to eq([true]) + expect(algorithm.compute(true)).to eq(result) + expect(algorithm.result).to eq(result) + end + + it 'computes twice for the different argument' do + algorithm.compute(true) + result = algorithm.compute(false) + + expect(result).to eq([true, false]) + expect(algorithm.result).to eq(result) + end + + it 'computes twice for the different keys, id' do + algorithm.compute(true) + algorithm.id = 'ad' + result = algorithm.compute(true) + + expect(result).to eq([true, true]) + expect(algorithm.result).to eq(result) + end + + it 'computes twice for the different keys, name' do + algorithm.compute(true) + algorithm.name = 'same' + result = algorithm.compute(true) + + expect(result).to eq([true, true]) + expect(algorithm.result).to eq(result) + end + + it 'computes twice for the different class name' do + algorithm.compute(true) + allow(ExpensiveAlgorithm).to receive(:name).and_return('CheapAlgo') + result = algorithm.compute(true) + + expect(result).to eq([true, true]) + expect(algorithm.result).to eq(result) + end + + it 'computes twice for the different method' do + algorithm.compute(true) + result = algorithm.repute(true) + + expect(result).to eq([true, true]) + expect(algorithm.result).to eq(result) + end + + it 'computes twice if RequestStore starts over' do + algorithm.compute(true) + RequestStore.end! + RequestStore.clear! + RequestStore.begin! + result = algorithm.compute(true) + + expect(result).to eq([true, true]) + expect(algorithm.result).to eq(result) + end + end + + context 'when RequestStore is inactive' do + before do + RequestStore.end! + end + + it 'computes twice even if everything is the same' do + algorithm.compute(true) + result = algorithm.compute(true) + + expect(result).to eq([true, true]) + expect(algorithm.result).to eq(result) + end + end +end |