Skip to content

Commit

Permalink
Capture and raise PG notices during tests that are normally logged to…
Browse files Browse the repository at this point in the history
… stderr
  • Loading branch information
bensheldon committed Jun 24, 2021
1 parent 7294bd1 commit bf67c00
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion spec/support/pg_locks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@ def initialize_type_map(map = type_map)
end
end

PostgresNoticeError = Class.new(StandardError)

ActiveSupport.on_load :active_record do
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend PostgresXidExtension

ActiveRecord::ConnectionAdapters::AbstractAdapter.set_callback :checkout, :before, lambda { |conn|
raw_connection = conn.raw_connection
next unless raw_connection.class.name == "PG::Connection"

raw_connection.set_notice_receiver do |result|
raise PostgresNoticeError, result.error_message
end
}
end

class PgLock < ActiveRecord::Base
Expand All @@ -23,11 +34,19 @@ def unlock
SQL
self.class.unscoped.exists?([where_sql, classid, objid])
end

def unlock!
where_sql = <<~SQL.squish
pg_terminate_backend(?)
SQL
self.class.unscoped.exists?([where_sql, pid])
end
end

RSpec.configure do |config|
config.before do
PgLock.advisory_lock.each(&:unlock) if PgLock.advisory_lock.count > 0
PgLock.advisory_lock.owns.each(&:unlock) if PgLock.advisory_lock.owns.count > 0
PgLock.advisory_lock.others.each(&:unlock!) if PgLock.advisory_lock.others.count > 0
expect(PgLock.advisory_lock.count).to eq(0), "Existing advisory locks BEFORE test run"
end

Expand Down

0 comments on commit bf67c00

Please sign in to comment.