2018-02-05 11:07:49 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe RedisCacheable do
|
2018-05-11 12:21:17 -04:00
|
|
|
let(:model) do
|
|
|
|
Struct.new(:id, :attributes) do
|
|
|
|
def read_attribute(attribute)
|
|
|
|
attributes[attribute]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-12 09:32:13 -04:00
|
|
|
let(:payload) { { name: 'value', time: Time.zone.now } }
|
2018-05-11 12:21:17 -04:00
|
|
|
let(:instance) { model.new(1, payload) }
|
|
|
|
let(:cache_key) { instance.__send__(:cache_attribute_key) }
|
2018-02-06 12:42:28 -05:00
|
|
|
|
|
|
|
before do
|
2018-05-11 12:21:17 -04:00
|
|
|
model.include(described_class)
|
2018-02-06 12:42:28 -05:00
|
|
|
end
|
2018-02-05 11:07:49 -05:00
|
|
|
|
|
|
|
describe '#cached_attribute' do
|
2018-05-11 12:21:17 -04:00
|
|
|
subject { instance.cached_attribute(payload.keys.first) }
|
2018-02-05 11:07:49 -05:00
|
|
|
|
|
|
|
it 'gets the cache attribute' do
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
2018-05-11 12:21:17 -04:00
|
|
|
expect(redis).to receive(:get).with(cache_key)
|
2018-02-06 13:01:23 -05:00
|
|
|
.and_return(payload.to_json)
|
2018-02-05 11:07:49 -05:00
|
|
|
end
|
|
|
|
|
2018-02-06 13:01:23 -05:00
|
|
|
expect(subject).to eq(payload.values.first)
|
2018-02-05 11:07:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#cache_attributes' do
|
2018-05-11 12:21:17 -04:00
|
|
|
subject { instance.cache_attributes(payload) }
|
2018-02-05 11:07:49 -05:00
|
|
|
|
|
|
|
it 'sets the cache attributes' do
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
2018-05-11 12:21:17 -04:00
|
|
|
expect(redis).to receive(:set).with(cache_key, payload.to_json, anything)
|
2018-02-05 11:07:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
2018-05-11 12:21:17 -04:00
|
|
|
|
2018-05-12 08:43:05 -04:00
|
|
|
describe '#cached_attr_reader', :clean_gitlab_redis_shared_state do
|
2018-05-11 12:21:17 -04:00
|
|
|
subject { instance.name }
|
|
|
|
|
|
|
|
before do
|
|
|
|
model.cached_attr_reader(:name)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is no cached value' do
|
2018-05-12 09:32:13 -04:00
|
|
|
it 'reads the attribute' do
|
2018-05-11 12:21:17 -04:00
|
|
|
expect(subject).to eq(payload[:name])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is a cached value' do
|
|
|
|
it 'reads the cached value' do
|
2018-05-12 08:43:05 -04:00
|
|
|
instance.cache_attributes(payload)
|
2018-05-11 12:21:17 -04:00
|
|
|
|
|
|
|
expect(subject).to eq(payload[:name])
|
|
|
|
end
|
|
|
|
end
|
2018-05-12 09:32:13 -04:00
|
|
|
|
|
|
|
it 'always returns the latest values' do
|
|
|
|
expect(instance.name).to eq(payload[:name])
|
|
|
|
|
|
|
|
instance.cache_attributes(name: 'new_value')
|
|
|
|
|
|
|
|
expect(instance.name).to eq('new_value')
|
|
|
|
end
|
2018-05-11 12:21:17 -04:00
|
|
|
end
|
|
|
|
|
2018-05-12 08:43:05 -04:00
|
|
|
describe '#cached_attr_time_reader', :clean_gitlab_redis_shared_state do
|
2018-05-11 12:21:17 -04:00
|
|
|
subject { instance.time }
|
|
|
|
|
|
|
|
before do
|
|
|
|
model.cached_attr_time_reader(:time)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is no cached value' do
|
2018-05-12 09:32:13 -04:00
|
|
|
it 'reads the attribute' do
|
2018-05-11 12:21:17 -04:00
|
|
|
expect(subject).to be_instance_of(ActiveSupport::TimeWithZone)
|
|
|
|
expect(subject).to be_within(1.minute).of(Time.zone.now)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is a cached value' do
|
|
|
|
it 'reads the cached value' do
|
2018-05-12 08:43:05 -04:00
|
|
|
instance.cache_attributes(time: Time.zone.now)
|
2018-05-11 12:21:17 -04:00
|
|
|
|
|
|
|
expect(subject).to be_instance_of(ActiveSupport::TimeWithZone)
|
|
|
|
expect(subject).to be_within(1.minute).of(Time.zone.now)
|
|
|
|
end
|
|
|
|
end
|
2018-05-12 09:32:13 -04:00
|
|
|
|
|
|
|
it 'always returns the latest values' do
|
|
|
|
expect(instance.time).to be_within(1.minute).of(Time.zone.now)
|
|
|
|
|
|
|
|
instance.cache_attributes(time: 1.hour.ago)
|
|
|
|
|
|
|
|
expect(instance.time).to be_within(1.minute).of(1.hour.ago)
|
|
|
|
end
|
2018-05-11 12:21:17 -04:00
|
|
|
end
|
2018-02-05 11:07:49 -05:00
|
|
|
end
|