Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test harness should only force-unlock db connections for the current database #430

Merged
merged 1 commit into from
Oct 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions spec/support/reset_good_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
GoodJob::CurrentThread.reset
GoodJob.preserve_job_records = false

PgLock.advisory_lock.owns.all?(&: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"
PgLock.current_database.advisory_lock.owns.all?(&:unlock) if PgLock.advisory_lock.owns.count > 0
PgLock.current_database.advisory_lock.others.each(&:unlock!) if PgLock.advisory_lock.others.count > 0
expect(PgLock.current_database.advisory_lock.count).to eq(0), "Existing advisory locks BEFORE test run"
end

config.around do |example|
Expand Down Expand Up @@ -39,12 +39,13 @@
expect(GoodJob::Scheduler.instances).to all be_shutdown
GoodJob::Scheduler.instances.clear

expect(PgLock.owns.advisory_lock.count).to eq(0), "Existing owned advisory locks AFTER test run"
expect(PgLock.current_database.advisory_lock.owns.count).to eq(0), "Existing owned advisory locks AFTER test run"

if PgLock.others.advisory_lock.any?
puts "There are #{PgLock.others.advisory_lock.count} advisory locks still open."
other_locks = PgLock.current_database.advisory_lock.others
if other_locks.any?
puts "There are #{other_locks.count} advisory locks still open."
puts "\n\nAdvisory Locks:"
PgLock.others.advisory_lock.includes(:pg_stat_activity).each do |pg_lock|
other_locks.includes(:pg_stat_activity).each do |pg_lock|
puts " - #{pg_lock.pid}: #{pg_lock.pg_stat_activity.application_name}"
end

Expand All @@ -53,7 +54,8 @@
puts " - #{pg_stat_activity.pid}: #{pg_stat_activity.application_name}"
end
end
expect(PgLock.others.advisory_lock.count).to eq(0), "Existing others advisory locks AFTER test run"

expect(PgLock.current_database.advisory_lock.others.count).to eq(0), "Existing others advisory locks AFTER test run"
end
end

Expand Down Expand Up @@ -93,6 +95,7 @@ class PgLock < ActiveRecord::Base

belongs_to :pg_stat_activity, primary_key: :pid, foreign_key: :pid

scope :current_database, -> { joins("JOIN pg_database ON pg_database.oid = pg_locks.database").where("pg_database.datname = current_database()") }
scope :advisory_lock, -> { where(locktype: 'advisory') }
scope :owns, -> { where('pid = pg_backend_pid()') }
scope :others, -> { where('pid != pg_backend_pid()') }
Expand Down