有没有办法使用 RSpec 测试并行事务?假设我有一个银行账户余额需要在减少或增加之前锁定在交易中。但是,目前,虽然我关闭了 RSpectransactional_fixtures
选项,但我无法在两个单独的线程中启动 2 个并行事务。由于某种原因,两者都被挂起。给定 Account 是一个模型然后这个规范将挂起:
it "should ensure operations are performed correctly" do
@account = Account.create(:balance => 0)
threads = []
(0..1).each do |index|
threads << Thread.new do
Account.transaction do
account = Account.find(@account.id, :lock => true)
account.balance += 100
sleep 0.5
account.save!
end
end
end
threads.each{|t|t.join}
@account.reload.balance.should == 200
end
有没有办法让它不挂起,同时还能展示交易能力?