Commit f69c0f80 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Remove unneeded asserts and add tests for inactive RequestStore

parent 3922b803
require 'spec_helper' require 'spec_helper'
describe Gitlab::Cache::RequestCache, :request_store do describe Gitlab::Cache::RequestCache do
let(:klass) do let(:klass) do
Class.new do Class.new do
extend Gitlab::Cache::RequestCache extend Gitlab::Cache::RequestCache
...@@ -35,13 +35,12 @@ describe Gitlab::Cache::RequestCache, :request_store do ...@@ -35,13 +35,12 @@ describe Gitlab::Cache::RequestCache, :request_store do
let(:algorithm) { klass.new('id', 'name', []) } let(:algorithm) { klass.new('id', 'name', []) }
context 'when RequestStore is active' do shared_examples 'cache for the same instance' do
it 'does not compute twice for the same argument' do it 'does not compute twice for the same argument' do
algorithm.compute(true)
result = algorithm.compute(true) result = algorithm.compute(true)
expect(result).to eq([true]) expect(result).to eq([true])
expect(algorithm.compute(true)).to eq(result)
expect(algorithm.result).to eq(result)
end end
it 'computes twice for the different argument' do it 'computes twice for the different argument' do
...@@ -49,7 +48,6 @@ describe Gitlab::Cache::RequestCache, :request_store do ...@@ -49,7 +48,6 @@ describe Gitlab::Cache::RequestCache, :request_store do
result = algorithm.compute(false) result = algorithm.compute(false)
expect(result).to eq([true, false]) expect(result).to eq([true, false])
expect(algorithm.result).to eq(result)
end end
it 'computes twice for the different class name' do it 'computes twice for the different class name' do
...@@ -58,7 +56,6 @@ describe Gitlab::Cache::RequestCache, :request_store do ...@@ -58,7 +56,6 @@ describe Gitlab::Cache::RequestCache, :request_store do
result = algorithm.compute(true) result = algorithm.compute(true)
expect(result).to eq([true, true]) expect(result).to eq([true, true])
expect(algorithm.result).to eq(result)
end end
it 'computes twice for the different method' do it 'computes twice for the different method' do
...@@ -66,18 +63,6 @@ describe Gitlab::Cache::RequestCache, :request_store do ...@@ -66,18 +63,6 @@ describe Gitlab::Cache::RequestCache, :request_store do
result = algorithm.repute(true) result = algorithm.repute(true)
expect(result).to eq([true, 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 request_cache_key is provided' do context 'when request_cache_key is provided' do
...@@ -93,7 +78,6 @@ describe Gitlab::Cache::RequestCache, :request_store do ...@@ -93,7 +78,6 @@ describe Gitlab::Cache::RequestCache, :request_store do
result = algorithm.compute(true) result = algorithm.compute(true)
expect(result).to eq([true, true]) expect(result).to eq([true, true])
expect(algorithm.result).to eq(result)
end end
it 'computes twice for the different keys, name' do it 'computes twice for the different keys, name' do
...@@ -102,7 +86,6 @@ describe Gitlab::Cache::RequestCache, :request_store do ...@@ -102,7 +86,6 @@ describe Gitlab::Cache::RequestCache, :request_store do
result = algorithm.compute(true) result = algorithm.compute(true)
expect(result).to eq([true, true]) expect(result).to eq([true, true])
expect(algorithm.result).to eq(result)
end end
it 'uses extra method cache key if provided' do it 'uses extra method cache key if provided' do
...@@ -112,30 +95,39 @@ describe Gitlab::Cache::RequestCache, :request_store do ...@@ -112,30 +95,39 @@ describe Gitlab::Cache::RequestCache, :request_store do
result = algorithm.dispute(true) # hit result = algorithm.dispute(true) # hit
expect(result).to eq([true, true]) expect(result).to eq([true, true])
expect(algorithm.result).to eq(result)
end end
end end
end end
context 'when RequestStore is inactive' do context 'when RequestStore is active', :request_store do
before do it_behaves_like 'cache for the same instance'
RequestStore.end!
it 'computes once for different instances when keys are the same' do
algorithm.compute(true)
result = klass.new('id', 'name', algorithm.result).compute(true)
expect(result).to eq([true])
end end
it 'computes only once if it is the same instance for the same key' do it 'computes twice if RequestStore starts over' do
algorithm.compute(true) algorithm.compute(true)
RequestStore.end!
RequestStore.clear!
RequestStore.begin!
result = algorithm.compute(true) result = algorithm.compute(true)
expect(result).to eq([true]) expect(result).to eq([true, true])
expect(algorithm.result).to eq(result)
end end
end
context 'when RequestStore is inactive' do
it_behaves_like 'cache for the same instance'
it 'computes twice for different instances even if keys are the same' do it 'computes twice for different instances even if keys are the same' do
algorithm.compute(true) algorithm.compute(true)
result = klass.new('id', 'name', algorithm.result).compute(true) result = klass.new('id', 'name', algorithm.result).compute(true)
expect(result).to eq([true, true]) expect(result).to eq([true, true])
expect(algorithm.result).to eq(result)
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment