1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix flaky advisory lock test.

Random failures due to active connection checking within the
assert_no_changes block.

Failure:
MigrationTest#test_with_advisory_lock_closes_connection [/rails/activerecord/test/cases/migration_test.rb:947]:
--- expected
+++ actual
@@ -1 +1 @@
-["SELECT 1", "SELECT 1"]
+["SELECT 1", "SELECT 1", "SELECT 1"]

This commit scopes the query down further to ensure that the advisory
unlock query is not left in pg_stat_activity which is an indication that
the connection pool was not disconnected.
This commit is contained in:
Guo Xiang Tan 2020-10-12 09:12:38 +08:00
parent 5699122abf
commit d2215580d8
No known key found for this signature in database
GPG key ID: FBD110179AAC1F20

View file

@ -942,7 +942,15 @@ class MigrationTest < ActiveRecord::TestCase
}.new }.new
migrator = ActiveRecord::Migrator.new(:up, [migration], @schema_migration, 100) migrator = ActiveRecord::Migrator.new(:up, [migration], @schema_migration, 100)
query = "SELECT query FROM pg_stat_activity WHERE datname = '#{ActiveRecord::Base.connection_db_config.database}' AND state = 'idle'" lock_id = migrator.send(:generate_migrator_advisory_lock_id)
query = <<~SQL
SELECT query
FROM pg_stat_activity
WHERE datname = '#{ActiveRecord::Base.connection_db_config.database}'
AND state = 'idle'
AND query LIKE '%#{lock_id}%'
SQL
assert_no_changes -> { ActiveRecord::Base.connection.exec_query(query).rows.flatten } do assert_no_changes -> { ActiveRecord::Base.connection.exec_query(query).rows.flatten } do
migrator.migrate migrator.migrate