Skip to content

Commit

Permalink
Test to ensure lock is released after failed query
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrage committed Jul 10, 2024
1 parent 9441f7c commit 0660b33
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion spec/safe_statements_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4167,7 +4167,7 @@ def up
)
end

it "releases the lock (even after an exception)" do
it "releases the lock even after an exception" do
begin
migration.safely_acquire_lock_for_table(table_name) do
raise "bogus error"
Expand All @@ -4178,6 +4178,32 @@ def up
expect(locks_for_table(table_name, connection: alternate_connection)).to be_empty
end

it "releases the lock even after a swallowed postgres exception" do
migration.safely_acquire_lock_for_table(table_name) do
expect(locks_for_table(table_name, connection: alternate_connection)).to contain_exactly(
having_attributes(
table: "bogus_table",
lock_type: "AccessExclusiveLock",
granted: true,
pid: kind_of(Integer),
),
)

begin
migration.connection.execute("SELECT * FROM garbage")
rescue
end

expect(locks_for_table(table_name, connection: alternate_connection)).to be_empty

expect do
migration.connection.execute("SELECT * FROM bogus_table")
end.to raise_error(ActiveRecord::StatementInvalid, /PG::InFailedSqlTransaction/)
end

expect(locks_for_table(table_name, connection: alternate_connection)).to be_empty
end

it "waits to acquire a lock if the table is already blocked" do
block_call_count = 0
expect(PgHaMigrations::BlockingDatabaseTransactions).to receive(:find_blocking_transactions).exactly(3).times do |*args|
Expand Down

0 comments on commit 0660b33

Please sign in to comment.