summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAsh McKenzie <amckenzie@gitlab.com>2019-03-20 20:47:53 +1100
committerAsh McKenzie <amckenzie@gitlab.com>2019-03-20 21:00:18 +1100
commit9dbd930c52f93e653c8638ad4354698bbbe9c115 (patch)
tree2a4c979a76f4d77d45c96d52177a4c90e4de9df6
parent60175cd01d81df7c8f16719d4b100933286d0779 (diff)
downloadgitlab-ce-59208-upgrading-to-11-9-0-results-in-a-error-500-on-every-page-when-active-broadcast-message-present.tar.gz
-rw-r--r--changelogs/unreleased/59208-upgrading-to-11-9-0-results-in-a-error-500-on-every-page-when-active-broadcast-message-present.yml5
-rw-r--r--lib/gitlab/json_cache.rb10
-rw-r--r--spec/lib/gitlab/json_cache_spec.rb79
3 files changed, 85 insertions, 9 deletions
diff --git a/changelogs/unreleased/59208-upgrading-to-11-9-0-results-in-a-error-500-on-every-page-when-active-broadcast-message-present.yml b/changelogs/unreleased/59208-upgrading-to-11-9-0-results-in-a-error-500-on-every-page-when-active-broadcast-message-present.yml
new file mode 100644
index 00000000000..9f49b21ca9b
--- /dev/null
+++ b/changelogs/unreleased/59208-upgrading-to-11-9-0-results-in-a-error-500-on-every-page-when-active-broadcast-message-present.yml
@@ -0,0 +1,5 @@
+---
+title: Correctly serialize BroadcastMessage for use with JsonCache
+merge_request: 26356
+author:
+type: fixed
diff --git a/lib/gitlab/json_cache.rb b/lib/gitlab/json_cache.rb
index 24daad638f4..e46ae0962dd 100644
--- a/lib/gitlab/json_cache.rb
+++ b/lib/gitlab/json_cache.rb
@@ -39,7 +39,7 @@ module Gitlab
end
def write(key, value, options = nil)
- backend.write(cache_key(key), value.to_json, options)
+ backend.write(cache_key(key), serialized(value), options)
end
def fetch(key, options = {}, &block)
@@ -57,6 +57,14 @@ module Gitlab
private
+ def serialized(value)
+ if value.respond_to?(:map)
+ value.map { |v| v.respond_to?(:as_json_for_cache) ? v.as_json_for_cache : v.as_json }.to_json
+ else
+ value.respond_to?(:to_json_for_cache) ? value.to_json_for_cache : value.to_json
+ end
+ end
+
def parse_value(raw, klass)
value = ActiveSupport::JSON.decode(raw)
diff --git a/spec/lib/gitlab/json_cache_spec.rb b/spec/lib/gitlab/json_cache_spec.rb
index 2cae8ec031a..ab34b46e692 100644
--- a/spec/lib/gitlab/json_cache_spec.rb
+++ b/spec/lib/gitlab/json_cache_spec.rb
@@ -7,7 +7,7 @@ describe Gitlab::JsonCache do
let(:namespace) { 'geo' }
let(:key) { 'foo' }
let(:expanded_key) { "#{namespace}:#{key}:#{Rails.version}" }
- let(:broadcast_message) { create(:broadcast_message) }
+ set(:broadcast_message) { create(:broadcast_message) }
subject(:cache) { described_class.new(namespace: namespace, backend: backend) }
@@ -192,17 +192,80 @@ describe Gitlab::JsonCache do
end
describe '#write' do
- it 'writes value to the cache with the given key' do
- cache.write(key, true)
+ context 'for a String' do
+ it 'writes value to the cache with the given key' do
+ cache.write(key, 'true')
- expect(backend).to have_received(:write).with(expanded_key, "true", nil)
+ expect(backend).to have_received(:write).with(expanded_key, 'true'.to_json, nil)
+ end
end
- it 'writes a string containing a JSON representation of the value to the cache' do
- cache.write(key, broadcast_message)
+ context 'for a Boolean' do
+ it 'writes value to the cache with the given key' do
+ cache.write(key, true)
- expect(backend).to have_received(:write)
- .with(expanded_key, broadcast_message.to_json, nil)
+ expect(backend).to have_received(:write).with(expanded_key, 'true', nil)
+ end
+ end
+
+ context 'for an Array' do
+ it 'writes a string containing a JSON representation of the value to the cache' do
+ array = %w{a b c}
+ cache.write(key, array)
+
+ expect(backend).to have_received(:write)
+ .with(expanded_key, array.to_json, nil)
+ end
+ end
+
+ context 'for a Hash' do
+ it 'writes a string containing a JSON representation of the value to the cache' do
+ array = %w{a b c}
+ cache.write(key, array)
+
+ expect(backend).to have_received(:write)
+ .with(expanded_key, array.to_json, nil)
+ end
+ end
+
+ context 'for an instance of Project' do
+ it 'writes a string containing a JSON representation of the value to the cache' do
+ project = create(:project)
+ cache.write(key, project)
+
+ expect(backend).to have_received(:write)
+ .with(expanded_key, project.to_json, nil)
+ end
+ end
+
+ context 'for an Array of Projects' do
+ it 'writes a string containing a JSON representation of the value to the cache' do
+ projects = [create(:project)]
+ cache.write(key, projects)
+
+ expect(backend).to have_received(:write)
+ .with(expanded_key, projects.to_json, nil)
+ end
+ end
+
+ context 'for an instance of BroadcaseMessage' do
+ it 'writes a string containing a JSON representation of the value to the cache' do
+ cache.write(key, broadcast_message)
+
+ expect(backend).to have_received(:write)
+ .with(expanded_key, broadcast_message.to_json_for_cache, nil)
+ end
+ end
+
+ context 'for an Array of BroadcaseMessages' do
+ it 'writes a string containing a JSON representation of the value to the cache' do
+ current_broadcast_messages = BroadcastMessage.current
+
+ cache.write(key, current_broadcast_messages)
+
+ expect(backend).to have_received(:write)
+ .with(expanded_key, current_broadcast_messages.map(&:as_json_for_cache).to_json, nil)
+ end
end
it 'passes options the underlying cache implementation' do