Skip to content

Commit 62c4d0f

Browse files
authored
Allow setting multiplier via ENV variable (#992)
Currently the CLI accepts `-m` or `--multiply-processes` with a `COUNT` value which is a multiple applied to the number of parallel process to be spun up. This commit adds the aditional abiity to modify `COUNT` by specifing `PARALLEL_TEST_MULTIPLE` which will be parsed into a float.
1 parent 8b97f62 commit 62c4d0f

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
### Added
88

9+
- Allow processor multiplier (flag: `-m` or `--multiply-processes`) to be set via the environment variable `PARALLEL_TEST_MULTIPLE`
10+
911
### Fixed
1012

1113
## 4.9.1 - 2025-02-19

Readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ TIPS
335335
e.g. `config.cache_store = ..., namespace: "test_#{ENV['TEST_ENV_NUMBER']}"`
336336
- Debug errors that only happen with multiple files using `--verbose` and [cleanser](https://github.com/grosser/cleanser)
337337
- `export PARALLEL_TEST_PROCESSORS=13` to override default processor count
338+
- `export PARALLEL_TEST_MULTIPLE=.5` to override default processor multiplier
338339
- Shell alias: `alias prspec='parallel_rspec -m 2 --'`
339340
- [Spring] Add the [spring-commands-parallel-tests](https://github.com/DocSpring/spring-commands-parallel-tests) gem to your `Gemfile` to get `parallel_tests` working with Spring.
340341
- `--first-is-1` will make the first environment be `1`, so you can test while running your full suite.<br/>

lib/parallel_tests.rb

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
module ParallelTests
77
WINDOWS = (RbConfig::CONFIG['host_os'] =~ /cygwin|mswin|mingw|bccwin|wince|emx/)
88
RUBY_BINARY = File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'])
9+
DEFAULT_MULTIPLE = 1.0
910

1011
autoload :CLI, "parallel_tests/cli"
1112
autoload :VERSION, "parallel_tests/version"
@@ -21,6 +22,14 @@ def determine_number_of_processes(count)
2122
].detect { |c| !c.to_s.strip.empty? }.to_i
2223
end
2324

25+
def determine_multiple(multiple)
26+
[
27+
multiple,
28+
ENV["PARALLEL_TEST_MULTIPLE"],
29+
DEFAULT_MULTIPLE,
30+
].detect { |c| !c.to_s.strip.empty? }.to_f
31+
end
32+
2433
def with_pid_file
2534
Tempfile.open('parallel_tests-pidfile') do |f|
2635
ENV['PARALLEL_PID_FILE'] = f.path

lib/parallel_tests/cli.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def run(argv)
1515
ENV['DISABLE_SPRING'] ||= '1'
1616

1717
num_processes = ParallelTests.determine_number_of_processes(options[:count])
18-
num_processes *= (options[:multiply] || 1)
18+
num_processes *= ParallelTests.determine_multiple(options[:multiply])
1919

2020
options[:first_is_1] ||= first_is_1?
2121

spec/parallel_tests_spec.rb

+34
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,40 @@ def call(count)
3434
end
3535
end
3636

37+
describe ".determine_multiple" do
38+
let(:default_multiple) { 1.0 }
39+
40+
before do
41+
allow(Parallel).to receive(:multiple).and_return(default_multiple)
42+
end
43+
44+
def call(multiple)
45+
ParallelTests.determine_multiple(multiple)
46+
end
47+
48+
it "uses the given multiple if set" do
49+
expect(call('.5')).to eq(0.5)
50+
end
51+
52+
it "uses the processor multiple from Parallel" do
53+
expect(call(nil)).to eq(default_multiple)
54+
end
55+
56+
it "uses the processor multiple from ENV before Parallel" do
57+
ENV['PARALLEL_TEST_MULTIPLE'] = '0.75'
58+
expect(call(nil)).to eq(0.75)
59+
end
60+
61+
it "does not use blank multiple" do
62+
expect(call(' ')).to eq(default_multiple)
63+
end
64+
65+
it "does not use blank env" do
66+
ENV['PARALLEL_TEST_MULTIPLE'] = ' '
67+
expect(call(nil)).to eq(default_multiple)
68+
end
69+
end
70+
3771
describe ".bundler_enabled?" do
3872
before do
3973
allow(Object).to receive(:const_defined?).with(:Bundler).and_return false

0 commit comments

Comments
 (0)