Skip to content

Commit c496ae8

Browse files
authored
Merge pull request rails#43693 from esparta/fix_race_conditions_test_cache_iii
ActiveSupport::Cache - fix more race conditions on test/cache - part III
2 parents 0f57141 + f57d111 commit c496ae8

File tree

2 files changed

+52
-27
lines changed

2 files changed

+52
-27
lines changed

activesupport/test/cache/behaviors/cache_instrumentation_behavior.rb

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,35 @@ def test_fetch_multi_uses_write_multi_entries_store_provider_interface
1010
end
1111

1212
def test_write_multi_instrumentation
13-
writes = { "a" => "aa", "b" => "bb" }
13+
key_1 = SecureRandom.uuid
14+
key_2 = SecureRandom.uuid
15+
value_1 = SecureRandom.alphanumeric
16+
value_2 = SecureRandom.alphanumeric
17+
writes = { key_1 => value_1, key_2 => value_2 }
1418

1519
events = with_instrumentation "write_multi" do
1620
@cache.write_multi(writes)
1721
end
1822

1923
assert_equal %w[ cache_write_multi.active_support ], events.map(&:name)
2024
assert_nil events[0].payload[:super_operation]
21-
assert_equal({ "a" => "aa", "b" => "bb" }, events[0].payload[:key])
25+
assert_equal({ key_1 => value_1, key_2 => value_2 }, events[0].payload[:key])
2226
end
2327

2428
def test_instrumentation_with_fetch_multi_as_super_operation
25-
@cache.write("b", "bb")
29+
key_1 = SecureRandom.uuid
30+
@cache.write(key_1, SecureRandom.alphanumeric)
31+
32+
key_2 = SecureRandom.uuid
2633

2734
events = with_instrumentation "read_multi" do
28-
@cache.fetch_multi("a", "b") { |key| key * 2 }
35+
@cache.fetch_multi(key_2, key_1) { |key| key * 2 }
2936
end
3037

3138
assert_equal %w[ cache_read_multi.active_support ], events.map(&:name)
3239
assert_equal :fetch_multi, events[0].payload[:super_operation]
33-
assert_equal ["a", "b"], events[0].payload[:key]
34-
assert_equal ["b"], events[0].payload[:hits]
40+
assert_equal [key_2, key_1], events[0].payload[:key]
41+
assert_equal [key_1], events[0].payload[:hits]
3542
assert_equal @cache.class.name, events[0].payload[:store]
3643
end
3744

@@ -48,15 +55,18 @@ def test_instrumentation_empty_fetch_multi
4855
end
4956

5057
def test_read_multi_instrumentation
51-
@cache.write("b", "bb")
58+
key_1 = SecureRandom.uuid
59+
@cache.write(key_1, SecureRandom.alphanumeric)
60+
61+
key_2 = SecureRandom.uuid
5262

5363
events = with_instrumentation "read_multi" do
54-
@cache.read_multi("a", "b")
64+
@cache.read_multi(key_2, key_1)
5565
end
5666

5767
assert_equal %w[ cache_read_multi.active_support ], events.map(&:name)
58-
assert_equal ["a", "b"], events[0].payload[:key]
59-
assert_equal ["b"], events[0].payload[:hits]
68+
assert_equal [key_2, key_1], events[0].payload[:key]
69+
assert_equal [key_1], events[0].payload[:hits]
6070
assert_equal @cache.class.name, events[0].payload[:store]
6171
end
6272

activesupport/test/cache/stores/redis_cache_store_test.rb

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,10 @@ def test_forward_compatibility
238238
@old_store = lookup_store
239239
ActiveSupport::Cache.format_version = previous_format
240240

241-
@old_store.write("foo", "bar")
242-
assert_equal "bar", @cache.read("foo")
241+
key = SecureRandom.uuid
242+
value = SecureRandom.alphanumeric
243+
@old_store.write(key, value)
244+
assert_equal value, @cache.read(key)
243245
end
244246

245247
def test_backward_compatibility
@@ -248,8 +250,10 @@ def test_backward_compatibility
248250
@old_store = lookup_store
249251
ActiveSupport::Cache.format_version = previous_format
250252

251-
@cache.write("foo", "bar")
252-
assert_equal "bar", @old_store.read("foo")
253+
key = SecureRandom.uuid
254+
value = SecureRandom.alphanumeric
255+
@cache.write(key, value)
256+
assert_equal value, @old_store.read(key)
253257
end
254258

255259
def after_teardown
@@ -363,11 +367,15 @@ def emulating_unavailability
363367

364368
class DeleteMatchedTest < StoreTest
365369
test "deletes keys matching glob" do
366-
@cache.write("foo", "bar")
367-
@cache.write("fu", "baz")
368-
@cache.delete_matched("foo*")
369-
assert_not @cache.exist?("foo")
370-
assert @cache.exist?("fu")
370+
prefix = SecureRandom.alphanumeric
371+
key = "#{prefix}#{SecureRandom.uuid}"
372+
@cache.write(key, "bar")
373+
374+
other_key = SecureRandom.uuid
375+
@cache.write(other_key, SecureRandom.alphanumeric)
376+
@cache.delete_matched("#{prefix}*")
377+
assert_not @cache.exist?(key)
378+
assert @cache.exist?(other_key)
371379
end
372380

373381
test "fails with regexp matchers" do
@@ -379,19 +387,26 @@ class DeleteMatchedTest < StoreTest
379387

380388
class ClearTest < StoreTest
381389
test "clear all cache key" do
382-
@cache.write("foo", "bar")
383-
@cache.write("fu", "baz")
390+
key = SecureRandom.uuid
391+
other_key = SecureRandom.uuid
392+
@cache.write(key, SecureRandom.uuid)
393+
@cache.write(other_key, SecureRandom.uuid)
384394
@cache.clear
385-
assert_not @cache.exist?("foo")
386-
assert_not @cache.exist?("fu")
395+
assert_not @cache.exist?(key)
396+
assert_not @cache.exist?(other_key)
387397
end
388398

389399
test "only clear namespace cache key" do
390-
@cache.write("foo", "bar")
391-
@cache.redis.set("fu", "baz")
400+
key = SecureRandom.uuid
401+
other_key = SecureRandom.uuid
402+
403+
@cache.write(key, SecureRandom.alphanumeric)
404+
@cache.redis.set(other_key, SecureRandom.alphanumeric)
392405
@cache.clear
393-
assert_not @cache.exist?("foo")
394-
assert @cache.redis.exists?("fu")
406+
407+
assert_not @cache.exist?(key)
408+
assert @cache.redis.exists?(other_key)
409+
@cache.redis.del(other_key)
395410
end
396411

397412
test "clear all cache key with Redis::Distributed" do

0 commit comments

Comments
 (0)