A platform for community discussion. Free, open, simple.
DEV: Improve postgresql fallover and multisite tests.
Guo Xiang Tanec58c33e9e5f
diff --git a/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb b/lib/active_record/connection_adapters/postgresql_fallback_adapter.rbindex 6016b162c08..3d4f9b24a49 100644--- a/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb+++ b/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb@@ -68,6 +68,8 @@ class PostgreSQLFallbackHandlerRailsMultisite::ConnectionManagement.with_connection(key) dobeginlogger.warn "#{log_prefix}: Checking master server..."+ is_connection_active = false+beginconnection = ActiveRecord::Base.postgresql_connection(config)is_connection_active = connection.active?@@ -99,7 +101,7 @@ class PostgreSQLFallbackHandlerenddef clear_connections- ActiveRecord::Base.connection_pool.disconnect!+ ActiveRecord::Base.clear_all_connections!endprivatediff --git a/spec/components/active_record/connection_adapters/postgresql_fallback_adapter_spec.rb b/spec/components/active_record/connection_adapters/postgresql_fallback_adapter_spec.rbindex 98a35defd5d..56036099858 100644--- a/spec/components/active_record/connection_adapters/postgresql_fallback_adapter_spec.rb+++ b/spec/components/active_record/connection_adapters/postgresql_fallback_adapter_spec.rb@@ -27,23 +27,20 @@ describe ActiveRecord::ConnectionHandling dolet(:postgresql_fallback_handler) { PostgreSQLFallbackHandler.instance }before do- # TODO: tgxworld will rewrite it without stubs- skip("Skip causes our build to be unstable")@threads = Thread.listpostgresql_fallback_handler.initialized = true-- ['default', multisite_db].each do |db|- postgresql_fallback_handler.master_up(db)- endendafter 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.flushallenddescribe "#postgresql_fallback_connection" do@@ -55,32 +52,25 @@ describe ActiveRecord::ConnectionHandling doendcontext '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)endexpect(postgresql_fallback_handler.master_down?).to eq(nil)@@ -109,8 +99,9 @@ describe ActiveRecord::ConnectionHandling doexpect(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 docontext 'when both master and replica server is down' doit 'should raise the right error' do- ActiveRecord::Base.expects(:postgresql_connection).with(config).raises(PG::ConnectionBad)-- ActiveRecord::Base.expects(:postgresql_connection).with(- config.dup.merge(host: replica_host, port: replica_port)- ).raises(PG::ConnectionBad).once+ ActiveRecord::Base.expects(:postgresql_connection)+ .with(config)+ .raises(PG::ConnectionBad)+ .once++ ActiveRecord::Base.expects(:postgresql_connection)+ .with(+ config.dup.merge(host: replica_host, port: replica_port)+ )+ .raises(PG::ConnectionBad)+ .oncepostgresql_fallback_handler.expects(:verify_master).twicediff --git a/spec/rails_helper.rb b/spec/rails_helper.rbindex a0fad312fd6..0d58507961b 100644--- a/spec/rails_helper.rb+++ b/spec/rails_helper.rb@@ -179,14 +179,15 @@ RSpec.configure do |config|config.before(:each, type: :multisite) doRails.configuration.multisite = true+RailsMultisite::ConnectionManagement.config_filename ="spec/fixtures/multisite/two_dbs.yml"endconfig.after(:each, type: :multisite) do+ ActiveRecord::Base.clear_all_connections!Rails.configuration.multisite = falseRailsMultisite::ConnectionManagement.clear_settings!- ActiveRecord::Base.clear_active_connections!ActiveRecord::Base.establish_connectionend