/
parallelize_executor.rb
76 lines (62 loc) · 2.04 KB
/
parallelize_executor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# frozen_string_literal: true
module ActiveSupport
module Testing
class ParallelizeExecutor # :nodoc:
attr_reader :size, :parallelize_with, :threshold
def initialize(size:, with:, threshold: ActiveSupport.test_parallelization_threshold)
@size = size
@parallelize_with = with
@threshold = threshold
end
def start
parallelize if should_parallelize?
show_execution_info
parallel_executor.start if parallelized?
end
def <<(work)
parallel_executor << work if parallelized?
end
def shutdown
parallel_executor.shutdown if parallelized?
end
private
def parallel_executor
@parallel_executor ||= build_parallel_executor
end
def build_parallel_executor
case parallelize_with
when :processes
Testing::Parallelization.new(size)
when :threads
ActiveSupport::TestCase.lock_threads = false if defined?(ActiveSupport::TestCase.lock_threads)
Minitest::Parallel::Executor.new(size)
else
raise ArgumentError, "#{parallelize_with} is not a supported parallelization executor."
end
end
def parallelize
@parallelized = true
Minitest::Test.parallelize_me!
end
def parallelized?
@parallelized if defined?(@parallelized)
end
def should_parallelize?
ENV["PARALLEL_WORKERS"] || tests_count > threshold
end
def tests_count
@tests_count ||= Minitest::Runnable.runnables.sum { |runnable| runnable.runnable_methods.size }
end
def show_execution_info
puts execution_info
end
def execution_info
if parallelized?
"Running #{tests_count} tests in parallel using #{parallel_executor.size} #{parallelize_with}"
else
"Running #{tests_count} tests in a single process (parallelization threshold is #{threshold})"
end
end
end
end
end