Module: Minitest

Defined in:
lib/minitest/parallel_fork.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.parallel_fork_stat_reporterObject (readonly)

Returns the value of attribute parallel_fork_stat_reporter.



29
30
31
# File 'lib/minitest/parallel_fork.rb', line 29

def parallel_fork_stat_reporter
  @parallel_fork_stat_reporter
end

Class Method Details

.__run(reporter, options) ⇒ Object

Override __run to use a child forks to run the speeds, which allows for parallel spec execution on MRI.



128
# File 'lib/minitest/parallel_fork.rb', line 128

alias __run __run

.after_parallel_fork(i = nil, &block) ⇒ Object

Set the after_parallel_fork block to the given block



20
21
22
# File 'lib/minitest/parallel_fork.rb', line 20

def after_parallel_fork(i=nil, &block)
  @after_parallel_fork = block
end

.before_parallel_fork(&block) ⇒ Object

Set the before_parallel_fork block to the given block



15
16
17
# File 'lib/minitest/parallel_fork.rb', line 15

def before_parallel_fork(&block)
  @before_parallel_fork = block
end

.on_parallel_fork_marshal_failure(&block) ⇒ Object

Set the on_parallel_fork_marshal_failure block to the given block



25
26
27
# File 'lib/minitest/parallel_fork.rb', line 25

def on_parallel_fork_marshal_failure(&block)
  @on_parallel_fork_marshal_failure = block
end

.parallel_fork_child_data(data) ⇒ Object



103
104
105
# File 'lib/minitest/parallel_fork.rb', line 103

def parallel_fork_child_data(data)
  data.map{|_pid, read| Thread.new(read, &:read)}.map(&:value).map{|data| parallel_fork_data_from_marshal(data)}
end

.parallel_fork_data_from_marshal(data) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/minitest/parallel_fork.rb', line 57

def parallel_fork_data_from_marshal(data)
  Marshal.load(data)
rescue ArgumentError
  if @on_parallel_fork_marshal_failure
    @on_parallel_fork_marshal_failure.call
  end
  raise
end

.parallel_fork_data_to_marshalObject



53
54
55
# File 'lib/minitest/parallel_fork.rb', line 53

def parallel_fork_data_to_marshal
  %i'count assertions results'.map{|meth| parallel_fork_stat_reporter.send(meth)}
end

.parallel_fork_numberObject



119
120
121
# File 'lib/minitest/parallel_fork.rb', line 119

def parallel_fork_number
  (ENV['NCPU'] || 4).to_i
end

.parallel_fork_run_test_suite(suite, reporter, options) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/minitest/parallel_fork.rb', line 72

def parallel_fork_run_test_suite(suite, reporter, options)
  if suite.is_a?(Minitest::Parallel::Test::ClassMethods)
    suite.extend(Minitest::Unparallelize)
  end

  suite.run(reporter, options)
end

.parallel_fork_run_test_suites(suites, reporter, options) ⇒ Object



66
67
68
69
70
# File 'lib/minitest/parallel_fork.rb', line 66

def parallel_fork_run_test_suites(suites, reporter, options)
  suites.each do |suite|
    parallel_fork_run_test_suite(suite, reporter, options)
  end
end

.parallel_fork_setup_children(suites, reporter, options) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/minitest/parallel_fork.rb', line 80

def parallel_fork_setup_children(suites, reporter, options)
  set_parallel_fork_stat_reporter(reporter)
  run_before_parallel_fork_hook

  n = parallel_fork_number
  n.times.map do |i|
    read, write = IO.pipe.each{|io| io.binmode}
    pid = Process.fork do
      read.close
      run_after_parallel_fork_hook(i)

      p_suites = []
      suites.each_with_index{|s, j| p_suites << s if j % n == i}
      parallel_fork_run_test_suites(p_suites, reporter, options)

      write.write(Marshal.dump(parallel_fork_data_to_marshal))
      write.close
    end
    write.close
    [pid, read]
  end
end

.parallel_fork_suitesObject



37
38
39
# File 'lib/minitest/parallel_fork.rb', line 37

def parallel_fork_suites
  Minitest::Runnable.runnables.shuffle
end

.parallel_fork_wait_for_children(child_info, reporter) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/minitest/parallel_fork.rb', line 107

def parallel_fork_wait_for_children(child_info, reporter)
  parallel_fork_child_data(child_info).each do |data|
    count, assertions, results = data
    reporter.reporters.each do |rep|
      next unless %i'count assertions results count= assertions='.all?{|meth| rep.respond_to?(meth)}
      rep.count += count
      rep.assertions += assertions
      rep.results.concat(results)
    end
  end
end

.run_after_parallel_fork_hook(i) ⇒ Object



47
48
49
50
51
# File 'lib/minitest/parallel_fork.rb', line 47

def run_after_parallel_fork_hook(i)
  if @after_parallel_fork
    @after_parallel_fork.call(i)
  end
end

.run_before_parallel_fork_hookObject



41
42
43
44
45
# File 'lib/minitest/parallel_fork.rb', line 41

def run_before_parallel_fork_hook
  if @before_parallel_fork
    @before_parallel_fork.call
  end
end

.set_parallel_fork_stat_reporter(reporter) ⇒ Object



31
32
33
34
35
# File 'lib/minitest/parallel_fork.rb', line 31

def set_parallel_fork_stat_reporter(reporter)
  @parallel_fork_stat_reporter = reporter.reporters.detect do |rep|
    %w'count assertions results count= assertions='.all?{|meth| rep.respond_to?(meth)}
  end
end