/
test_redis_connection.rb
282 lines (227 loc) · 8.39 KB
/
test_redis_connection.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# frozen_string_literal: true
require_relative 'helper'
require 'sidekiq/cli'
describe Sidekiq::RedisConnection do
describe "create" do
before do
Sidekiq.options = Sidekiq::DEFAULTS.dup
@old = ENV['REDIS_URL']
ENV['REDIS_URL'] = 'redis://localhost/15'
end
after do
ENV['REDIS_URL'] = @old
end
# 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
it "creates a pooled redis connection" do
pool = Sidekiq::RedisConnection.create
assert_equal Redis, pool.checkout.class
end
# 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
prev_concurrency = Sidekiq.options[:concurrency]
Sidekiq.options[:concurrency] = 6
pool = server_connection
assert_equal 11, pool.instance_eval{ @size }
assert_equal 11, pool.instance_eval{ @available.length }
ensure
Sidekiq.options[:concurrency] = prev_concurrency
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
it "disables client setname with nil id" do
pool = Sidekiq::RedisConnection.create(:id => nil)
assert_equal Redis, pool.checkout.class
assert_equal "redis://localhost:6379/15", pool.checkout.connection.fetch(:id)
end
describe "network_timeout" do
it "sets a custom network_timeout if specified" do
pool = Sidekiq::RedisConnection.create(:network_timeout => 8)
redis = pool.checkout
assert_equal 8, client_for(redis).timeout
end
it "uses the default network_timeout if none specified" do
pool = Sidekiq::RedisConnection.create
redis = pool.checkout
assert_equal 5, client_for(redis).timeout
end
end
describe "namespace" do
it "uses a given :namespace set by a symbol key" do
pool = Sidekiq::RedisConnection.create(:namespace => "xxx")
assert_equal "xxx", pool.checkout.namespace
end
it "uses a given :namespace set by a string key" do
pool = Sidekiq::RedisConnection.create("namespace" => "xxx")
assert_equal "xxx", pool.checkout.namespace
end
it "uses given :namespace over :namespace from Sidekiq.options" do
Sidekiq.options[:namespace] = "xxx"
pool = Sidekiq::RedisConnection.create(:namespace => "yyy")
assert_equal "yyy", pool.checkout.namespace
end
end
describe "socket path" do
it "uses a given :path" do
pool = Sidekiq::RedisConnection.create(:path => "/var/run/redis.sock")
assert_equal "unix", client_for(pool.checkout).scheme
assert_equal "/var/run/redis.sock", pool.checkout.connection.fetch(:location)
assert_equal 15, pool.checkout.connection.fetch(:db)
end
it "uses a given :path and :db" do
pool = Sidekiq::RedisConnection.create(:path => "/var/run/redis.sock", :db => 8)
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)
end
end
describe "pool_timeout" do
it "uses a given :timeout over the default of 1" do
pool = Sidekiq::RedisConnection.create(:pool_timeout => 5)
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
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
describe 'logging redis options' do
it 'redacts credentials' do
output = capture_logging do
Sidekiq::RedisConnection.create(
role: 'master',
master_name: 'mymaster',
sentinels: [
{ host: 'host1', port: 26379, password: 'secret'},
{ host: 'host2', port: 26379, password: 'secret'},
{ host: 'host3', port: 26379, password: 'secret'},
],
password: 'secret'
)
end
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"')
assert_includes(output, ':password=>"REDACTED"')
end
end
end
describe ".determine_redis_provider" do
before do
@old_env = ENV.to_hash
end
after do
ENV.update(@old_env)
end
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
assert_equal uri, Sidekiq::RedisConnection.__send__(:determine_redis_provider)
ENV[var] = nil
end
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
describe "with REDIS_PROVIDER set" do
it "rejects URLs in REDIS_PROVIDER" do
uri = 'redis://sidekiq-redis-provider:6379/0'
ENV['REDIS_PROVIDER'] = uri
assert_raises RuntimeError do
Sidekiq::RedisConnection.__send__(:determine_redis_provider)
end
ENV['REDIS_PROVIDER'] = nil
end
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