2015-12-31 15:33:35 -08:00
|
|
|
# frozen_string_literal: true
|
2018-12-07 17:59:42 +01:00
|
|
|
|
2022-03-03 12:50:03 -08:00
|
|
|
require_relative "helper"
|
|
|
|
require "sidekiq/cli"
|
2012-10-14 16:07:37 -07:00
|
|
|
|
2019-03-01 13:25:56 -08:00
|
|
|
describe Sidekiq::RedisConnection do
|
|
|
|
describe "create" do
|
2018-12-03 22:24:37 +01:00
|
|
|
before do
|
|
|
|
Sidekiq.options = Sidekiq::DEFAULTS.dup
|
2022-03-03 12:50:03 -08:00
|
|
|
@old = ENV["REDIS_URL"]
|
|
|
|
ENV["REDIS_URL"] = "redis://localhost/15"
|
2018-12-07 17:59:42 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2022-03-03 12:50:03 -08:00
|
|
|
ENV["REDIS_URL"] = @old
|
2018-12-03 22:24:37 +01:00
|
|
|
end
|
2012-10-14 16:07:37 -07:00
|
|
|
|
2017-09-28 21:40:21 -07: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 16:03:12 -08:00
|
|
|
it "creates a pooled redis connection" do
|
|
|
|
pool = Sidekiq::RedisConnection.create
|
|
|
|
assert_equal Redis, pool.checkout.class
|
|
|
|
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)
|
2022-03-03 12:50:03 -08:00
|
|
|
assert_equal 42, pool.instance_eval { @size }
|
|
|
|
assert_equal 42, pool.instance_eval { @available.length }
|
2018-03-27 16:53:59 -04:00
|
|
|
|
|
|
|
pool = server_connection(size: 42)
|
2022-03-03 12:50:03 -08:00
|
|
|
assert_equal 42, pool.instance_eval { @size }
|
|
|
|
assert_equal 42, pool.instance_eval { @available.length }
|
2018-03-27 16:53:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "defaults server pool sizes based on concurrency with padding" do
|
|
|
|
_expected_padding = 5
|
2019-10-08 14:31:02 +03:00
|
|
|
prev_concurrency = Sidekiq.options[:concurrency]
|
2018-03-27 16:53:59 -04:00
|
|
|
Sidekiq.options[:concurrency] = 6
|
|
|
|
pool = server_connection
|
|
|
|
|
2022-03-03 12:50:03 -08:00
|
|
|
assert_equal 11, pool.instance_eval { @size }
|
|
|
|
assert_equal 11, pool.instance_eval { @available.length }
|
2019-10-08 14:31:02 +03:00
|
|
|
ensure
|
|
|
|
Sidekiq.options[:concurrency] = prev_concurrency
|
2018-03-27 16:53:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "defaults client pool sizes to 5" do
|
|
|
|
pool = client_connection
|
|
|
|
|
2022-03-03 12:50:03 -08:00
|
|
|
assert_equal 5, pool.instance_eval { @size }
|
|
|
|
assert_equal 5, pool.instance_eval { @available.length }
|
2018-03-27 16:53:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "changes client pool sizes with ENV" do
|
2022-03-03 12:50:03 -08:00
|
|
|
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")
|
2018-03-27 16:53:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-21 04:54:18 -07:00
|
|
|
it "disables client setname with nil id" do
|
2022-03-03 12:50:03 -08:00
|
|
|
pool = Sidekiq::RedisConnection.create(id: nil)
|
2017-06-21 04:54:18 -07:00
|
|
|
assert_equal Redis, pool.checkout.class
|
2018-12-07 17:59:42 +01:00
|
|
|
assert_equal "redis://localhost:6379/15", pool.checkout.connection.fetch(:id)
|
2017-06-21 04:54:18 -07:00
|
|
|
end
|
|
|
|
|
2013-07-17 09:02:51 -06:00
|
|
|
describe "network_timeout" do
|
|
|
|
it "sets a custom network_timeout if specified" do
|
2022-03-03 12:50:03 -08:00
|
|
|
pool = Sidekiq::RedisConnection.create(network_timeout: 8)
|
2013-07-17 09:02:51 -06:00
|
|
|
redis = pool.checkout
|
2013-09-22 14:38:33 -07:00
|
|
|
|
2017-09-28 21:40:21 -07:00
|
|
|
assert_equal 8, client_for(redis).timeout
|
2013-07-17 09:02:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it "uses the default network_timeout if none specified" do
|
|
|
|
pool = Sidekiq::RedisConnection.create
|
|
|
|
redis = pool.checkout
|
|
|
|
|
2017-09-28 21:40:21 -07:00
|
|
|
assert_equal 5, client_for(redis).timeout
|
2013-07-17 09:02:51 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-19 16:03:12 -08:00
|
|
|
describe "namespace" do
|
2016-05-26 12:50:37 +07:00
|
|
|
it "uses a given :namespace set by a symbol key" do
|
2022-03-03 12:50:03 -08:00
|
|
|
pool = Sidekiq::RedisConnection.create(namespace: "xxx")
|
2013-01-19 16:03:12 -08:00
|
|
|
assert_equal "xxx", pool.checkout.namespace
|
|
|
|
end
|
|
|
|
|
2016-05-26 12:50:37 +07: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 16:03:12 -08:00
|
|
|
it "uses given :namespace over :namespace from Sidekiq.options" do
|
2013-01-26 08:07:13 -08:00
|
|
|
Sidekiq.options[:namespace] = "xxx"
|
2022-03-03 12:50:03 -08:00
|
|
|
pool = Sidekiq::RedisConnection.create(namespace: "yyy")
|
2013-01-19 16:03:12 -08:00
|
|
|
assert_equal "yyy", pool.checkout.namespace
|
|
|
|
end
|
|
|
|
end
|
2013-07-16 15:40:50 -06:00
|
|
|
|
2013-10-21 21:53:34 +02:00
|
|
|
describe "socket path" do
|
|
|
|
it "uses a given :path" do
|
2022-03-03 12:50:03 -08:00
|
|
|
pool = Sidekiq::RedisConnection.create(path: "/var/run/redis.sock")
|
2017-09-28 21:40:21 -07:00
|
|
|
assert_equal "unix", client_for(pool.checkout).scheme
|
|
|
|
assert_equal "/var/run/redis.sock", pool.checkout.connection.fetch(:location)
|
2018-12-07 17:59:42 +01:00
|
|
|
assert_equal 15, pool.checkout.connection.fetch(:db)
|
2013-10-21 21:53:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "uses a given :path and :db" do
|
2022-03-03 12:50:03 -08:00
|
|
|
pool = Sidekiq::RedisConnection.create(path: "/var/run/redis.sock", db: 8)
|
2017-09-28 21:40:21 -07: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 21:53:34 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-17 09:02:51 -06:00
|
|
|
describe "pool_timeout" do
|
2013-07-16 15:40:50 -06:00
|
|
|
it "uses a given :timeout over the default of 1" do
|
2022-03-03 12:50:03 -08:00
|
|
|
pool = Sidekiq::RedisConnection.create(pool_timeout: 5)
|
2013-09-22 14:38:33 -07:00
|
|
|
|
2022-03-03 12:50:03 -08:00
|
|
|
assert_equal 5, pool.instance_eval { @timeout }
|
2013-07-16 15:40:50 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it "uses the default timeout of 1 if no override" do
|
|
|
|
pool = Sidekiq::RedisConnection.create
|
|
|
|
|
2022-03-03 12:50:03 -08:00
|
|
|
assert_equal 1, pool.instance_eval { @timeout }
|
2013-07-16 15:40:50 -06:00
|
|
|
end
|
|
|
|
end
|
2018-09-09 15:11:37 +02: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
|
2022-03-03 12:50:03 -08:00
|
|
|
redis_driver = Object.new
|
|
|
|
Redis::Connection.drivers << redis_driver
|
2018-09-09 15:11:37 +02:00
|
|
|
|
2022-03-03 12:50:03 -08:00
|
|
|
pool = Sidekiq::RedisConnection.create
|
|
|
|
redis = pool.checkout
|
2018-09-09 15:11:37 +02:00
|
|
|
|
2022-03-03 12:50:03 -08:00
|
|
|
assert_equal redis_driver, redis.instance_variable_get(:@client).driver
|
|
|
|
ensure
|
|
|
|
Redis::Connection.drivers.pop
|
2018-09-09 15:11:37 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "uses a given :driver" do
|
|
|
|
redis_driver = Object.new
|
2022-03-03 12:50:03 -08:00
|
|
|
pool = Sidekiq::RedisConnection.create(driver: redis_driver)
|
2018-09-09 15:11:37 +02:00
|
|
|
redis = pool.checkout
|
|
|
|
|
|
|
|
assert_equal redis_driver, redis.instance_variable_get(:@client).driver
|
|
|
|
end
|
|
|
|
end
|
2019-11-03 06:27:36 -08:00
|
|
|
|
2022-03-03 12:50:03 -08:00
|
|
|
describe "logging redis options" do
|
|
|
|
it "redacts credentials" do
|
2020-04-01 08:11:44 -07:00
|
|
|
options = {
|
2022-03-03 12:50:03 -08:00
|
|
|
role: "master",
|
|
|
|
master_name: "mymaster",
|
2020-04-01 08:11:44 -07:00
|
|
|
sentinels: [
|
2022-03-03 12:50:03 -08:00
|
|
|
{host: "host1", port: 26379, password: "secret"},
|
|
|
|
{host: "host2", port: 26379, password: "secret"},
|
|
|
|
{host: "host3", port: 26379, password: "secret"}
|
2020-04-18 16:00:29 -04:00
|
|
|
],
|
2022-03-03 12:50:03 -08:00
|
|
|
password: "secret"
|
2020-04-01 08:11:44 -07:00
|
|
|
}
|
|
|
|
|
2019-11-03 06:27:36 -08:00
|
|
|
output = capture_logging do
|
2020-04-01 08:11:44 -07:00
|
|
|
Sidekiq::RedisConnection.create(options)
|
2019-11-03 06:27:36 -08:00
|
|
|
end
|
|
|
|
|
2020-04-01 08:11:44 -07:00
|
|
|
refute_includes(options.inspect, "REDACTED")
|
2019-11-03 06:27:36 -08:00
|
|
|
assert_includes(output, ':host=>"host1", :port=>26379, :password=>"REDACTED"')
|
|
|
|
assert_includes(output, ':host=>"host2", :port=>26379, :password=>"REDACTED"')
|
|
|
|
assert_includes(output, ':host=>"host3", :port=>26379, :password=>"REDACTED"')
|
2020-03-23 10:40:44 -07:00
|
|
|
assert_includes(output, ':password=>"REDACTED"')
|
2019-11-03 06:27:36 -08:00
|
|
|
end
|
2020-04-18 16:00:29 -04:00
|
|
|
|
2022-03-03 12:50:03 -08:00
|
|
|
it "prunes SSL parameters from the logging" do
|
2020-04-18 16:00:29 -04:00
|
|
|
options = {
|
|
|
|
ssl_params: {
|
|
|
|
cert_store: OpenSSL::X509::Store.new
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output = capture_logging do
|
|
|
|
Sidekiq::RedisConnection.create(options)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_includes(options.inspect, "ssl_params")
|
|
|
|
refute_includes(output, "ssl_params")
|
|
|
|
end
|
2019-11-03 06:27:36 -08:00
|
|
|
end
|
2013-01-19 16:03:12 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe ".determine_redis_provider" do
|
2014-06-08 13:43:22 +10:00
|
|
|
before do
|
2014-06-08 13:53:47 +10:00
|
|
|
@old_env = ENV.to_hash
|
2014-06-08 13:43:22 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
ENV.update(@old_env)
|
|
|
|
end
|
|
|
|
|
2022-03-03 12:50:03 -08:00
|
|
|
def with_env_var(var, uri, skip_provider = false)
|
|
|
|
vars = ["REDISTOGO_URL", "REDIS_PROVIDER", "REDIS_URL"] - [var]
|
2012-10-14 16:07:37 -07:00
|
|
|
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 16:07:37 -07:00
|
|
|
ENV[var] = nil
|
|
|
|
end
|
|
|
|
|
2013-11-24 08:16:37 -06:00
|
|
|
describe "with REDISTOGO_URL and a parallel REDIS_PROVIDER set" do
|
|
|
|
it "sets connection URI to the provider" do
|
2022-03-03 12:50:03 -08:00
|
|
|
uri = "redis://sidekiq-redis-provider:6379/0"
|
|
|
|
provider = "SIDEKIQ_REDIS_PROVIDER"
|
2013-11-24 08:16:37 -06:00
|
|
|
|
2022-03-03 12:50:03 -08:00
|
|
|
ENV["REDIS_PROVIDER"] = provider
|
2013-11-24 08:16:37 -06:00
|
|
|
ENV[provider] = uri
|
2022-03-03 12:50:03 -08:00
|
|
|
ENV["REDISTOGO_URL"] = "redis://redis-to-go:6379/0"
|
2013-11-24 08:16:37 -06:00
|
|
|
with_env_var provider, uri, true
|
|
|
|
|
|
|
|
ENV[provider] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-14 16:07:37 -07:00
|
|
|
describe "with REDIS_PROVIDER set" do
|
2018-09-17 11:51:27 -07:00
|
|
|
it "rejects URLs in REDIS_PROVIDER" do
|
2022-03-03 12:50:03 -08:00
|
|
|
uri = "redis://sidekiq-redis-provider:6379/0"
|
2018-09-17 11:51:27 -07:00
|
|
|
|
2022-03-03 12:50:03 -08:00
|
|
|
ENV["REDIS_PROVIDER"] = uri
|
2018-09-17 11:51:27 -07:00
|
|
|
|
2018-09-17 11:58:34 -07:00
|
|
|
assert_raises RuntimeError do
|
2018-09-17 11:51:27 -07:00
|
|
|
Sidekiq::RedisConnection.__send__(:determine_redis_provider)
|
2018-09-17 11:58:34 -07:00
|
|
|
end
|
2018-09-17 11:51:27 -07:00
|
|
|
|
2022-03-03 12:50:03 -08:00
|
|
|
ENV["REDIS_PROVIDER"] = nil
|
2018-09-17 11:51:27 -07:00
|
|
|
end
|
|
|
|
|
2012-10-14 16:07:37 -07:00
|
|
|
it "sets connection URI to the provider" do
|
2022-03-03 12:50:03 -08:00
|
|
|
uri = "redis://sidekiq-redis-provider:6379/0"
|
|
|
|
provider = "SIDEKIQ_REDIS_PROVIDER"
|
2012-10-14 16:07:37 -07:00
|
|
|
|
2022-03-03 12:50:03 -08:00
|
|
|
ENV["REDIS_PROVIDER"] = provider
|
2012-10-14 16:07:37 -07:00
|
|
|
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
|
2022-03-03 12:50:03 -08:00
|
|
|
with_env_var "REDIS_URL", "redis://redis-uri:6379/0"
|
2012-10-14 16:07:37 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|