From df3847acb90fee5102569dce69a146d2cce5c7ae Mon Sep 17 00:00:00 2001 From: Brandur Date: Sun, 28 Apr 2024 08:03:59 -0700 Subject: [PATCH] Improve tests for advisory locks 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. --- drivers/riverqueue-activerecord/spec/driver_spec.rb | 10 +++++++++- drivers/riverqueue-sequel/spec/driver_spec.rb | 8 +++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/drivers/riverqueue-activerecord/spec/driver_spec.rb b/drivers/riverqueue-activerecord/spec/driver_spec.rb index 95fc03e..9db6729 100644 --- a/drivers/riverqueue-activerecord/spec/driver_spec.rb +++ b/drivers/riverqueue-activerecord/spec/driver_spec.rb @@ -67,7 +67,15 @@ 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 + 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 diff --git a/drivers/riverqueue-sequel/spec/driver_spec.rb b/drivers/riverqueue-sequel/spec/driver_spec.rb index ef2abc1..6264c21 100644 --- a/drivers/riverqueue-sequel/spec/driver_spec.rb +++ b/drivers/riverqueue-sequel/spec/driver_spec.rb @@ -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