discourse/spec/components/discourse_redis_spec.rb

224 lines
6.3 KiB
Ruby
Raw Normal View History

require 'rails_helper'
describe DiscourseRedis do
let(:slave_host) { 'testhost' }
let(:slave_port) { 1234 }
let(:config) do
2017-07-27 20:20:09 -05:00
DiscourseRedis.config.dup.merge(slave_host: 'testhost', slave_port: 1234, connector: DiscourseRedis::Connector)
end
let(:fallback_handler) { DiscourseRedis::FallbackHandler.instance }
it "ignore_readonly returns nil from a pure exception" do
result = DiscourseRedis.ignore_readonly { raise Redis::CommandError.new("READONLY") }
expect(result).to eq(nil)
end
describe 'redis commands' do
let(:raw_redis) { Redis.new(DiscourseRedis.config) }
before do
raw_redis.flushall
end
after do
raw_redis.flushall
end
describe 'when namespace is enabled' do
let(:redis) { DiscourseRedis.new }
it 'should append namespace to the keys' do
2017-08-23 20:00:15 -05:00
raw_redis.set('default:key', 1)
raw_redis.set('test:key2', 1)
2017-08-23 20:00:15 -05:00
expect(redis.keys).to include('key')
expect(redis.keys).to_not include('key2')
expect(redis.scan_each.to_a).to eq(['key'])
redis.scan_each.each do |key|
expect(key).to eq('key')
end
redis.del('key')
expect(raw_redis.get('default:key')).to eq(nil)
expect(redis.scan_each.to_a).to eq([])
raw_redis.set('default:key1', '1')
raw_redis.set('default:key2', '2')
expect(redis.mget('key1', 'key2')).to eq(['1', '2'])
expect(redis.scan_each.to_a).to contain_exactly('key1', 'key2')
end
end
describe 'when namespace is disabled' do
let(:redis) { DiscourseRedis.new(nil, namespace: false) }
it 'should not append any namespace to the keys' do
2017-08-23 20:00:15 -05:00
raw_redis.set('default:key', 1)
raw_redis.set('test:key2', 1)
2017-08-23 20:00:15 -05:00
expect(redis.keys).to include('default:key', 'test:key2')
redis.del('key')
expect(raw_redis.get('key')).to eq(nil)
raw_redis.set('key1', '1')
raw_redis.set('key2', '2')
expect(redis.mget('key1', 'key2')).to eq(['1', '2'])
end
it 'should noop a readonly redis' do
expect(Discourse.recently_readonly?).to eq(false)
redis.without_namespace
.expects(:set)
.raises(Redis::CommandError.new("READONLY"))
redis.set('key', 1)
expect(Discourse.recently_readonly?).to eq(true)
end
end
end
context '.slave_host' do
it 'should return the right config' do
slave_config = DiscourseRedis.slave_config(config)
expect(slave_config[:host]).to eq(slave_host)
expect(slave_config[:port]).to eq(slave_port)
end
end
context 'when redis connection is to a slave redis server' do
it 'should check the status of the master server' do
begin
fallback_handler.master = false
$redis.without_namespace.expects(:set).raises(Redis::CommandError.new("READONLY"))
fallback_handler.expects(:verify_master).once
$redis.set('test', '1')
ensure
fallback_handler.master = true
2018-05-30 01:43:30 -05:00
$redis.del('test')
end
end
end
describe DiscourseRedis::Connector do
let(:connector) { DiscourseRedis::Connector.new(config) }
2018-05-30 01:43:30 -05:00
after do
fallback_handler.master = true
end
it 'should return the master config when master is up' do
expect(connector.resolve).to eq(config)
end
2017-08-18 14:10:37 -05:00
class BrokenRedis
def initialize(error)
@error = error
end
def call(*args)
raise @error
end
def disconnect
end
end
2017-08-22 15:54:29 -05:00
it 'should return the slave config when master is down' do
2018-05-30 01:43:30 -05:00
error = Redis::CannotConnectError
2017-08-22 15:54:29 -05:00
2018-05-30 01:43:30 -05:00
expect do
connector.resolve(BrokenRedis.new(error))
end.to raise_error(Redis::CannotConnectError)
2017-08-22 15:54:29 -05:00
2018-05-30 01:43:30 -05:00
config = connector.resolve
expect(config[:host]).to eq(slave_host)
expect(config[:port]).to eq(slave_port)
2017-08-22 15:54:29 -05:00
end
it "should return the slave config when master's hostname cannot be resolved" do
2018-05-30 01:43:30 -05:00
error = RuntimeError.new('Name or service not known')
2018-05-30 01:43:30 -05:00
expect do
connector.resolve(BrokenRedis.new(error))
end.to raise_error(error)
2018-05-30 01:43:30 -05:00
expect(fallback_handler.master).to eq(false)
2018-05-30 01:43:30 -05:00
config = connector.resolve
expect(config[:host]).to eq(slave_host)
expect(config[:port]).to eq(slave_port)
expect(fallback_handler.master).to eq(false)
end
it "should return the slave config when master is still loading data" do
2018-05-30 01:43:30 -05:00
Redis::Client.any_instance
.expects(:call)
.with([:info])
.returns("someconfig:haha\r\nloading:1")
2018-05-30 01:43:30 -05:00
config = connector.resolve
expect(config[:host]).to eq(slave_host)
expect(config[:port]).to eq(slave_port)
end
it "should raise the right error" do
error = RuntimeError.new('test error')
Redis::Client.any_instance.expects(:call).raises(error).twice
2.times { expect { connector.resolve }.to raise_error(error) }
end
end
describe DiscourseRedis::FallbackHandler do
2018-05-30 01:43:30 -05:00
before do
@original_keepalive_interval = MessageBus.keepalive_interval
end
after do
fallback_handler.master = true
2018-05-30 01:43:30 -05:00
MessageBus.keepalive_interval = @original_keepalive_interval
end
describe '#initiate_fallback_to_master' do
it 'should return the right value if the master server is still down' do
fallback_handler.master = false
Redis::Client.any_instance.expects(:call).with([:info]).returns("Some other stuff")
expect(fallback_handler.initiate_fallback_to_master).to eq(false)
expect(MessageBus.keepalive_interval).to eq(0)
end
it 'should fallback to the master server once it is up' do
fallback_handler.master = false
2018-05-30 01:43:30 -05:00
redis_connection = mock('test')
2017-10-02 00:16:01 -05:00
Redis::Client.expects(:new).with(DiscourseRedis.slave_config).returns(redis_connection)
redis_connection.expects(:call).with([:info]).returns(DiscourseRedis::FallbackHandler::MASTER_LINK_STATUS)
DiscourseRedis::FallbackHandler::CONNECTION_TYPES.each do |connection_type|
2017-10-02 00:16:01 -05:00
redis_connection.expects(:call).with([:client, [:kill, 'type', connection_type]])
end
2018-05-30 01:43:30 -05:00
redis_connection.expects(:disconnect)
expect(fallback_handler.initiate_fallback_to_master).to eq(true)
expect(fallback_handler.master).to eq(true)
expect(Discourse.recently_readonly?).to eq(false)
expect(MessageBus.keepalive_interval).to eq(-1)
end
end
end
end