summaryrefslogtreecommitdiff
path: root/test/lib/minitest/parallel_each.rb
diff options
context:
space:
mode:
authorhsbt <hsbt@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-05-17 06:26:51 +0000
committerhsbt <hsbt@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-05-17 06:26:51 +0000
commitf8c6a5dc02439bb99c8403602288c2937b9356cc (patch)
tree263e3a008f86de66e7d93f5aa4be8b708cc20020 /test/lib/minitest/parallel_each.rb
parentc466dcc05bd9b95717dba424be9535e2e75822bc (diff)
* test/runner.rb: remove dependency test-unit and minitest
from stdlib when running with test-all. [Feature #9711][ruby-core:61890] * test/testunit/*.rb: ditto. * test/lib: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45970 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/lib/minitest/parallel_each.rb')
-rw-r--r--test/lib/minitest/parallel_each.rb80
1 files changed, 80 insertions, 0 deletions
diff --git a/test/lib/minitest/parallel_each.rb b/test/lib/minitest/parallel_each.rb
new file mode 100644
index 0000000000..e1020b35a0
--- /dev/null
+++ b/test/lib/minitest/parallel_each.rb
@@ -0,0 +1,80 @@
+# 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.
+######################################################################
+
+##
+# 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).
+
+class ParallelEach
+ require 'thread'
+ include Enumerable
+
+ ##
+ # How many Threads to use for this parallel #each.
+
+ N = (ENV['N'] || 2).to_i
+
+ ##
+ # Create a new ParallelEach instance over +list+.
+
+ def initialize list
+ @queue = Queue.new # *sigh*... the Queue api sucks sooo much...
+
+ list.each { |i| @queue << i }
+ N.times { @queue << nil }
+ end
+
+ def grep pattern # :nodoc:
+ self.class.new super
+ end
+
+ 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.
+
+ 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
+
+ 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
+end