ruby-on-rails – 在RSpec中使用Thread.new测试并发性
发布时间:2020-12-17 02:09:22 所属栏目:百科 来源:网络整理
导读:我正在尝试围绕并发性进行测试.最终目标是使用ActiveRecord skips locked records in PostgreSQL测试该服务. 这适用于两个控制台: # in console 1queue = MyFancyQueue.firstitem_1 = queue.items.firstitem_1.with_lock { sleep 30 } # locks item_1 for 3
我正在尝试围绕并发性进行测试.最终目标是使用ActiveRecord
skips locked records in PostgreSQL测试该服务.
这适用于两个控制台: # 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 但是,使用RSpec,我在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 会抛出一个错误,说找不到queue.items.first,并且在使用pry查看线程时,我看不到任何项目. 如何使用RSpec有效地测试这样的东西? 解决方法
如果您使用DatabaseCleaner,请确保DatabaseCleaner.strategy =:截断此测试.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |