2012-11-28 22:23:11 -05:00
|
|
|
# encoding: utf-8
|
|
|
|
######################################################################
|
|
|
|
# This file is imported from the minitest project.
|
|
|
|
# DO NOT make modifications in this repo. They _will_ be reverted!
|
|
|
|
# File a patch instead and assign it to Ryan Davis.
|
|
|
|
######################################################################
|
|
|
|
|
2013-05-02 00:48:43 -04:00
|
|
|
##
|
|
|
|
# Provides a parallel #each that lets you enumerate using N threads.
|
|
|
|
# Use environment variable N to customize. Defaults to 2. Enumerable,
|
|
|
|
# so all the goodies come along (tho not all are wrapped yet to
|
|
|
|
# return another ParallelEach instance).
|
|
|
|
|
2012-11-28 22:23:11 -05:00
|
|
|
class ParallelEach
|
|
|
|
require 'thread'
|
|
|
|
include Enumerable
|
|
|
|
|
2013-05-02 00:48:43 -04:00
|
|
|
##
|
|
|
|
# How many Threads to use for this parallel #each.
|
|
|
|
|
2012-11-28 22:23:11 -05:00
|
|
|
N = (ENV['N'] || 2).to_i
|
|
|
|
|
2013-05-02 00:48:43 -04:00
|
|
|
##
|
|
|
|
# Create a new ParallelEach instance over +list+.
|
|
|
|
|
2012-11-28 22:23:11 -05:00
|
|
|
def initialize list
|
|
|
|
@queue = Queue.new # *sigh*... the Queue api sucks sooo much...
|
|
|
|
|
|
|
|
list.each { |i| @queue << i }
|
|
|
|
N.times { @queue << nil }
|
|
|
|
end
|
|
|
|
|
2013-05-02 00:48:43 -04:00
|
|
|
def grep pattern # :nodoc:
|
2012-11-28 22:23:11 -05:00
|
|
|
self.class.new super
|
|
|
|
end
|
|
|
|
|
2013-05-02 00:48:43 -04:00
|
|
|
def select(&block) # :nodoc:
|
|
|
|
self.class.new super
|
|
|
|
end
|
|
|
|
|
|
|
|
alias find_all select # :nodoc:
|
|
|
|
|
|
|
|
##
|
|
|
|
# Starts N threads that yield each element to your block. Joins the
|
|
|
|
# threads at the end.
|
|
|
|
|
2012-11-28 22:23:11 -05:00
|
|
|
def each
|
|
|
|
threads = N.times.map {
|
|
|
|
Thread.new do
|
|
|
|
Thread.current.abort_on_exception = true
|
|
|
|
while job = @queue.pop
|
|
|
|
yield job
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
threads.map(&:join)
|
|
|
|
end
|
2013-05-02 00:48:43 -04:00
|
|
|
|
|
|
|
def count
|
|
|
|
[@queue.size - N, 0].max
|
|
|
|
end
|
|
|
|
|
|
|
|
alias_method :size, :count
|
|
|
|
end
|
|
|
|
|
|
|
|
class MiniTest::Unit
|
|
|
|
alias _old_run_suites _run_suites
|
|
|
|
|
|
|
|
##
|
|
|
|
# Runs all the +suites+ for a given +type+. Runs suites declaring
|
|
|
|
# a test_order of +:parallel+ in parallel, and everything else
|
|
|
|
# serial.
|
|
|
|
|
|
|
|
def _run_suites suites, type
|
|
|
|
parallel, serial = suites.partition { |s| s.test_order == :parallel }
|
|
|
|
|
|
|
|
ParallelEach.new(parallel).map { |suite| _run_suite suite, type } +
|
|
|
|
serial.map { |suite| _run_suite suite, type }
|
|
|
|
end
|
2012-11-28 22:23:11 -05:00
|
|
|
end
|