运行下面列出的规范时出错。它不会等待线程完成并取消存根迁移方法,从而导致其中一个线程命中实际方法。
注意到它只发生在加载轨道的情况下,没有它们它可以正常工作或者只是更快地完成......
规格:
it "should not allow infinit recursion" do
runner.stub(total_records: 4)
runner.stub(:fetch_records_batch).and_return([:one, :two])
runner.should_receive(:migrate).at_most(100).times
expect { runner.run }.to raise_error(Exception, /Migration fall into recursion/)
end
it "should pass"
1.should eq 1
end
提取的一段代码:
class Runner
def migrate(record)
raise NotImplementedError
end
def run
while have_records?(records = fetch_records_batch)
threads = []
records.each do |record|
threads << Thread.new(record) do |thread_record|
begin
result = migrate(thread_record)
rescue RuntimeError => exception
register_event :record_migration_error, thread_record, exception
end
end
recursion_preventer
end
threads.each(&:join)
end
end
def recursion_preventer
@counter ||= 0
@counter += 1
raise Exception, "Migration fall into recursion. Check logs." if @counter > (total_records * 1.1).round + 10
end
end