aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/cache
diff options
context:
space:
mode:
authorGabriel Sobrinho <gabriel.sobrinho@gmail.com>2017-12-12 22:11:47 -0200
committerRafael Mendonça França <rafaelmfranca@gmail.com>2018-01-18 15:05:16 -0500
commit4ac143d1937e8d6f33162863a17c1b1db57687c1 (patch)
treee9a70dbe26280cee2ba48b42e2a0b720b68cac14 /activesupport/test/cache
parent7df7780e40b08ebc27b9a9650148e93f144ab1e6 (diff)
downloadrails-4ac143d1937e8d6f33162863a17c1b1db57687c1.tar.gz
rails-4ac143d1937e8d6f33162863a17c1b1db57687c1.tar.bz2
rails-4ac143d1937e8d6f33162863a17c1b1db57687c1.zip
Support for connection pooling on mem cache store
Diffstat (limited to 'activesupport/test/cache')
-rw-r--r--activesupport/test/cache/stores/mem_cache_store_test.rb74
1 files changed, 74 insertions, 0 deletions
diff --git a/activesupport/test/cache/stores/mem_cache_store_test.rb b/activesupport/test/cache/stores/mem_cache_store_test.rb
index 99624caf8a..7f537c3bbf 100644
--- a/activesupport/test/cache/stores/mem_cache_store_test.rb
+++ b/activesupport/test/cache/stores/mem_cache_store_test.rb
@@ -5,6 +5,18 @@ require "active_support/cache"
require_relative "../behaviors"
require "dalli"
+# Emulates a latency on Dalli's back-end for the key latency to facilitate
+# connection pool testing.
+class SlowDalliClient < Dalli::Client
+ def get(key, options = {})
+ if key =~ /latency/
+ sleep 3
+ else
+ super
+ end
+ end
+end
+
class MemCacheStoreTest < ActiveSupport::TestCase
begin
ss = Dalli::Client.new("localhost:11211").stats
@@ -34,6 +46,56 @@ class MemCacheStoreTest < ActiveSupport::TestCase
include EncodedKeyCacheBehavior
include AutoloadingCacheBehavior
+ def test_connection_pool
+ emulating_latency do
+ begin
+ cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, pool_size: 2, pool_timeout: 1)
+ cache.clear
+
+ threads = []
+
+ assert_raises Timeout::Error do
+ # One of the three threads will fail in 1 second because our pool size
+ # is only two.
+ 3.times do
+ threads << Thread.new do
+ cache.read("latency")
+ end
+ end
+
+ threads.each(&:join)
+ end
+ ensure
+ threads.each(&:kill)
+ end
+ end
+ end
+
+ def test_no_connection_pool
+ emulating_latency do
+ begin
+ cache = ActiveSupport::Cache.lookup_store(:mem_cache_store)
+ cache.clear
+
+ threads = []
+
+ assert_nothing_raised do
+ # Default connection pool size is 5, assuming 10 will make sure that
+ # the connection pool isn't used at all.
+ 10.times do
+ threads << Thread.new do
+ cache.read("latency")
+ end
+ end
+
+ threads.each(&:join)
+ end
+ ensure
+ threads.each(&:kill)
+ end
+ end
+ end
+
def test_raw_values
cache = ActiveSupport::Cache.lookup_store(:mem_cache_store, raw: true)
cache.clear
@@ -89,4 +151,16 @@ class MemCacheStoreTest < ActiveSupport::TestCase
value << "bingo"
assert_not_equal value, @cache.read("foo")
end
+
+ private
+
+ def emulating_latency
+ old_client = Dalli.send(:remove_const, :Client)
+ Dalli.const_set(:Client, SlowDalliClient)
+
+ yield
+ ensure
+ Dalli.send(:remove_const, :Client)
+ Dalli.const_set(:Client, old_client)
+ end
end