diff options
author | Reuben Pereira <rpereira@gitlab.com> | 2019-04-04 17:27:29 +0000 |
---|---|---|
committer | Michael Kozono <mkozono@gmail.com> | 2019-04-04 17:27:29 +0000 |
commit | 5caced3650bf51bd1035347b9823367dd9095e02 (patch) | |
tree | e0a1467406fdc7a530d66a0e4fdc07546466238c /spec/models | |
parent | a2d044bf97ec350019b2daebd962ab4901070818 (diff) | |
download | gitlab-ce-5caced3650bf51bd1035347b9823367dd9095e02.tar.gz |
Allow reactive caching to be used in services
Add support for defining a reactive_cache_worker_finder function
that will be used by the reactive_caching_worker to generate/initialize
the calling object. This allows reactive caching to work with Services
where the object cannot be obtained from DB but a new object can be
initialized.
Diffstat (limited to 'spec/models')
-rw-r--r-- | spec/models/concerns/reactive_caching_spec.rb | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/spec/models/concerns/reactive_caching_spec.rb b/spec/models/concerns/reactive_caching_spec.rb index 32e13d5abed..53df9e0bc05 100644 --- a/spec/models/concerns/reactive_caching_spec.rb +++ b/spec/models/concerns/reactive_caching_spec.rb @@ -16,6 +16,10 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do attr_reader :id + def self.primary_key + :id + end + def initialize(id, &blk) @id = id @calculator = blk @@ -106,6 +110,46 @@ describe ReactiveCaching, :use_clean_rails_memory_store_caching do end end + describe '.reactive_cache_worker_finder' do + context 'with default reactive_cache_worker_finder' do + let(:args) { %w(other args) } + + before do + allow(instance.class).to receive(:find_by).with(id: instance.id) + .and_return(instance) + end + + it 'calls the activerecord find_by method' do + result = instance.class.reactive_cache_worker_finder.call(instance.id, *args) + + expect(result).to eq(instance) + expect(instance.class).to have_received(:find_by).with(id: instance.id) + end + end + + context 'with custom reactive_cache_worker_finder' do + let(:args) { %w(arg1 arg2) } + let(:instance) { CustomFinderCacheTest.new(666, &calculation) } + + class CustomFinderCacheTest < CacheTest + self.reactive_cache_worker_finder = ->(_id, *args) { from_cache(*args) } + + def self.from_cache(*args); end + end + + before do + allow(instance.class).to receive(:from_cache).with(*args).and_return(instance) + end + + it 'overrides the default reactive_cache_worker_finder' do + result = instance.class.reactive_cache_worker_finder.call(instance.id, *args) + + expect(result).to eq(instance) + expect(instance.class).to have_received(:from_cache).with(*args) + end + end + end + describe '#clear_reactive_cache!' do before do stub_reactive_cache(instance, 4) |