Skip to content

Commit

Permalink
Improve tests for advisory locks
Browse files Browse the repository at this point in the history
Improve tests for advisory locks so that we actually check that the
advisory lock is held from another thread. Both ActiveRecord and Sequel
have a lot of laziness, so where an SQL command doesn't have a checked
result, it's easy to think you've invoked it, but only have a lazy data
set instead.
  • Loading branch information
brandur committed Apr 28, 2024
1 parent 9dd763b commit 79bab44
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
11 changes: 10 additions & 1 deletion drivers/riverqueue-activerecord/spec/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,16 @@ class SimpleArgsWithInsertOpts < SimpleArgs

describe "#advisory_lock" do
it "takes an advisory lock" do
driver.advisory_lock(123)
driver.transaction do
conn2 = ::ActiveRecord::Base.connection_pool.checkout
driver.advisory_lock(123)

Thread.new do
conn = ::ActiveRecord::Base.connection_pool.checkout
expect(conn.execute("SELECT pg_try_advisory_xact_lock(123)").first["pg_try_advisory_xact_lock"]).to be false
::ActiveRecord::Base.connection_pool.checkin(conn)
end.join
end
end
end

Expand Down
8 changes: 7 additions & 1 deletion drivers/riverqueue-sequel/spec/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ class SimpleArgsWithInsertOpts < SimpleArgs

describe "#advisory_lock" do
it "takes an advisory lock" do
driver.advisory_lock(123)
driver.transaction do
driver.advisory_lock(123)

Thread.new do
expect(DB.fetch("SELECT pg_try_advisory_xact_lock(?)", 123).first[:pg_try_advisory_xact_lock]).to be false
end.join
end
end
end

Expand Down

0 comments on commit 79bab44

Please sign in to comment.