mar*_*nza 5 ruby postgresql multithreading rspec ruby-on-rails
我正在尝试围绕并发性进行测试。最终目标是使用skips lockedPostgreSQL 中的ActiveRecord记录测试服务。
这在两个控制台中都很有效:
# in console 1
queue = MyFancyQueue.first
item_1 = queue.items.first
item_1.with_lock { sleep 30 } # locks item_1 for 30 seconds
# in console 2, while item_1 is locked
queue = MyFancyQueue.first
queue.items.first # => item_1
queue.items.lock('FOR UPDATE SKIP LOCKED').first # => item_2
Run Code Online (Sandbox Code Playgroud)
但是,使用 RSpec,我在 a 中运行的任何代码Thread.new都找不到任何记录,例如:
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
Run Code Online (Sandbox Code Playgroud)
会抛出一个错误,说queue.items.first找不到,当使用pry查看线程时,我看不到任何项目。
我如何使用 RSpec 有效地测试这样的东西?
| 归档时间: |
|
| 查看次数: |
1975 次 |
| 最近记录: |