debian-mirror-gitlab/spec/models/concerns/reactive_caching_spec.rb

252 lines
6.6 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe ReactiveCaching, :use_clean_rails_memory_store_caching do
2018-11-08 19:23:39 +05:30
include ExclusiveLeaseHelpers
2017-08-17 22:00:37 +05:30
include ReactiveCachingHelpers
class CacheTest
include ReactiveCaching
self.reactive_cache_key = ->(thing) { ["foo", thing.id] }
self.reactive_cache_lifetime = 5.minutes
self.reactive_cache_refresh_interval = 15.seconds
attr_reader :id
2019-07-07 11:18:12 +05:30
def self.primary_key
:id
end
2017-08-17 22:00:37 +05:30
def initialize(id, &blk)
@id = id
@calculator = blk
end
def calculate_reactive_cache
@calculator.call
end
def result
with_reactive_cache do |data|
2019-07-07 11:18:12 +05:30
data
2017-08-17 22:00:37 +05:30
end
end
end
let(:calculation) { -> { 2 + 2 } }
let(:cache_key) { "foo:666" }
let(:instance) { CacheTest.new(666, &calculation) }
describe '#with_reactive_cache' do
2017-09-10 17:25:29 +05:30
before do
stub_reactive_cache
end
2017-08-17 22:00:37 +05:30
subject(:go!) { instance.result }
2019-09-30 21:07:59 +05:30
shared_examples 'a cacheable value' do |cached_value|
2017-09-10 17:25:29 +05:30
before do
2019-09-30 21:07:59 +05:30
stub_reactive_cache(instance, cached_value)
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
2019-09-30 21:07:59 +05:30
it { is_expected.to eq(cached_value) }
2017-08-17 22:00:37 +05:30
2018-11-08 19:23:39 +05:30
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
2017-08-17 22:00:37 +05:30
context 'and expired' do
2017-09-10 17:25:29 +05:30
before do
invalidate_reactive_cache(instance)
end
2017-08-17 22:00:37 +05:30
it { is_expected.to be_nil }
2018-11-18 11:00:15 +05:30
it 'refreshes cache' do
expect(ReactiveCachingWorker).to receive(:perform_async).with(CacheTest, 666)
instance.with_reactive_cache { raise described_class::InvalidateReactiveCache }
end
end
2017-08-17 22:00:37 +05:30
end
2019-07-07 11:18:12 +05:30
2019-09-30 21:07:59 +05:30
context 'when cache is empty' do
it { is_expected.to be_nil }
it 'enqueues a background worker to bootstrap the cache' do
expect(ReactiveCachingWorker).to receive(:perform_async).with(CacheTest, 666)
go!
2019-07-07 11:18:12 +05:30
end
2019-09-30 21:07:59 +05:30
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-07-07 11:18:12 +05:30
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
2017-08-17 22:00:37 +05:30
end
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-11-08 19:23:39 +05:30
it { expect(reactive_cache_alive?(instance)).to be_falsy }
2017-08-17 22:00:37 +05:30
end
describe '#exclusively_update_reactive_cache!' do
subject(:go!) { instance.exclusively_update_reactive_cache! }
context 'when the lease is free and lifetime is not exceeded' do
2017-09-10 17:25:29 +05:30
before do
stub_reactive_cache(instance, "preexisting")
end
2017-08-17 22:00:37 +05:30
it 'takes and releases the lease' do
2018-11-08 19:23:39 +05:30
expect_to_obtain_exclusive_lease(cache_key, 'uuid')
expect_to_cancel_exclusive_lease(cache_key, 'uuid')
2017-08-17 22:00:37 +05:30
go!
end
it 'caches the result of #calculate_reactive_cache' do
go!
expect(read_reactive_cache(instance)).to eq(calculation.call)
end
it "enqueues a repeat worker" do
expect_reactive_cache_update_queued(instance)
go!
end
2018-11-18 11:00:15 +05:30
it "calls a reactive_cache_updated only once if content did not change on subsequent update" do
expect(instance).to receive(:calculate_reactive_cache).twice
expect(instance).to receive(:reactive_cache_updated).once
2.times { instance.exclusively_update_reactive_cache! }
end
2017-08-17 22:00:37 +05:30
context 'and #calculate_reactive_cache raises an exception' do
2017-09-10 17:25:29 +05:30
before do
stub_reactive_cache(instance, "preexisting")
end
2017-08-17 22:00:37 +05:30
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-09-30 21:07:59 +05:30
it 'does not enqueue a repeat worker' do
expect(ReactiveCachingWorker)
.not_to receive(:perform_in)
2017-08-17 22:00:37 +05:30
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
end
context 'when the lease is already taken' do
it 'skips the calculation' do
2018-11-08 19:23:39 +05:30
stub_exclusive_lease_taken(cache_key)
2017-08-17 22:00:37 +05:30
expect(instance).to receive(:calculate_reactive_cache).never
go!
end
end
end
2019-09-04 21:01:54 +05:30
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) }
it { expect(subject.reactive_cache_worker_finder).to respond_to(:call) }
end
2017-08-17 22:00:37 +05:30
end