Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky tests for RedisCacheStore #45732

Merged
merged 1 commit into from Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions activesupport/test/cache/behaviors/cache_store_behavior.rb
Expand Up @@ -131,9 +131,9 @@ def test_read_multi_with_expires
end

def test_read_multi_with_empty_keys_and_a_logger_and_no_namespace
@cache.options[:namespace] = nil
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the main offending line - we reset the namespace, and @cache.clear in teardown just flushed the db and we get nils in our tests.

@cache.logger = ActiveSupport::Logger.new(nil)
assert_equal({}, @cache.read_multi)
cache = lookup_store(namespace: nil)
cache.logger = ActiveSupport::Logger.new(nil)
assert_equal({}, cache.read_multi)
end

def test_fetch_multi
Expand Down
34 changes: 16 additions & 18 deletions activesupport/test/cache/stores/redis_cache_store_test.rb
Expand Up @@ -126,23 +126,6 @@ class InitializationTest < ActiveSupport::TestCase
assert_same @cache.redis, redis_instance
end

test "fetch caches nil" do
cache = build
cache.write("foo", nil)
assert_not_called(cache, :write) do
assert_nil cache.fetch("foo") { "baz" }
end
end

test "skip_nil is passed to ActiveSupport::Cache" do
cache = build(skip_nil: true)
cache.clear
assert_not_called(cache, :write) do
assert_nil cache.fetch("foo") { nil }
assert_equal false, cache.exist?("foo")
end
end

private
def build(**kwargs)
ActiveSupport::Cache::RedisCacheStore.new(driver: DRIVER, **kwargs.merge(pool: false)).tap(&:redis)
Expand Down Expand Up @@ -240,6 +223,21 @@ def test_decrement_expires_in
assert @cache.redis.ttl("#{@namespace}:dar") > 0
end

test "fetch caches nil" do
@cache.write("foo", nil)
assert_not_called(@cache, :write) do
assert_nil @cache.fetch("foo") { "baz" }
end
end

test "skip_nil is passed to ActiveSupport::Cache" do
@cache = lookup_store(skip_nil: true)
assert_not_called(@cache, :write) do
assert_nil @cache.fetch("foo") { nil }
assert_equal false, @cache.exist?("foo")
end
end

def test_large_string_with_default_compression_settings
assert_compressed(LARGE_STRING)
end
Expand Down Expand Up @@ -400,7 +398,7 @@ def emulating_unavailability
old_client = Redis.send(:remove_const, :Client)
Redis.const_set(:Client, MaxClientsReachedRedisClient)

yield ActiveSupport::Cache::RedisCacheStore.new
yield ActiveSupport::Cache::RedisCacheStore.new(namespace: @namespace)
ensure
Redis.send(:remove_const, :Client)
Redis.const_set(:Client, old_client)
Expand Down