Parse the cached value when the it is false

This commit is contained in:
Douglas Barbosa Alexandre 2019-06-25 19:26:29 -03:00
parent 87b468c254
commit cad8bb187d
No known key found for this signature in database
GPG key ID: 4DC4A918C347CAC9
2 changed files with 35 additions and 2 deletions

View file

@ -34,7 +34,7 @@ module Gitlab
def read(key, klass = nil)
value = backend.read(cache_key(key))
value = parse_value(value, klass) if value
value = parse_value(value, klass) unless value.nil?
value
end

View file

@ -129,19 +129,52 @@ describe Gitlab::JsonCache do
.with(expanded_key)
.and_return(nil)
expect(ActiveSupport::JSON).not_to receive(:decode)
expect(cache.read(key)).to be_nil
end
context 'when the cached value is a boolean' do
context 'when the cached value is true' do
it 'parses the cached value' do
allow(backend).to receive(:read)
.with(expanded_key)
.and_return(true)
expect(ActiveSupport::JSON).to receive(:decode).with("true").and_call_original
expect(cache.read(key, BroadcastMessage)).to eq(true)
end
end
context 'when the cached value is false' do
it 'parses the cached value' do
allow(backend).to receive(:read)
.with(expanded_key)
.and_return(false)
expect(ActiveSupport::JSON).to receive(:decode).with("false").and_call_original
expect(cache.read(key, BroadcastMessage)).to eq(false)
end
end
context 'when the cached value is a JSON true value' do
it 'parses the cached value' do
allow(backend).to receive(:read)
.with(expanded_key)
.and_return("true")
expect(cache.read(key, BroadcastMessage)).to eq(true)
end
end
context 'when the cached value is a JSON false value' do
it 'parses the cached value' do
allow(backend).to receive(:read)
.with(expanded_key)
.and_return("false")
expect(cache.read(key, BroadcastMessage)).to eq(false)
end
end
context 'when the cached value is a hash' do
it 'parses the cached value' do
allow(backend).to receive(:read)