Class: Mutineer::WorkerPool

Inherits:
Object
  • Object
show all
Defined in:
lib/mutineer/worker_pool.rb

Overview

Fixed-size fork pool (KTD1/KTD2). run forks up to size children at once; each child runs the block on one work item, marshals its Result to a private pipe, and exits. The parent reaps any finished child with Process.wait2(-1), opening exactly one slot per reap, then refills. Results are returned in the SAME ORDER as items regardless of finish order, so verdicts are identical to a serial run (R4) and downstream output is stable.

The block is run inside the child via yield(*items[i]); whatever it returns (a Result) is the marshaled payload. Per-mutant timeout is handled one level down by Isolation (KTD2) — the pool adds no separate wall clock.

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ WorkerPool

Returns a new instance of WorkerPool.



17
18
19
# File 'lib/mutineer/worker_pool.rb', line 17

def initialize(size)
  @size = [size.to_i, 1].max
end

Instance Method Details

#run(items) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mutineer/worker_pool.rb', line 21

def run(items)
  results = Array.new(items.size)
  queue   = (0...items.size).to_a
  running = {} # pid => [index, read_io]

  until queue.empty? && running.empty?
    fill(items, queue, running) { |*args| yield(*args) }
    reap(results, running)
  end

  results
end