2017-09-10 17:25:29 +05:30
|
|
|
RSpec.shared_examples "redis_shared_examples" do
|
2017-08-17 22:00:37 +05:30
|
|
|
include StubENV
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
let(:test_redis_url) { "redis://redishost:#{redis_port}"}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
before do
|
2017-09-10 17:25:29 +05:30
|
|
|
stub_env(environment_config_file_name, Rails.root.join(config_file_name))
|
2017-08-17 22:00:37 +05:30
|
|
|
clear_raw_config
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
after do
|
2017-08-17 22:00:37 +05:30
|
|
|
clear_raw_config
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
describe '.params' do
|
|
|
|
subject { described_class.params }
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
it 'withstands mutation' do
|
|
|
|
params1 = described_class.params
|
|
|
|
params2 = described_class.params
|
|
|
|
params1[:foo] = :bar
|
|
|
|
|
|
|
|
expect(params2).not_to have_key(:foo)
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
context 'when url contains unix socket reference' do
|
|
|
|
context 'with old format' do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:config_file_name) { config_old_format_socket }
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'returns path key instead' do
|
2017-09-10 17:25:29 +05:30
|
|
|
is_expected.to include(path: old_socket_path)
|
2016-09-13 17:45:13 +05:30
|
|
|
is_expected.not_to have_key(:url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with new format' do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:config_file_name) { config_new_format_socket }
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'returns path key instead' do
|
2017-09-10 17:25:29 +05:30
|
|
|
is_expected.to include(path: new_socket_path)
|
2016-09-13 17:45:13 +05:30
|
|
|
is_expected.not_to have_key(:url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when url is host based' do
|
|
|
|
context 'with old format' do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:config_file_name) { config_old_format_host }
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'returns hash with host, port, db, and password' do
|
2017-09-10 17:25:29 +05:30
|
|
|
is_expected.to include(host: 'localhost', password: 'mypassword', port: redis_port, db: redis_database)
|
2016-09-13 17:45:13 +05:30
|
|
|
is_expected.not_to have_key(:url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with new format' do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:config_file_name) { config_new_format_host }
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'returns hash with host, port, db, and password' do
|
2017-09-10 17:25:29 +05:30
|
|
|
is_expected.to include(host: 'localhost', password: 'mynewpassword', port: redis_port, db: redis_database)
|
2016-09-13 17:45:13 +05:30
|
|
|
is_expected.not_to have_key(:url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
describe '.url' do
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'withstands mutation' do
|
|
|
|
url1 = described_class.url
|
|
|
|
url2 = described_class.url
|
|
|
|
url1 << 'foobar' unless url1.frozen?
|
|
|
|
|
|
|
|
expect(url2).not_to end_with('foobar')
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when yml file with env variable' do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:config_file_name) { config_with_environment_variable_inside }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
before do
|
2017-09-10 17:25:29 +05:30
|
|
|
stub_env(config_env_variable_url, test_redis_url)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'reads redis url from env variable' do
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(described_class.url).to eq test_redis_url
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '._raw_config' do
|
|
|
|
subject { described_class._raw_config }
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:config_file_name) { '/var/empty/doesnotexist' }
|
2016-11-03 12:29:30 +05:30
|
|
|
|
|
|
|
it 'should be frozen' do
|
|
|
|
expect(subject).to be_frozen
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when the file does not exist' do
|
|
|
|
expect(subject).to eq(false)
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
it "returns false when the filename can't be determined" do
|
|
|
|
expect(described_class).to receive(:config_file_name).and_return(nil)
|
|
|
|
|
|
|
|
expect(subject).to eq(false)
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '.with' do
|
2017-09-10 17:25:29 +05:30
|
|
|
before do
|
|
|
|
clear_pool
|
|
|
|
end
|
|
|
|
after do
|
|
|
|
clear_pool
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
|
|
|
context 'when running not on sidekiq workers' do
|
2017-09-10 17:25:29 +05:30
|
|
|
before do
|
|
|
|
allow(Sidekiq).to receive(:server?).and_return(false)
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
|
|
|
it 'instantiates a connection pool with size 5' do
|
|
|
|
expect(ConnectionPool).to receive(:new).with(size: 5).and_call_original
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
described_class.with { |_redis_shared_example| true }
|
2016-11-03 12:29:30 +05:30
|
|
|
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
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
described_class.with { |_redis_shared_example| true }
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe '#sentinels' do
|
|
|
|
subject { described_class.new(Rails.env).sentinels }
|
|
|
|
|
|
|
|
context 'when sentinels are defined' do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:config_file_name) { config_new_format_host }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
it 'returns an array of hashes with host and port keys' do
|
2017-09-10 17:25:29 +05:30
|
|
|
is_expected.to include(host: 'localhost', port: sentinel_port)
|
|
|
|
is_expected.to include(host: 'slave2', port: sentinel_port)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when sentinels are not defined' do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:config_file_name) { config_old_format_host }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
it 'returns nil' do
|
|
|
|
is_expected.to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#sentinels?' do
|
|
|
|
subject { described_class.new(Rails.env).sentinels? }
|
|
|
|
|
|
|
|
context 'when sentinels are defined' do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:config_file_name) { config_new_format_host }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
it 'returns true' do
|
|
|
|
is_expected.to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when sentinels are not defined' do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:config_file_name) { config_old_format_host }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
is_expected.to be_falsey
|
|
|
|
end
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
describe '#raw_config_hash' do
|
|
|
|
it 'returns default redis url when no config file is present' do
|
|
|
|
expect(subject).to receive(:fetch_config) { false }
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(subject.send(:raw_config_hash)).to eq(url: class_redis_url )
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns old-style single url config in a hash' do
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(subject).to receive(:fetch_config) { test_redis_url }
|
|
|
|
expect(subject.send(:raw_config_hash)).to eq(url: test_redis_url)
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#fetch_config' do
|
|
|
|
it 'returns false when no config file is present' do
|
2016-11-03 12:29:30 +05:30
|
|
|
allow(described_class).to receive(:_raw_config) { false }
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(subject.send(:fetch_config)).to eq false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false when config file is present but has invalid YAML' do
|
|
|
|
allow(described_class).to receive(:_raw_config) { "# development: true" }
|
|
|
|
|
|
|
|
expect(subject.send(:fetch_config)).to eq false
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
|
|
|
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
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|