From cad8bb187deabcb4fc8ee9b58442845658205ee5 Mon Sep 17 00:00:00 2001 From: Douglas Barbosa Alexandre Date: Tue, 25 Jun 2019 19:26:29 -0300 Subject: [PATCH] Parse the cached value when the it is false --- lib/gitlab/json_cache.rb | 2 +- spec/lib/gitlab/json_cache_spec.rb | 35 +++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/lib/gitlab/json_cache.rb b/lib/gitlab/json_cache.rb index d01183d7845..84c6817f3c7 100644 --- a/lib/gitlab/json_cache.rb +++ b/lib/gitlab/json_cache.rb @@ -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 diff --git a/spec/lib/gitlab/json_cache_spec.rb b/spec/lib/gitlab/json_cache_spec.rb index 59160741c45..39cdd42088e 100644 --- a/spec/lib/gitlab/json_cache_spec.rb +++ b/spec/lib/gitlab/json_cache_spec.rb @@ -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) -- GitLab