Skip to content

Commit 503cd95

Browse files
authored
Improve tests for advisory locks (#12)
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.
1 parent 9dd763b commit 503cd95

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

drivers/riverqueue-activerecord/spec/driver_spec.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,18 @@ class SimpleArgsWithInsertOpts < SimpleArgs
6767

6868
describe "#advisory_lock" do
6969
it "takes an advisory lock" do
70-
driver.advisory_lock(123)
70+
driver.transaction do
71+
driver.advisory_lock(123)
72+
73+
Thread.new do
74+
conn = ::ActiveRecord::Base.connection_pool.checkout
75+
begin
76+
expect(conn.execute("SELECT pg_try_advisory_xact_lock(123)").first["pg_try_advisory_xact_lock"]).to be false
77+
ensure
78+
::ActiveRecord::Base.connection_pool.checkin(conn)
79+
end
80+
end.join
81+
end
7182
end
7283
end
7384

drivers/riverqueue-sequel/spec/driver_spec.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ class SimpleArgsWithInsertOpts < SimpleArgs
6767

6868
describe "#advisory_lock" do
6969
it "takes an advisory lock" do
70-
driver.advisory_lock(123)
70+
driver.transaction do
71+
driver.advisory_lock(123)
72+
73+
Thread.new do
74+
expect(DB.fetch("SELECT pg_try_advisory_xact_lock(?)", 123).first[:pg_try_advisory_xact_lock]).to be false
75+
end.join
76+
end
7177
end
7278
end
7379

0 commit comments

Comments
 (0)