title: Rspec should wait for threads to finish
body: <p>There is an error running a spec listed below. It does not wait for threads to complete and unstub a migrate method resulting that one of threads hits a real method.<br>
  Noticed it only happens with rails loaded, without them it is working correctly or just finish faster...</p>
  
  <p>Spec:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Extracted piece of code:</p>
  
  <pre><code>class Runner
    def migrate(record)
      raise NotImplementedError
    end
  
    def run
       while have_records?(records = fetch_records_batch)
         threads = []
         records.each do |record|
           threads &lt;&lt; Thread.new(record) do |thread_record|
             begin
               result = migrate(thread_record)
             rescue RuntimeError =&gt; exception
               register_event :record_migration_error, thread_record, exception
             end
           end
           recursion_preventer
        end
        threads.each(&amp;:join)
      end
    end
  
    def recursion_preventer
      @counter ||= 0
      @counter += 1
      raise Exception, "Migration fall into recursion. Check logs." if @counter &gt; (total_records * 1.1).round + 10
    end
  end
  </code></pre>