A platform for community discussion. Free, open, simple.
FIX: Handle `nil` values in `DistributedCache#defer_get_set` (#15978)
David Taylorffcd2e9faf07
diff --git a/lib/distributed_cache.rb b/lib/distributed_cache.rbindex 2597c0a3e56..89e6058d11c 100644--- a/lib/distributed_cache.rb+++ b/lib/distributed_cache.rb@@ -21,7 +21,7 @@ class DistributedCache < MessageBus::DistributedCacheenddef defer_get_set(k, &block)- return self[k] if self[k]+ return self[k] if hash.key? kvalue = block.callself.defer_set(k, value)valuediff --git a/spec/lib/distributed_cache_spec.rb b/spec/lib/distributed_cache_spec.rbnew file mode 100644index 00000000000..91f0022a29a--- /dev/null+++ b/spec/lib/distributed_cache_spec.rb@@ -0,0 +1,31 @@+# frozen_string_literal: true++require 'rails_helper'++describe "DistributedCache extensions" do+ let(:cache) { DistributedCache.new('mytest') }++ it "can defer_get_set" do+ messages = MessageBus.track_publish("/distributed_hash") do+ cache.defer_get_set("key") { "value" }+ end+ expect(messages.size).to eq(1)+ expect(cache["key"]).to eq("value")+ end++ it "works correctly for nil values" do+ block_called_counter = 0+ messages = MessageBus.track_publish("/distributed_hash") do+ 2.times do+ cache.defer_get_set("key") do+ block_called_counter += 1+ nil+ end+ end+ end++ expect(block_called_counter).to eq(1)+ expect(messages.size).to eq(1)+ expect(cache["key"]).to eq(nil)+ end+end