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
require 'spec_helper'
describe Gitlab::Redis do
let(:redis_config) { Rails.root.join('config', 'resque.yml').to_s }
before(:each) { clear_raw_config }
after(:each) { clear_raw_config }
describe '.params' do
subject { described_class.params }
it 'withstands mutation' do
params1 = described_class.params
params2 = described_class.params
params1[:foo] = :bar
expect(params2).not_to have_key(:foo)
end
context 'when url contains unix socket reference' do
let(:config_old) { Rails.root.join('spec/fixtures/config/redis_old_format_socket.yml').to_s }
let(:config_new) { Rails.root.join('spec/fixtures/config/redis_new_format_socket.yml').to_s }
context 'with old format' do
it 'returns path key instead' do
stub_const("#{described_class}::CONFIG_FILE", config_old)
is_expected.to include(path: '/path/to/old/redis.sock')
is_expected.not_to have_key(:url)
end
end
context 'with new format' do
it 'returns path key instead' do
stub_const("#{described_class}::CONFIG_FILE", config_new)
is_expected.to include(path: '/path/to/redis.sock')
is_expected.not_to have_key(:url)
end
end
end
context 'when url is host based' do
let(:config_old) { Rails.root.join('spec/fixtures/config/redis_old_format_host.yml') }
let(:config_new) { Rails.root.join('spec/fixtures/config/redis_new_format_host.yml') }
context 'with old format' do
it 'returns hash with host, port, db, and password' do
stub_const("#{described_class}::CONFIG_FILE", config_old)
is_expected.to include(host: 'localhost', password: 'mypassword', port: 6379, db: 99)
is_expected.not_to have_key(:url)
end
end
context 'with new format' do
it 'returns hash with host, port, db, and password' do
stub_const("#{described_class}::CONFIG_FILE", config_new)
is_expected.to include(host: 'localhost', password: 'mynewpassword', port: 6379, db: 99)
is_expected.not_to have_key(:url)
end
end
end
end
describe '.url' do
it 'withstands mutation' do
url1 = described_class.url
url2 = described_class.url
url1 << 'foobar'
expect(url2).not_to end_with('foobar')
end
end
describe '._raw_config' do
subject { described_class._raw_config }
it 'should be frozen' do
expect(subject).to be_frozen
end
it 'returns false when the file does not exist' do
stub_const("#{described_class}::CONFIG_FILE", '/var/empty/doesnotexist')
expect(subject).to eq(false)
end
end
describe '.with' do
before { clear_pool }
after { clear_pool }
context 'when running not on sidekiq workers' do
before { allow(Sidekiq).to receive(:server?).and_return(false) }
it 'instantiates a connection pool with size 5' do
expect(ConnectionPool).to receive(:new).with(size: 5).and_call_original
described_class.with { |_redis| true }
end
end
context 'when running on sidekiq workers' do
before do
allow(Sidekiq).to receive(:server?).and_return(true)
allow(Sidekiq).to receive(:options).and_return({ concurrency: 18 })
end
it 'instantiates a connection pool with a size based on the concurrency of the worker' do
expect(ConnectionPool).to receive(:new).with(size: 18 + 5).and_call_original
described_class.with { |_redis| true }
end
end
end
describe '#sentinels' do
subject { described_class.new(Rails.env).sentinels }
context 'when sentinels are defined' do
let(:config) { Rails.root.join('spec/fixtures/config/redis_new_format_host.yml') }
it 'returns an array of hashes with host and port keys' do
stub_const("#{described_class}::CONFIG_FILE", config)
is_expected.to include(host: 'localhost', port: 26380)
is_expected.to include(host: 'slave2', port: 26381)
end
end
context 'when sentinels are not defined' do
let(:config) { Rails.root.join('spec/fixtures/config/redis_old_format_host.yml') }
it 'returns nil' do
stub_const("#{described_class}::CONFIG_FILE", config)
is_expected.to be_nil
end
end
end
describe '#sentinels?' do
subject { described_class.new(Rails.env).sentinels? }
context 'when sentinels are defined' do
let(:config) { Rails.root.join('spec/fixtures/config/redis_new_format_host.yml') }
it 'returns true' do
stub_const("#{described_class}::CONFIG_FILE", config)
is_expected.to be_truthy
end
end
context 'when sentinels are not defined' do
let(:config) { Rails.root.join('spec/fixtures/config/redis_old_format_host.yml') }
it 'returns false' do
stub_const("#{described_class}::CONFIG_FILE", config)
is_expected.to be_falsey
end
end
end
describe '#raw_config_hash' do
it 'returns default redis url when no config file is present' do
expect(subject).to receive(:fetch_config) { false }
expect(subject.send(:raw_config_hash)).to eq(url: Gitlab::Redis::DEFAULT_REDIS_URL)
end
it 'returns old-style single url config in a hash' do
expect(subject).to receive(:fetch_config) { 'redis://myredis:6379' }
expect(subject.send(:raw_config_hash)).to eq(url: 'redis://myredis:6379')
end
end
describe '#fetch_config' do
it 'returns false when no config file is present' do
allow(described_class).to receive(:_raw_config) { false }
expect(subject.send(:fetch_config)).to be_falsey
end
end
def clear_raw_config
described_class.remove_instance_variable(:@_raw_config)
rescue NameError
# raised if @_raw_config was not set; ignore
end
def clear_pool
described_class.remove_instance_variable(:@pool)
rescue NameError
# raised if @pool was not set; ignore
end
end