I am trying to set up a test around concurrency. The ultimate goal is to verify that the service uses ActiveRecord skips lockedrecords in PostgreSQL .
This works great in two consoles:
queue = MyFancyQueue.first
item_1 = queue.items.first
item_1.with_lock { sleep 30 }
queue = MyFancyQueue.first
queue.items.first
queue.items.lock('FOR UPDATE SKIP LOCKED').first
However, with RSpec, any code that I run in Thread.newcannot find any record, for example:
let(:queue) { MyFancyQueue.create }
let!(:items) { create_list(:item, 2, queue: queue) }
context 'with first item locked' do
it 'returns the second item' do
Thread.new { queue.items.first.with_lock { sleep 30 } }
expect(queue.items.lock('FOR UPDATE SKIP LOCKED').first).to eq items.last
end
end
throws an error saying that queue.items.firstit cannot be found, and when used pryfor viewing in the stream, I do not see any elements.
How to effectively check something like this with RSpec?
source
share