【发布时间】:2011-01-13 16:39:41
【问题描述】:
有没有办法使用 RSpec 测试并行事务?
假设我有一个银行账户余额需要在减少或增加之前锁定在交易中。
但是,目前,虽然我关闭了 RSpec transactional_fixtures 选项,但我无法在两个单独的线程中启动 2 个并行事务。由于某种原因,两者都被挂起。
给定帐户是一个模型
那么这个规范就会挂起:
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
有没有什么办法让它不挂起,同时还能展示交易能力?
【问题讨论】:
标签: transactions rspec2