mirror of
https://github.com/discourse/discourse.git
synced 2025-06-16 14:51:26 +08:00
DEV: Improve postgresql fallover and multisite tests.
This commit is contained in:
@ -68,6 +68,8 @@ class PostgreSQLFallbackHandler
|
|||||||
RailsMultisite::ConnectionManagement.with_connection(key) do
|
RailsMultisite::ConnectionManagement.with_connection(key) do
|
||||||
begin
|
begin
|
||||||
logger.warn "#{log_prefix}: Checking master server..."
|
logger.warn "#{log_prefix}: Checking master server..."
|
||||||
|
is_connection_active = false
|
||||||
|
|
||||||
begin
|
begin
|
||||||
connection = ActiveRecord::Base.postgresql_connection(config)
|
connection = ActiveRecord::Base.postgresql_connection(config)
|
||||||
is_connection_active = connection.active?
|
is_connection_active = connection.active?
|
||||||
@ -99,7 +101,7 @@ class PostgreSQLFallbackHandler
|
|||||||
end
|
end
|
||||||
|
|
||||||
def clear_connections
|
def clear_connections
|
||||||
ActiveRecord::Base.connection_pool.disconnect!
|
ActiveRecord::Base.clear_all_connections!
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -27,23 +27,20 @@ describe ActiveRecord::ConnectionHandling do
|
|||||||
let(:postgresql_fallback_handler) { PostgreSQLFallbackHandler.instance }
|
let(:postgresql_fallback_handler) { PostgreSQLFallbackHandler.instance }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
# TODO: tgxworld will rewrite it without stubs
|
|
||||||
skip("Skip causes our build to be unstable")
|
|
||||||
@threads = Thread.list
|
@threads = Thread.list
|
||||||
postgresql_fallback_handler.initialized = true
|
postgresql_fallback_handler.initialized = true
|
||||||
|
|
||||||
['default', multisite_db].each do |db|
|
|
||||||
postgresql_fallback_handler.master_up(db)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
after do
|
after do
|
||||||
|
(Thread.list - @threads).each(&:kill)
|
||||||
Sidekiq.unpause!
|
Sidekiq.unpause!
|
||||||
postgresql_fallback_handler.setup!
|
postgresql_fallback_handler.setup!
|
||||||
|
|
||||||
ActiveRecord::Base.unstub(:postgresql_connection)
|
ActiveRecord::Base.unstub(:postgresql_connection)
|
||||||
(Thread.list - @threads).each(&:kill)
|
ActiveRecord::Base.clear_all_connections!
|
||||||
ActiveRecord::Base.connection_pool.disconnect!
|
|
||||||
ActiveRecord::Base.establish_connection
|
ActiveRecord::Base.establish_connection
|
||||||
|
|
||||||
|
$redis.flushall
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#postgresql_fallback_connection" do
|
describe "#postgresql_fallback_connection" do
|
||||||
@ -55,32 +52,25 @@ describe ActiveRecord::ConnectionHandling do
|
|||||||
end
|
end
|
||||||
|
|
||||||
context 'when master server is down' do
|
context 'when master server is down' do
|
||||||
before do
|
let(:replica_connection) { mock('replica_connection') }
|
||||||
@replica_connection = mock('replica_connection')
|
|
||||||
end
|
|
||||||
|
|
||||||
after do
|
|
||||||
pg_readonly_mode_key = Discourse::PG_READONLY_MODE_KEY
|
|
||||||
|
|
||||||
with_multisite_db(multisite_db) do
|
|
||||||
Discourse.disable_readonly_mode(pg_readonly_mode_key)
|
|
||||||
end
|
|
||||||
|
|
||||||
Discourse.disable_readonly_mode(pg_readonly_mode_key)
|
|
||||||
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[Rails.env])
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should failover to a replica server' do
|
it 'should failover to a replica server' do
|
||||||
RailsMultisite::ConnectionManagement.stubs(:all_dbs).returns(['default', multisite_db])
|
RailsMultisite::ConnectionManagement
|
||||||
|
.stubs(:all_dbs)
|
||||||
|
.returns(['default', multisite_db])
|
||||||
|
|
||||||
postgresql_fallback_handler.expects(:verify_master).at_least(3)
|
postgresql_fallback_handler.expects(:verify_master).at_least(3)
|
||||||
|
|
||||||
[config, multisite_config].each do |configuration|
|
[config, multisite_config].each do |configuration|
|
||||||
ActiveRecord::Base.expects(:postgresql_connection).with(configuration).raises(PG::ConnectionBad)
|
ActiveRecord::Base.expects(:postgresql_connection)
|
||||||
ActiveRecord::Base.expects(:verify_replica).with(@replica_connection)
|
.with(configuration)
|
||||||
|
.raises(PG::ConnectionBad)
|
||||||
|
|
||||||
|
ActiveRecord::Base.expects(:verify_replica).with(replica_connection)
|
||||||
|
|
||||||
ActiveRecord::Base.expects(:postgresql_connection).with(
|
ActiveRecord::Base.expects(:postgresql_connection).with(
|
||||||
configuration.dup.merge(host: replica_host, port: replica_port)
|
configuration.dup.merge(host: replica_host, port: replica_port)
|
||||||
).returns(@replica_connection)
|
).returns(replica_connection)
|
||||||
end
|
end
|
||||||
|
|
||||||
expect(postgresql_fallback_handler.master_down?).to eq(nil)
|
expect(postgresql_fallback_handler.master_down?).to eq(nil)
|
||||||
@ -109,8 +99,9 @@ describe ActiveRecord::ConnectionHandling do
|
|||||||
|
|
||||||
expect(message.data[:db]).to eq(multisite_db)
|
expect(message.data[:db]).to eq(multisite_db)
|
||||||
|
|
||||||
expect { ActiveRecord::Base.postgresql_fallback_connection(multisite_config) }
|
expect do
|
||||||
.to change { Discourse.readonly_mode? }.from(false).to(true)
|
ActiveRecord::Base.postgresql_fallback_connection(multisite_config)
|
||||||
|
end.to change { Discourse.readonly_mode? }.from(false).to(true)
|
||||||
|
|
||||||
expect(postgresql_fallback_handler.master_down?).to eq(true)
|
expect(postgresql_fallback_handler.master_down?).to eq(true)
|
||||||
ensure
|
ensure
|
||||||
@ -135,11 +126,17 @@ describe ActiveRecord::ConnectionHandling do
|
|||||||
|
|
||||||
context 'when both master and replica server is down' do
|
context 'when both master and replica server is down' do
|
||||||
it 'should raise the right error' do
|
it 'should raise the right error' do
|
||||||
ActiveRecord::Base.expects(:postgresql_connection).with(config).raises(PG::ConnectionBad)
|
ActiveRecord::Base.expects(:postgresql_connection)
|
||||||
|
.with(config)
|
||||||
|
.raises(PG::ConnectionBad)
|
||||||
|
.once
|
||||||
|
|
||||||
ActiveRecord::Base.expects(:postgresql_connection).with(
|
ActiveRecord::Base.expects(:postgresql_connection)
|
||||||
|
.with(
|
||||||
config.dup.merge(host: replica_host, port: replica_port)
|
config.dup.merge(host: replica_host, port: replica_port)
|
||||||
).raises(PG::ConnectionBad).once
|
)
|
||||||
|
.raises(PG::ConnectionBad)
|
||||||
|
.once
|
||||||
|
|
||||||
postgresql_fallback_handler.expects(:verify_master).twice
|
postgresql_fallback_handler.expects(:verify_master).twice
|
||||||
|
|
||||||
|
@ -179,14 +179,15 @@ RSpec.configure do |config|
|
|||||||
|
|
||||||
config.before(:each, type: :multisite) do
|
config.before(:each, type: :multisite) do
|
||||||
Rails.configuration.multisite = true
|
Rails.configuration.multisite = true
|
||||||
|
|
||||||
RailsMultisite::ConnectionManagement.config_filename =
|
RailsMultisite::ConnectionManagement.config_filename =
|
||||||
"spec/fixtures/multisite/two_dbs.yml"
|
"spec/fixtures/multisite/two_dbs.yml"
|
||||||
end
|
end
|
||||||
|
|
||||||
config.after(:each, type: :multisite) do
|
config.after(:each, type: :multisite) do
|
||||||
|
ActiveRecord::Base.clear_all_connections!
|
||||||
Rails.configuration.multisite = false
|
Rails.configuration.multisite = false
|
||||||
RailsMultisite::ConnectionManagement.clear_settings!
|
RailsMultisite::ConnectionManagement.clear_settings!
|
||||||
ActiveRecord::Base.clear_active_connections!
|
|
||||||
ActiveRecord::Base.establish_connection
|
ActiveRecord::Base.establish_connection
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user