summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStan Hu <stanhu@gmail.com>2019-06-25 23:55:47 +0000
committerStan Hu <stanhu@gmail.com>2019-06-25 23:55:47 +0000
commit5e7833d8c0355bbebf438777022271ec732368ee (patch)
treef5f22a1f368c8a0805363ba6d4559902ccedd347
parent4633df7b9e756ae50f35246e9ba93e1c3ca1e01a (diff)
parentcad8bb187deabcb4fc8ee9b58442845658205ee5 (diff)
downloadgitlab-ce-5e7833d8c0355bbebf438777022271ec732368ee.tar.gz
Merge branch 'da-fix-json-cache-false-values' into 'master'
Parse the cached value when it is false on Gitlab::JsonCache#read See merge request gitlab-org/gitlab-ce!30061
-rw-r--r--lib/gitlab/json_cache.rb2
-rw-r--r--spec/lib/gitlab/json_cache_spec.rb35
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)