Fixes #36956 by dup'ing the value and entry object returned from MemoryStore.

This commit is contained in:
Jonathan Hyman 2019-09-16 08:37:59 -04:00
parent 88fe8d9e7c
commit 948c3c8bd5
2 changed files with 32 additions and 0 deletions

View File

@ -124,6 +124,8 @@ module ActiveSupport
entry = @data[key] entry = @data[key]
synchronize do synchronize do
if entry if entry
entry = entry.dup
entry.dup_value!
@key_access[key] = Time.now.to_f @key_access[key] = Time.now.to_f
else else
@key_access.delete(key) @key_access.delete(key)

View File

@ -107,6 +107,36 @@ class MemoryStorePruningTest < ActiveSupport::TestCase
assert_not @cache.exist?(1) assert_not @cache.exist?(1)
end end
def test_cache_not_mutated
item = { "foo" => "bar" }
key = "test_key"
@cache.write(key, item)
read_item = @cache.read(key)
read_item["foo"] = "xyz"
assert_equal item, @cache.read(key)
end
def test_cache_different_object_ids_hash
item = { "foo" => "bar" }
key = "test_key"
@cache.write(key, item)
read_item = @cache.read(key)
assert_not_equal item.object_id, read_item.object_id
assert_not_equal read_item.object_id, @cache.read(key).object_id
end
def test_cache_different_object_ids_string
item = "my_string"
key = "test_key"
@cache.write(key, item)
read_item = @cache.read(key)
assert_not_equal item.object_id, read_item.object_id
assert_not_equal read_item.object_id, @cache.read(key).object_id
end
def test_write_with_unless_exist def test_write_with_unless_exist
assert_equal true, @cache.write(1, "aaaaaaaaaa") assert_equal true, @cache.write(1, "aaaaaaaaaa")
assert_equal false, @cache.write(1, "aaaaaaaaaa", unless_exist: true) assert_equal false, @cache.write(1, "aaaaaaaaaa", unless_exist: true)