diff options
author | Michael Kozono <mkozono@gmail.com> | 2018-09-25 20:07:59 +0300 |
---|---|---|
committer | Michael Kozono <mkozono@gmail.com> | 2018-09-28 01:16:09 +0300 |
commit | d9c4ebc5a0b2e911f17865e482de1dfcc2189ac3 (patch) | |
tree | 17da1018b4eabbc858f33568e03cf944b946331c /spec/lib/gitlab/repository_cache_adapter_spec.rb | |
parent | 0920342094ae07d7225b42cf23cf9637076d2b25 (diff) |
Extract `Repository.memoize_method` method
And reuse `Gitlab::Utils::StrongMemoize`.
There is a subtle behavior change required to reuse StrongMemoize in
this case. The early fallback check now occurs *before* reading the
memoized value instead of after.
I think this is fine since a memoized value should only exist if
`exists?` is also already memoized as `true`.
Diffstat (limited to 'spec/lib/gitlab/repository_cache_adapter_spec.rb')
-rw-r--r-- | spec/lib/gitlab/repository_cache_adapter_spec.rb | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/spec/lib/gitlab/repository_cache_adapter_spec.rb b/spec/lib/gitlab/repository_cache_adapter_spec.rb index 5bd4d6c6a48..703e73f1a9b 100644 --- a/spec/lib/gitlab/repository_cache_adapter_spec.rb +++ b/spec/lib/gitlab/repository_cache_adapter_spec.rb @@ -65,6 +65,64 @@ describe Gitlab::RepositoryCacheAdapter do end end + describe '#memoize_method_output' do + let(:fallback) { 10 } + + context 'with a non-existing repository' do + let(:project) { create(:project) } # No repository + + subject do + repository.memoize_method_output(:cats, fallback: fallback) do + repository.cats_call_stub + end + end + + it 'returns the fallback value' do + expect(subject).to eq(fallback) + end + + it 'avoids calling the original method' do + expect(repository).not_to receive(:cats_call_stub) + + subject + end + + it 'does not set the instance variable' do + subject + + expect(repository.instance_variable_defined?(:@cats)).to eq(false) + end + end + + context 'with a method throwing a non-existing-repository error' do + subject do + repository.memoize_method_output(:cats, fallback: fallback) do + raise Gitlab::Git::Repository::NoRepository + end + end + + it 'returns the fallback value' do + expect(subject).to eq(fallback) + end + + it 'does not set the instance variable' do + subject + + expect(repository.instance_variable_defined?(:@cats)).to eq(false) + end + end + + context 'with an existing repository' do + it 'sets the instance variable' do + repository.memoize_method_output(:cats, fallback: fallback) do + 'block output' + end + + expect(repository.instance_variable_get(:@cats)).to eq('block output') + end + end + end + describe '#expire_method_caches' do it 'expires the caches of the given methods' do expect(cache).to receive(:expire).with(:rendered_readme) |