2019-03-30 07:23:56 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-17 04:09:50 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe ReactiveCaching, :use_clean_rails_memory_store_caching do
|
2018-06-28 18:51:36 +00:00
|
|
|
include ExclusiveLeaseHelpers
|
2016-12-17 04:09:50 +00:00
|
|
|
include ReactiveCachingHelpers
|
|
|
|
|
2020-04-24 09:09:44 +00:00
|
|
|
let(:cache_class_test) do
|
|
|
|
Class.new do
|
|
|
|
include ReactiveCaching
|
2016-12-17 04:09:50 +00:00
|
|
|
|
2020-04-24 09:09:44 +00:00
|
|
|
self.reactive_cache_key = ->(thing) { ["foo", thing.id] }
|
2016-12-17 04:09:50 +00:00
|
|
|
|
2020-04-24 09:09:44 +00:00
|
|
|
self.reactive_cache_lifetime = 5.minutes
|
|
|
|
self.reactive_cache_refresh_interval = 15.seconds
|
2020-09-22 12:09:39 +00:00
|
|
|
self.reactive_cache_work_type = :no_dependency
|
2016-12-17 04:09:50 +00:00
|
|
|
|
2020-04-24 09:09:44 +00:00
|
|
|
attr_reader :id
|
2016-12-17 04:09:50 +00:00
|
|
|
|
2020-04-24 09:09:44 +00:00
|
|
|
def self.primary_key
|
|
|
|
:id
|
|
|
|
end
|
2019-04-04 17:27:29 +00:00
|
|
|
|
2020-04-24 09:09:44 +00:00
|
|
|
def initialize(id, &blk)
|
|
|
|
@id = id
|
|
|
|
@calculator = blk
|
|
|
|
end
|
2016-12-17 04:09:50 +00:00
|
|
|
|
2020-04-24 09:09:44 +00:00
|
|
|
def calculate_reactive_cache
|
|
|
|
@calculator.call
|
|
|
|
end
|
2016-12-17 04:09:50 +00:00
|
|
|
|
2020-04-24 09:09:44 +00:00
|
|
|
def result
|
|
|
|
with_reactive_cache do |data|
|
|
|
|
data
|
|
|
|
end
|
2016-12-17 04:09:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-24 09:09:44 +00:00
|
|
|
let(:external_dependency_cache_class_test) do
|
|
|
|
Class.new(cache_class_test) do
|
|
|
|
self.reactive_cache_work_type = :external_dependency
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-17 04:09:50 +00:00
|
|
|
let(:calculation) { -> { 2 + 2 } }
|
|
|
|
let(:cache_key) { "foo:666" }
|
2020-04-24 09:09:44 +00:00
|
|
|
let(:instance) { cache_class_test.new(666, &calculation) }
|
2016-12-17 04:09:50 +00:00
|
|
|
|
|
|
|
describe '#with_reactive_cache' do
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
|
|
|
stub_reactive_cache
|
|
|
|
end
|
|
|
|
|
2016-12-17 04:09:50 +00:00
|
|
|
subject(:go!) { instance.result }
|
|
|
|
|
2020-04-24 09:09:44 +00:00
|
|
|
shared_examples 'reactive worker call' do |worker_class|
|
|
|
|
let(:instance) do
|
|
|
|
test_class.new(666, &calculation)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'performs caching with correct worker' do
|
|
|
|
expect(worker_class).to receive(:perform_async).with(test_class, 666)
|
|
|
|
|
|
|
|
go!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-08 10:18:07 +00:00
|
|
|
shared_examples 'a cacheable value' do |cached_value|
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
2019-07-08 10:18:07 +00:00
|
|
|
stub_reactive_cache(instance, cached_value)
|
2017-06-14 18:18:56 +00:00
|
|
|
end
|
2016-12-17 04:09:50 +00:00
|
|
|
|
2019-07-08 10:18:07 +00:00
|
|
|
it { is_expected.to eq(cached_value) }
|
2016-12-17 04:09:50 +00:00
|
|
|
|
2018-05-08 06:07:48 +00:00
|
|
|
it 'does not enqueue a background worker' do
|
|
|
|
expect(ReactiveCachingWorker).not_to receive(:perform_async)
|
|
|
|
|
|
|
|
go!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the cache lifespan' do
|
|
|
|
expect(Rails.cache).to receive(:write).with(alive_reactive_cache_key(instance), true, expires_in: anything)
|
|
|
|
|
|
|
|
go!
|
|
|
|
end
|
|
|
|
|
2016-12-17 04:09:50 +00:00
|
|
|
context 'and expired' do
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
|
|
|
invalidate_reactive_cache(instance)
|
|
|
|
end
|
|
|
|
|
2016-12-17 04:09:50 +00:00
|
|
|
it { is_expected.to be_nil }
|
2018-08-07 14:02:57 +00:00
|
|
|
|
2020-04-24 09:09:44 +00:00
|
|
|
it_behaves_like 'reactive worker call', ReactiveCachingWorker do
|
|
|
|
let(:test_class) { cache_class_test }
|
|
|
|
end
|
2018-08-07 14:02:57 +00:00
|
|
|
|
2020-04-24 09:09:44 +00:00
|
|
|
it_behaves_like 'reactive worker call', ExternalServiceReactiveCachingWorker do
|
|
|
|
let(:test_class) { external_dependency_cache_class_test }
|
2018-08-07 14:02:57 +00:00
|
|
|
end
|
|
|
|
end
|
2016-12-17 04:09:50 +00:00
|
|
|
end
|
2019-02-18 11:24:00 +00:00
|
|
|
|
2019-07-08 10:18:07 +00:00
|
|
|
context 'when cache is empty' do
|
|
|
|
it { is_expected.to be_nil }
|
|
|
|
|
2020-04-24 09:09:44 +00:00
|
|
|
it_behaves_like 'reactive worker call', ReactiveCachingWorker do
|
|
|
|
let(:test_class) { cache_class_test }
|
|
|
|
end
|
2019-07-08 10:18:07 +00:00
|
|
|
|
2020-04-24 09:09:44 +00:00
|
|
|
it_behaves_like 'reactive worker call', ExternalServiceReactiveCachingWorker do
|
|
|
|
let(:test_class) { external_dependency_cache_class_test }
|
2019-02-18 11:24:00 +00:00
|
|
|
end
|
|
|
|
|
2019-07-08 10:18:07 +00:00
|
|
|
it 'updates the cache lifespan' do
|
|
|
|
expect(reactive_cache_alive?(instance)).to be_falsy
|
|
|
|
|
|
|
|
go!
|
|
|
|
|
|
|
|
expect(reactive_cache_alive?(instance)).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the cache is full' do
|
|
|
|
it_behaves_like 'a cacheable value', 4
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the cache contains non-nil but blank value' do
|
|
|
|
it_behaves_like 'a cacheable value', false
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the cache contains nil value' do
|
|
|
|
it_behaves_like 'a cacheable value', nil
|
2019-02-18 11:24:00 +00:00
|
|
|
end
|
2016-12-17 04:09:50 +00:00
|
|
|
end
|
|
|
|
|
2020-02-26 15:08:56 +00:00
|
|
|
describe '#with_reactive_cache_set', :use_clean_rails_redis_caching do
|
|
|
|
subject(:go!) do
|
|
|
|
instance.with_reactive_cache_set('resource', {}) do |data|
|
|
|
|
data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calls with_reactive_cache' do
|
|
|
|
expect(instance)
|
|
|
|
.to receive(:with_reactive_cache)
|
|
|
|
|
|
|
|
go!
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'data returned' do
|
|
|
|
let(:resource) { 'resource' }
|
|
|
|
let(:set_key) { "#{cache_key}:#{resource}" }
|
|
|
|
let(:set_cache) { Gitlab::ReactiveCacheSetCache.new }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_reactive_cache(instance, true, resource, {})
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'saves keys in set' do
|
|
|
|
expect(set_cache.read(set_key)).to be_empty
|
|
|
|
|
|
|
|
go!
|
|
|
|
|
|
|
|
expect(set_cache.read(set_key)).not_to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the data' do
|
|
|
|
expect(go!).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-04 17:27:29 +00:00
|
|
|
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) }
|
2020-04-24 09:09:44 +00:00
|
|
|
let(:instance) { custom_finder_cache_test.new(666, &calculation) }
|
2019-04-04 17:27:29 +00:00
|
|
|
|
2020-04-24 09:09:44 +00:00
|
|
|
let(:custom_finder_cache_test) do
|
|
|
|
Class.new(cache_class_test) do
|
|
|
|
self.reactive_cache_worker_finder = ->(_id, *args) { from_cache(*args) }
|
2019-04-04 17:27:29 +00:00
|
|
|
|
2020-04-24 09:09:44 +00:00
|
|
|
def self.from_cache(*args); end
|
|
|
|
end
|
2019-04-04 17:27:29 +00:00
|
|
|
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
|
|
|
|
|
2016-12-17 04:09:50 +00:00
|
|
|
describe '#clear_reactive_cache!' do
|
|
|
|
before do
|
|
|
|
stub_reactive_cache(instance, 4)
|
|
|
|
instance.clear_reactive_cache!
|
|
|
|
end
|
|
|
|
|
|
|
|
it { expect(instance.result).to be_nil }
|
2018-06-01 13:15:13 +00:00
|
|
|
it { expect(reactive_cache_alive?(instance)).to be_falsy }
|
2016-12-17 04:09:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#exclusively_update_reactive_cache!' do
|
|
|
|
subject(:go!) { instance.exclusively_update_reactive_cache! }
|
|
|
|
|
2020-02-10 15:08:54 +00:00
|
|
|
shared_examples 'successful cache' do
|
|
|
|
it 'caches the result of #calculate_reactive_cache' do
|
|
|
|
go!
|
|
|
|
|
|
|
|
expect(read_reactive_cache(instance)).to eq(calculation.call)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not raise the exception' do
|
|
|
|
expect { go! }.not_to raise_exception(ReactiveCaching::ExceededReactiveCacheLimit)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-17 04:09:50 +00:00
|
|
|
context 'when the lease is free and lifetime is not exceeded' do
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
2020-02-10 15:08:54 +00:00
|
|
|
stub_reactive_cache(instance, 'preexisting')
|
2017-06-14 18:18:56 +00:00
|
|
|
end
|
2016-12-17 04:09:50 +00:00
|
|
|
|
2020-02-10 15:08:54 +00:00
|
|
|
it_behaves_like 'successful cache'
|
|
|
|
|
2016-12-17 04:09:50 +00:00
|
|
|
it 'takes and releases the lease' do
|
2018-06-28 18:51:36 +00:00
|
|
|
expect_to_obtain_exclusive_lease(cache_key, 'uuid')
|
|
|
|
expect_to_cancel_exclusive_lease(cache_key, 'uuid')
|
2016-12-17 04:09:50 +00:00
|
|
|
|
|
|
|
go!
|
|
|
|
end
|
|
|
|
|
2020-02-10 15:08:54 +00:00
|
|
|
it 'enqueues a repeat worker' do
|
2016-12-17 04:09:50 +00:00
|
|
|
expect_reactive_cache_update_queued(instance)
|
|
|
|
|
|
|
|
go!
|
|
|
|
end
|
|
|
|
|
2020-04-24 09:09:44 +00:00
|
|
|
context 'when :external_dependency cache' do
|
|
|
|
let(:instance) do
|
|
|
|
external_dependency_cache_class_test.new(666, &calculation)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'enqueues a repeat worker' do
|
|
|
|
expect_reactive_cache_update_queued(instance, worker_klass: ExternalServiceReactiveCachingWorker)
|
|
|
|
|
|
|
|
go!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-10 15:08:54 +00:00
|
|
|
it 'calls a reactive_cache_updated only once if content did not change on subsequent update' do
|
2018-07-25 10:03:18 +00:00
|
|
|
expect(instance).to receive(:calculate_reactive_cache).twice
|
|
|
|
expect(instance).to receive(:reactive_cache_updated).once
|
|
|
|
|
|
|
|
2.times { instance.exclusively_update_reactive_cache! }
|
|
|
|
end
|
|
|
|
|
2019-11-19 18:06:27 +00:00
|
|
|
it 'does not delete the value key' do
|
|
|
|
expect(Rails.cache).to receive(:delete).with(cache_key).never
|
|
|
|
|
|
|
|
go!
|
|
|
|
end
|
|
|
|
|
2020-06-24 12:09:24 +00:00
|
|
|
context 'when reactive_cache_hard_limit is set' do
|
|
|
|
let(:test_class) { Class.new(cache_class_test) { self.reactive_cache_hard_limit = 1.megabyte } }
|
|
|
|
let(:instance) { test_class.new(666, &calculation) }
|
|
|
|
|
|
|
|
context 'when cache size is over the overridden limit' do
|
|
|
|
let(:calculation) { -> { 'a' * 2 * 1.megabyte } }
|
2020-02-10 15:08:54 +00:00
|
|
|
|
|
|
|
it 'raises ExceededReactiveCacheLimit exception and does not cache new data' do
|
|
|
|
expect { go! }.to raise_exception(ReactiveCaching::ExceededReactiveCacheLimit)
|
|
|
|
|
|
|
|
expect(read_reactive_cache(instance)).not_to eq(calculation.call)
|
|
|
|
end
|
|
|
|
|
2020-07-08 06:09:13 +00:00
|
|
|
context 'when reactive_cache_limit_enabled? is overridden to return false' do
|
2020-06-24 12:09:24 +00:00
|
|
|
before do
|
|
|
|
allow(instance).to receive(:reactive_cache_limit_enabled?).and_return(false)
|
|
|
|
end
|
2020-02-10 15:08:54 +00:00
|
|
|
|
|
|
|
it_behaves_like 'successful cache'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-24 12:09:24 +00:00
|
|
|
context 'when cache size is within the overridden limit' do
|
|
|
|
let(:calculation) { -> { 'Smaller than 1Mb reactive_cache_hard_limit' } }
|
2020-02-10 15:08:54 +00:00
|
|
|
|
|
|
|
it_behaves_like 'successful cache'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-17 04:09:50 +00:00
|
|
|
context 'and #calculate_reactive_cache raises an exception' do
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
|
|
|
stub_reactive_cache(instance, "preexisting")
|
|
|
|
end
|
|
|
|
|
2016-12-17 04:09:50 +00:00
|
|
|
let(:calculation) { -> { raise "foo"} }
|
|
|
|
|
|
|
|
it 'leaves the cache untouched' do
|
|
|
|
expect { go! }.to raise_error("foo")
|
|
|
|
expect(read_reactive_cache(instance)).to eq("preexisting")
|
|
|
|
end
|
|
|
|
|
2019-07-06 07:58:13 +00:00
|
|
|
it 'does not enqueue a repeat worker' do
|
|
|
|
expect(ReactiveCachingWorker)
|
|
|
|
.not_to receive(:perform_in)
|
2016-12-17 04:09:50 +00:00
|
|
|
|
|
|
|
expect { go! }.to raise_error("foo")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when lifetime is exceeded' do
|
|
|
|
it 'skips the calculation' do
|
|
|
|
expect(instance).to receive(:calculate_reactive_cache).never
|
|
|
|
|
|
|
|
go!
|
|
|
|
end
|
2019-11-19 18:06:27 +00:00
|
|
|
|
|
|
|
it 'deletes the value key' do
|
|
|
|
expect(Rails.cache).to receive(:delete).with(cache_key).once
|
|
|
|
|
|
|
|
go!
|
|
|
|
end
|
2016-12-17 04:09:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the lease is already taken' do
|
|
|
|
it 'skips the calculation' do
|
2018-06-28 18:51:36 +00:00
|
|
|
stub_exclusive_lease_taken(cache_key)
|
|
|
|
|
2016-12-17 04:09:50 +00:00
|
|
|
expect(instance).to receive(:calculate_reactive_cache).never
|
|
|
|
|
|
|
|
go!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-06-07 07:17:33 +00:00
|
|
|
|
|
|
|
describe 'default options' do
|
|
|
|
let(:cached_class) { Class.new { include ReactiveCaching } }
|
|
|
|
|
|
|
|
subject { cached_class.new }
|
|
|
|
|
|
|
|
it { expect(subject.reactive_cache_lease_timeout).to be_a(ActiveSupport::Duration) }
|
|
|
|
it { expect(subject.reactive_cache_refresh_interval).to be_a(ActiveSupport::Duration) }
|
|
|
|
it { expect(subject.reactive_cache_lifetime).to be_a(ActiveSupport::Duration) }
|
|
|
|
it { expect(subject.reactive_cache_key).to respond_to(:call) }
|
2020-06-24 12:09:24 +00:00
|
|
|
it { expect(subject.reactive_cache_hard_limit).to be_nil }
|
2019-06-07 07:17:33 +00:00
|
|
|
it { expect(subject.reactive_cache_worker_finder).to respond_to(:call) }
|
|
|
|
end
|
2020-09-22 12:09:39 +00:00
|
|
|
|
|
|
|
describe 'classes including this concern' do
|
|
|
|
it 'sets reactive_cache_work_type' do
|
|
|
|
classes = ObjectSpace.each_object(Class).select do |klass|
|
|
|
|
klass < described_class && klass.name
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(classes).to all(have_attributes(reactive_cache_work_type: be_in(described_class::WORK_TYPE.keys)))
|
|
|
|
end
|
|
|
|
end
|
2016-12-17 04:09:50 +00:00
|
|
|
end
|