FIX: Don't configure Redis connector if Redis slave config is not set.

This commit is contained in:
Guo Xiang Tan 2017-02-02 13:47:57 +08:00
parent c34520d384
commit 3c28d94706
2 changed files with 35 additions and 5 deletions

View File

@ -37,29 +37,39 @@ class GlobalSetting
{"production" => hash}
end
# For testing purposes
def self.reset_redis_config!
@config = nil
end
def self.redis_config
@config ||=
begin
c = {}
c[:host] = redis_host if redis_host
c[:port] = redis_port if redis_port
c[:slave_host] = redis_slave_host if redis_slave_host
c[:slave_port] = redis_slave_port if redis_slave_port
if redis_slave_host && redis_slave_port
c[:slave_host] = redis_slave_host
c[:slave_port] = redis_slave_port
c[:connector] = DiscourseRedis::Connector
end
c[:password] = redis_password if redis_password.present?
c[:db] = redis_db if redis_db != 0
c[:db] = 1 if Rails.env == "test"
if redis_sentinels.present?
c[:sentinels] = redis_sentinels.split(",").map do |address|
host,port = address.split(":")
{host: host, port: port}
end.to_a
end
c[:connector] = DiscourseRedis::Connector
c.freeze
end
end
class BaseProvider
def self.coerce(setting)
return setting == "true" if setting == "true" || setting == "false"

View File

@ -1,6 +1,26 @@
require 'rails_helper'
require 'tempfile'
describe GlobalSetting do
describe '.redis_config' do
describe 'when slave config is not present' do
it "should not set any connector" do
expect(GlobalSetting.redis_config[:connector]).to eq(nil)
end
end
describe 'when slave config is present' do
it "should set the right connector" do
GlobalSetting.reset_redis_config!
GlobalSetting.expects(:redis_slave_port).returns(6379).at_least_once
GlobalSetting.expects(:redis_slave_host).returns('0.0.0.0').at_least_once
expect(GlobalSetting.redis_config[:connector]).to eq(DiscourseRedis::Connector)
end
end
end
end
describe GlobalSetting::EnvProvider do
it "can detect keys from env" do
ENV['DISCOURSE_BLA'] = '1'
@ -9,6 +29,7 @@ describe GlobalSetting::EnvProvider do
expect(GlobalSetting::EnvProvider.new.keys).to include(:bla_2)
end
end
describe GlobalSetting::FileProvider do
it "can parse a simple file" do
f = Tempfile.new('foo')
@ -48,5 +69,4 @@ describe GlobalSetting::FileProvider do
f.unlink
end
end