From dc407392cd6527a9902e91efabedbdced43594e1 Mon Sep 17 00:00:00 2001 From: fatkodima Date: Thu, 14 Dec 2017 17:05:13 +0200 Subject: Add support for connection pooling on RedisCacheStore --- .../cache/behaviors/connection_pool_behavior.rb | 7 +++- .../test/cache/stores/redis_cache_store_test.rb | 45 ++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/cache/behaviors/connection_pool_behavior.rb b/activesupport/test/cache/behaviors/connection_pool_behavior.rb index 0d46f88552..a0dcc8199c 100644 --- a/activesupport/test/cache/behaviors/connection_pool_behavior.rb +++ b/activesupport/test/cache/behaviors/connection_pool_behavior.rb @@ -6,7 +6,7 @@ module ConnectionPoolBehavior emulating_latency do begin - cache = ActiveSupport::Cache.lookup_store(store, pool_size: 2, pool_timeout: 1) + cache = ActiveSupport::Cache.lookup_store(store, { pool_size: 2, pool_timeout: 1 }.merge(store_options)) cache.clear threads = [] @@ -33,7 +33,7 @@ module ConnectionPoolBehavior def test_no_connection_pool emulating_latency do begin - cache = ActiveSupport::Cache.lookup_store(store) + cache = ActiveSupport::Cache.lookup_store(store, store_options) cache.clear threads = [] @@ -54,4 +54,7 @@ module ConnectionPoolBehavior end end end + + private + def store_options; {}; end end diff --git a/activesupport/test/cache/stores/redis_cache_store_test.rb b/activesupport/test/cache/stores/redis_cache_store_test.rb index 62752d2c65..a2bb4253f0 100644 --- a/activesupport/test/cache/stores/redis_cache_store_test.rb +++ b/activesupport/test/cache/stores/redis_cache_store_test.rb @@ -5,6 +5,24 @@ require "active_support/cache" require "active_support/cache/redis_cache_store" require_relative "../behaviors" +driver_name = %w[ ruby hiredis ].include?(ENV["REDIS_DRIVER"]) ? ENV["REDIS_DRIVER"] : "hiredis" +driver = Object.const_get("Redis::Connection::#{driver_name.camelize}") + +Redis::Connection.drivers.clear +Redis::Connection.drivers.append(driver) + +# Emulates a latency on Redis's back-end for the key latency to facilitate +# connection pool testing. +class SlowRedis < Redis + def get(key, options = {}) + if key =~ /latency/ + sleep 3 + else + super + end + end +end + module ActiveSupport::Cache::RedisCacheStoreTests DRIVER = %w[ ruby hiredis ].include?(ENV["REDIS_DRIVER"]) ? ENV["REDIS_DRIVER"] : "hiredis" @@ -110,6 +128,33 @@ module ActiveSupport::Cache::RedisCacheStoreTests include AutoloadingCacheBehavior end + class ConnectionPoolBehaviourTest < StoreTest + include ConnectionPoolBehavior + + private + + def store + :redis_cache_store + end + + def emulating_latency + old_redis = Object.send(:remove_const, :Redis) + Object.const_set(:Redis, SlowRedis) + + yield + ensure + Object.send(:remove_const, :Redis) + Object.const_set(:Redis, old_redis) + end + end + + class RedisDistributedConnectionPoolBehaviourTest < ConnectionPoolBehaviourTest + private + def store_options + { url: %w[ redis://localhost:6379/0 redis://localhost:6379/0 ] } + end + end + # Separate test class so we can omit the namespace which causes expected, # appropriate complaints about incompatible string encodings. class KeyEncodingSafetyTest < StoreTest -- cgit v1.2.3