2015-12-31 18:33:35 -05:00
|
|
|
# frozen_string_literal: true
|
2018-12-07 11:59:42 -05:00
|
|
|
|
2018-12-03 16:24:37 -05:00
|
|
|
require_relative 'helper'
|
2018-12-03 17:09:26 -05:00
|
|
|
require 'sidekiq/cli'
|
2012-10-14 19:07:37 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
describe Sidekiq::RedisConnection do
|
|
|
|
describe "create" do
|
2018-12-03 16:24:37 -05:00
|
|
|
before do
|
|
|
|
Sidekiq.options = Sidekiq::DEFAULTS.dup
|
2018-12-07 11:59:42 -05:00
|
|
|
@old = ENV['REDIS_URL']
|
|
|
|
ENV['REDIS_URL'] = 'redis://localhost/15'
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
ENV['REDIS_URL'] = @old
|
2018-12-03 16:24:37 -05:00
|
|
|
end
|
2012-10-14 19:07:37 -04:00
|
|
|
|
2017-09-29 00:40:21 -04:00
|
|
|
# To support both redis-rb 3.3.x #client and 4.0.x #_client
|
|
|
|
def client_for(redis)
|
|
|
|
if redis.respond_to?(:_client)
|
|
|
|
redis._client
|
|
|
|
else
|
|
|
|
redis.client
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-19 19:03:12 -05:00
|
|
|
it "creates a pooled redis connection" do
|
|
|
|
pool = Sidekiq::RedisConnection.create
|
|
|
|
assert_equal Redis, pool.checkout.class
|
2017-09-29 00:40:21 -04:00
|
|
|
assert_equal "Sidekiq-server-PID-#{$$}", pool.checkout.connection.fetch(:id)
|
2013-01-19 19:03:12 -05:00
|
|
|
end
|
|
|
|
|
2018-03-27 16:53:59 -04:00
|
|
|
# Readers for these ivars should be available in the next release of
|
|
|
|
# `connection_pool`, until then we need to reach into the internal state to
|
|
|
|
# verify the setting.
|
|
|
|
describe "size" do
|
|
|
|
def client_connection(*args)
|
|
|
|
Sidekiq.stub(:server?, nil) do
|
|
|
|
Sidekiq::RedisConnection.create(*args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def server_connection(*args)
|
|
|
|
Sidekiq.stub(:server?, "constant") do
|
|
|
|
Sidekiq::RedisConnection.create(*args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "uses the specified custom pool size" do
|
|
|
|
pool = client_connection(size: 42)
|
|
|
|
assert_equal 42, pool.instance_eval{ @size }
|
|
|
|
assert_equal 42, pool.instance_eval{ @available.length }
|
|
|
|
|
|
|
|
pool = server_connection(size: 42)
|
|
|
|
assert_equal 42, pool.instance_eval{ @size }
|
|
|
|
assert_equal 42, pool.instance_eval{ @available.length }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "defaults server pool sizes based on concurrency with padding" do
|
|
|
|
_expected_padding = 5
|
|
|
|
Sidekiq.options[:concurrency] = 6
|
|
|
|
pool = server_connection
|
|
|
|
|
|
|
|
assert_equal 11, pool.instance_eval{ @size }
|
|
|
|
assert_equal 11, pool.instance_eval{ @available.length }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "defaults client pool sizes to 5" do
|
|
|
|
pool = client_connection
|
|
|
|
|
|
|
|
assert_equal 5, pool.instance_eval{ @size }
|
|
|
|
assert_equal 5, pool.instance_eval{ @available.length }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "changes client pool sizes with ENV" do
|
|
|
|
begin
|
|
|
|
ENV['RAILS_MAX_THREADS'] = '9'
|
|
|
|
pool = client_connection
|
|
|
|
|
|
|
|
assert_equal 9, pool.instance_eval{ @size }
|
|
|
|
assert_equal 9, pool.instance_eval{ @available.length }
|
|
|
|
ensure
|
|
|
|
ENV.delete('RAILS_MAX_THREADS')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-21 07:54:18 -04:00
|
|
|
it "disables client setname with nil id" do
|
|
|
|
pool = Sidekiq::RedisConnection.create(:id => nil)
|
|
|
|
assert_equal Redis, pool.checkout.class
|
2018-12-07 11:59:42 -05:00
|
|
|
assert_equal "redis://localhost:6379/15", pool.checkout.connection.fetch(:id)
|
2017-06-21 07:54:18 -04:00
|
|
|
end
|
|
|
|
|
2013-07-17 11:02:51 -04:00
|
|
|
describe "network_timeout" do
|
|
|
|
it "sets a custom network_timeout if specified" do
|
|
|
|
pool = Sidekiq::RedisConnection.create(:network_timeout => 8)
|
|
|
|
redis = pool.checkout
|
2013-09-22 17:38:33 -04:00
|
|
|
|
2017-09-29 00:40:21 -04:00
|
|
|
assert_equal 8, client_for(redis).timeout
|
2013-07-17 11:02:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "uses the default network_timeout if none specified" do
|
|
|
|
pool = Sidekiq::RedisConnection.create
|
|
|
|
redis = pool.checkout
|
|
|
|
|
2017-09-29 00:40:21 -04:00
|
|
|
assert_equal 5, client_for(redis).timeout
|
2013-07-17 11:02:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-19 19:03:12 -05:00
|
|
|
describe "namespace" do
|
2016-05-26 01:50:37 -04:00
|
|
|
it "uses a given :namespace set by a symbol key" do
|
2013-01-19 19:03:12 -05:00
|
|
|
pool = Sidekiq::RedisConnection.create(:namespace => "xxx")
|
|
|
|
assert_equal "xxx", pool.checkout.namespace
|
|
|
|
end
|
|
|
|
|
2016-05-26 01:50:37 -04:00
|
|
|
it "uses a given :namespace set by a string key" do
|
|
|
|
pool = Sidekiq::RedisConnection.create("namespace" => "xxx")
|
|
|
|
assert_equal "xxx", pool.checkout.namespace
|
|
|
|
end
|
|
|
|
|
2013-01-19 19:03:12 -05:00
|
|
|
it "uses given :namespace over :namespace from Sidekiq.options" do
|
2013-01-26 11:07:13 -05:00
|
|
|
Sidekiq.options[:namespace] = "xxx"
|
2013-01-19 19:03:12 -05:00
|
|
|
pool = Sidekiq::RedisConnection.create(:namespace => "yyy")
|
|
|
|
assert_equal "yyy", pool.checkout.namespace
|
|
|
|
end
|
|
|
|
end
|
2013-07-16 17:40:50 -04:00
|
|
|
|
2013-10-21 15:53:34 -04:00
|
|
|
describe "socket path" do
|
|
|
|
it "uses a given :path" do
|
|
|
|
pool = Sidekiq::RedisConnection.create(:path => "/var/run/redis.sock")
|
2017-09-29 00:40:21 -04:00
|
|
|
assert_equal "unix", client_for(pool.checkout).scheme
|
|
|
|
assert_equal "/var/run/redis.sock", pool.checkout.connection.fetch(:location)
|
2018-12-07 11:59:42 -05:00
|
|
|
assert_equal 15, pool.checkout.connection.fetch(:db)
|
2013-10-21 15:53:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "uses a given :path and :db" do
|
|
|
|
pool = Sidekiq::RedisConnection.create(:path => "/var/run/redis.sock", :db => 8)
|
2017-09-29 00:40:21 -04:00
|
|
|
assert_equal "unix", client_for(pool.checkout).scheme
|
|
|
|
assert_equal "/var/run/redis.sock", pool.checkout.connection.fetch(:location)
|
|
|
|
assert_equal 8, pool.checkout.connection.fetch(:db)
|
2013-10-21 15:53:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-17 11:02:51 -04:00
|
|
|
describe "pool_timeout" do
|
2013-07-16 17:40:50 -04:00
|
|
|
it "uses a given :timeout over the default of 1" do
|
2013-07-17 11:02:51 -04:00
|
|
|
pool = Sidekiq::RedisConnection.create(:pool_timeout => 5)
|
2013-09-22 17:38:33 -04:00
|
|
|
|
2013-07-16 17:40:50 -04:00
|
|
|
assert_equal 5, pool.instance_eval{ @timeout }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "uses the default timeout of 1 if no override" do
|
|
|
|
pool = Sidekiq::RedisConnection.create
|
|
|
|
|
|
|
|
assert_equal 1, pool.instance_eval{ @timeout }
|
|
|
|
end
|
|
|
|
end
|
2018-09-09 09:11:37 -04:00
|
|
|
|
|
|
|
describe "driver" do
|
|
|
|
it "uses redis' ruby driver" do
|
|
|
|
pool = Sidekiq::RedisConnection.create
|
|
|
|
redis = pool.checkout
|
|
|
|
|
|
|
|
assert_equal Redis::Connection::Ruby, redis.instance_variable_get(:@client).driver
|
|
|
|
end
|
|
|
|
|
|
|
|
it "uses redis' default driver if there are many available" do
|
|
|
|
begin
|
|
|
|
redis_driver = Object.new
|
|
|
|
Redis::Connection.drivers << redis_driver
|
|
|
|
|
|
|
|
pool = Sidekiq::RedisConnection.create
|
|
|
|
redis = pool.checkout
|
|
|
|
|
|
|
|
assert_equal redis_driver, redis.instance_variable_get(:@client).driver
|
|
|
|
ensure
|
|
|
|
Redis::Connection.drivers.pop
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "uses a given :driver" do
|
|
|
|
redis_driver = Object.new
|
|
|
|
pool = Sidekiq::RedisConnection.create(:driver => redis_driver)
|
|
|
|
redis = pool.checkout
|
|
|
|
|
|
|
|
assert_equal redis_driver, redis.instance_variable_get(:@client).driver
|
|
|
|
end
|
|
|
|
end
|
2013-01-19 19:03:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe ".determine_redis_provider" do
|
|
|
|
|
2014-06-07 23:43:22 -04:00
|
|
|
before do
|
2014-06-07 23:53:47 -04:00
|
|
|
@old_env = ENV.to_hash
|
2014-06-07 23:43:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
ENV.update(@old_env)
|
|
|
|
end
|
|
|
|
|
2012-10-14 19:07:37 -04:00
|
|
|
def with_env_var(var, uri, skip_provider=false)
|
|
|
|
vars = ['REDISTOGO_URL', 'REDIS_PROVIDER', 'REDIS_URL'] - [var]
|
|
|
|
vars.each do |v|
|
|
|
|
next if skip_provider
|
|
|
|
ENV[v] = nil
|
|
|
|
end
|
|
|
|
ENV[var] = uri
|
2014-02-14 09:43:34 -05:00
|
|
|
assert_equal uri, Sidekiq::RedisConnection.__send__(:determine_redis_provider)
|
2012-10-14 19:07:37 -04:00
|
|
|
ENV[var] = nil
|
|
|
|
end
|
|
|
|
|
2013-11-24 09:16:37 -05:00
|
|
|
describe "with REDISTOGO_URL and a parallel REDIS_PROVIDER set" do
|
|
|
|
it "sets connection URI to the provider" do
|
|
|
|
uri = 'redis://sidekiq-redis-provider:6379/0'
|
|
|
|
provider = 'SIDEKIQ_REDIS_PROVIDER'
|
|
|
|
|
|
|
|
ENV['REDIS_PROVIDER'] = provider
|
|
|
|
ENV[provider] = uri
|
|
|
|
ENV['REDISTOGO_URL'] = 'redis://redis-to-go:6379/0'
|
|
|
|
with_env_var provider, uri, true
|
|
|
|
|
|
|
|
ENV[provider] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-14 19:07:37 -04:00
|
|
|
describe "with REDIS_PROVIDER set" do
|
2018-09-17 14:51:27 -04:00
|
|
|
it "rejects URLs in REDIS_PROVIDER" do
|
|
|
|
uri = 'redis://sidekiq-redis-provider:6379/0'
|
|
|
|
|
|
|
|
ENV['REDIS_PROVIDER'] = uri
|
|
|
|
|
2018-09-17 14:58:34 -04:00
|
|
|
assert_raises RuntimeError do
|
2018-09-17 14:51:27 -04:00
|
|
|
Sidekiq::RedisConnection.__send__(:determine_redis_provider)
|
2018-09-17 14:58:34 -04:00
|
|
|
end
|
2018-09-17 14:51:27 -04:00
|
|
|
|
|
|
|
ENV['REDIS_PROVIDER'] = nil
|
|
|
|
end
|
|
|
|
|
2012-10-14 19:07:37 -04:00
|
|
|
it "sets connection URI to the provider" do
|
|
|
|
uri = 'redis://sidekiq-redis-provider:6379/0'
|
|
|
|
provider = 'SIDEKIQ_REDIS_PROVIDER'
|
|
|
|
|
|
|
|
ENV['REDIS_PROVIDER'] = provider
|
|
|
|
ENV[provider] = uri
|
|
|
|
|
|
|
|
with_env_var provider, uri, true
|
|
|
|
|
|
|
|
ENV[provider] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with REDIS_URL set" do
|
|
|
|
it "sets connection URI to custom uri" do
|
|
|
|
with_env_var 'REDIS_URL', 'redis://redis-uri:6379/0'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|