-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathexecutor_spec.rb
More file actions
75 lines (61 loc) · 2.17 KB
/
Copy pathexecutor_spec.rb
File metadata and controls
75 lines (61 loc) · 2.17 KB
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
# typed: strict
# frozen_string_literal: true
require "spec_helper"
module Tapioca
class ExecutorSpec < Minitest::Spec
include Tapioca::Helpers::Test::Parallel
describe "Tapioca::Executor" do
before do
@queue = (0...8).to_a #: Array[Integer]
@executor = Executor.new(@queue) #: Executor
end
it "runs sequentially when the number of workers is one" do
executor = Executor.new(@queue, number_of_workers: 1)
parent_pid = Process.pid
executor.run_in_parallel do
assert_equal(parent_pid, Process.pid)
end
end
it "forks different processes if number of workers is greater than one" do
executor = Executor.new(@queue, number_of_workers: 4)
parent_pid = Process.pid
executor.run_in_parallel do
refute_equal(parent_pid, Process.pid)
end
end
it "can return a value from the parallelized block" do
queue = @queue.dup
executor = Executor.new(@queue, number_of_workers: 4)
result = executor.run_in_parallel { |number| number }
assert_equal(queue, result.sort)
end
it "limits_parallel_work_to_nprocessors_by_default" do
ENV["PARALLEL_PROCESSOR_COUNT"] = nil
nprocessors = 3
T.unsafe(Etc).stub(:nprocessors, -> { nprocessors }) do
T.unsafe(Parallel).stub(:map, assert_parallel_count(nprocessors)) do
executor = Executor.new(@queue)
executor.run_in_parallel {}
end
end
end
it "limits_parallel_work_to_PARALLEL_PROCESS_COUNT" do
env_limit = 2
ENV["PARALLEL_PROCESSOR_COUNT"] = env_limit.to_s
T.unsafe(Etc).stub(:nprocessors, -> { env_limit + 1 }) do
T.unsafe(Parallel).stub(:map, assert_parallel_count(env_limit)) do
executor = Executor.new(@queue)
executor.run_in_parallel {}
end
end
end
#: (Integer expected_count) -> ^(untyped _arg1, untyped _arg2) -> Array[Integer]
def assert_parallel_count(expected_count)
->(_, options) {
assert_equal(expected_count, options[:in_processes])
[]
}
end
end
end
end