DEV: Improve postgresql fallover and multisite tests.

This commit is contained in:
Guo Xiang Tan 2019-01-15 12:52:12 +08:00
parent 6c2333a780
commit ec58c33e9e
3 changed files with 34 additions and 34 deletions

View File

@ -68,6 +68,8 @@ class PostgreSQLFallbackHandler
RailsMultisite::ConnectionManagement.with_connection(key) do
begin
logger.warn "#{log_prefix}: Checking master server..."
is_connection_active = false
begin
connection = ActiveRecord::Base.postgresql_connection(config)
is_connection_active = connection.active?
@ -99,7 +101,7 @@ class PostgreSQLFallbackHandler
end
def clear_connections
ActiveRecord::Base.connection_pool.disconnect!
ActiveRecord::Base.clear_all_connections!
end
private

View File

@ -27,23 +27,20 @@ describe ActiveRecord::ConnectionHandling do
let(:postgresql_fallback_handler) { PostgreSQLFallbackHandler.instance }
before do
# TODO: tgxworld will rewrite it without stubs
skip("Skip causes our build to be unstable")
@threads = Thread.list
postgresql_fallback_handler.initialized = true
['default', multisite_db].each do |db|
postgresql_fallback_handler.master_up(db)
end
end
after do
(Thread.list - @threads).each(&:kill)
Sidekiq.unpause!
postgresql_fallback_handler.setup!
ActiveRecord::Base.unstub(:postgresql_connection)
(Thread.list - @threads).each(&:kill)
ActiveRecord::Base.connection_pool.disconnect!
ActiveRecord::Base.clear_all_connections!
ActiveRecord::Base.establish_connection
$redis.flushall
end
describe "#postgresql_fallback_connection" do
@ -55,32 +52,25 @@ describe ActiveRecord::ConnectionHandling do
end
context 'when master server is down' do
before do
@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
let(:replica_connection) { mock('replica_connection') }
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)
[config, multisite_config].each do |configuration|
ActiveRecord::Base.expects(:postgresql_connection).with(configuration).raises(PG::ConnectionBad)
ActiveRecord::Base.expects(:verify_replica).with(@replica_connection)
ActiveRecord::Base.expects(:postgresql_connection)
.with(configuration)
.raises(PG::ConnectionBad)
ActiveRecord::Base.expects(:verify_replica).with(replica_connection)
ActiveRecord::Base.expects(:postgresql_connection).with(
configuration.dup.merge(host: replica_host, port: replica_port)
).returns(@replica_connection)
).returns(replica_connection)
end
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 { ActiveRecord::Base.postgresql_fallback_connection(multisite_config) }
.to change { Discourse.readonly_mode? }.from(false).to(true)
expect do
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)
ensure
@ -135,11 +126,17 @@ describe ActiveRecord::ConnectionHandling do
context 'when both master and replica server is down' 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)
).raises(PG::ConnectionBad).once
)
.raises(PG::ConnectionBad)
.once
postgresql_fallback_handler.expects(:verify_master).twice

View File

@ -179,14 +179,15 @@ RSpec.configure do |config|
config.before(:each, type: :multisite) do
Rails.configuration.multisite = true
RailsMultisite::ConnectionManagement.config_filename =
"spec/fixtures/multisite/two_dbs.yml"
end
config.after(:each, type: :multisite) do
ActiveRecord::Base.clear_all_connections!
Rails.configuration.multisite = false
RailsMultisite::ConnectionManagement.clear_settings!
ActiveRecord::Base.clear_active_connections!
ActiveRecord::Base.establish_connection
end