Class: Minitest::ParallelEach

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/minitest/parallel_each.rb

Overview

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).

Constant Summary collapse

N =

How many Threads to use for this parallel #each.

(ENV['N'] || 2).to_i

Instance Method Summary collapse

Constructor Details

#initialize(list) ⇒ ParallelEach

Create a new ParallelEach instance over list.



19
20
21
22
23
24
# File 'lib/minitest/parallel_each.rb', line 19

def initialize list
  @queue = Queue.new # *sigh*... the Queue api sucks sooo much...

  list.each { |i| @queue << i }
  N.times { @queue << nil }
end

Instance Method Details

#countObject Also known as: size

:nodoc:



48
49
50
# File 'lib/minitest/parallel_each.rb', line 48

def count # :nodoc:
  [@queue.size - N, 0].max
end

#eachObject

Starts N threads that yield each element to your block. Joins the threads at the end.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/minitest/parallel_each.rb', line 36

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

#select(&block) ⇒ Object Also known as: find_all

:nodoc:



26
27
28
# File 'lib/minitest/parallel_each.rb', line 26

def select(&block) # :nodoc:
  self.class.new super
end