From bd2ade8977a284f2d419a15defb55d02e2a5e13b Mon Sep 17 00:00:00 2001 From: Gonzalo Rodriguez Date: Fri, 22 Jun 2018 17:45:58 -0300 Subject: [PATCH] Only require connection_pool running connection_pool appraisal --- Appraisals | 5 ++ gemfiles/connection_pool_dalli.gemfile | 8 +++ rack-attack.gemspec | 1 - ...e_support_redis_cache_store_pooled_spec.rb | 58 ++++++++++--------- .../connection_pool_dalli_client_spec.rb | 2 +- spec/spec_helper.rb | 5 ++ 6 files changed, 49 insertions(+), 30 deletions(-) create mode 100644 gemfiles/connection_pool_dalli.gemfile diff --git a/Appraisals b/Appraisals index 2ccb781..55b47f0 100644 --- a/Appraisals +++ b/Appraisals @@ -32,3 +32,8 @@ end appraise 'dalli2' do gem 'dalli', '~> 2.0' end + +appraise "connection_pool_dalli" do + gem "connection_pool", "~> 2.2" + gem "dalli", "~> 2.7" +end diff --git a/gemfiles/connection_pool_dalli.gemfile b/gemfiles/connection_pool_dalli.gemfile new file mode 100644 index 0000000..69dc887 --- /dev/null +++ b/gemfiles/connection_pool_dalli.gemfile @@ -0,0 +1,8 @@ +# This file was generated by Appraisal + +source "https://rubygems.org" + +gem "connection_pool", "~> 2.2" +gem "dalli", "~> 2.7" + +gemspec path: "../" diff --git a/rack-attack.gemspec b/rack-attack.gemspec index 588024e..59ba7df 100644 --- a/rack-attack.gemspec +++ b/rack-attack.gemspec @@ -48,6 +48,5 @@ Gem::Specification.new do |s| # which rack-attack uses only for testing compatibility in test suite. s.add_development_dependency 'actionpack', '>= 3.0.0' s.add_development_dependency 'activesupport', '>= 3.0.0' - s.add_development_dependency 'connection_pool' s.add_development_dependency 'redis-activesupport' end diff --git a/spec/acceptance/stores/active_support_redis_cache_store_pooled_spec.rb b/spec/acceptance/stores/active_support_redis_cache_store_pooled_spec.rb index 8c006bb..3447981 100644 --- a/spec/acceptance/stores/active_support_redis_cache_store_pooled_spec.rb +++ b/spec/acceptance/stores/active_support_redis_cache_store_pooled_spec.rb @@ -1,40 +1,42 @@ require_relative "../../spec_helper" -require_relative "../../support/cache_store_helper" -require "timecop" +if defined?(::ConnectionPool) + require_relative "../../support/cache_store_helper" + require "timecop" -if ActiveSupport.version >= Gem::Version.new("5.2.0") - describe "ActiveSupport::Cache::RedisCacheStore (pooled) as a cache backend" do - before do - Rack::Attack.cache.store = ActiveSupport::Cache::RedisCacheStore.new(pool_size: 2) - end - - after do - Rack::Attack.cache.store.clear - end - - it_works_for_cache_backed_features - - it "doesn't leak keys" do - Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request| - request.ip + if ActiveSupport.version >= Gem::Version.new("5.2.0") + describe "ActiveSupport::Cache::RedisCacheStore (pooled) as a cache backend" do + before do + Rack::Attack.cache.store = ActiveSupport::Cache::RedisCacheStore.new(pool_size: 2) end - key = nil - - # Freeze time during these statement to be sure that the key used by rack attack is the same - # we pre-calculate in local variable `key` - Timecop.freeze do - key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4" - - get "/", {}, "REMOTE_ADDR" => "1.2.3.4" + after do + Rack::Attack.cache.store.clear end - assert Rack::Attack.cache.store.fetch(key) + it_works_for_cache_backed_features - sleep 2.1 + it "doesn't leak keys" do + Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request| + request.ip + end - assert_nil Rack::Attack.cache.store.fetch(key) + key = nil + + # Freeze time during these statement to be sure that the key used by rack attack is the same + # we pre-calculate in local variable `key` + Timecop.freeze do + key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4" + + get "/", {}, "REMOTE_ADDR" => "1.2.3.4" + end + + assert Rack::Attack.cache.store.fetch(key) + + sleep 2.1 + + assert_nil Rack::Attack.cache.store.fetch(key) + end end end end diff --git a/spec/acceptance/stores/connection_pool_dalli_client_spec.rb b/spec/acceptance/stores/connection_pool_dalli_client_spec.rb index bb8bf66..15f4564 100644 --- a/spec/acceptance/stores/connection_pool_dalli_client_spec.rb +++ b/spec/acceptance/stores/connection_pool_dalli_client_spec.rb @@ -1,6 +1,6 @@ require_relative "../../spec_helper" -if defined?(::Dalli) +if defined?(::Dalli) && defined?(::ConnectionPool) require_relative "../../support/cache_store_helper" require "connection_pool" require "dalli" diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 71ff76b..4bac5f8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -18,6 +18,11 @@ begin rescue LoadError end +begin + require "connection_pool" +rescue LoadError +end + class MiniTest::Spec include Rack::Test::Methods