Skip to content

Commit 6ca9094

Browse files
committed
Add --test-threads option to test binaries
This change allows parallelism of test runs to be specified by a command line flag names --test-threads in addition to the existing environment variable RUST_TEST_THREADS. Fixes #25636.
1 parent 4c02363 commit 6ca9094

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

man/rustc.1

+2-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ which link to the standard library.
264264
.TP
265265
\fBRUST_TEST_THREADS\fR
266266
The test framework Rust provides executes tests in parallel. This variable sets
267-
the maximum number of threads used for this purpose.
267+
the maximum number of threads used for this purpose. This setting is overridden
268+
by the --test-threads option.
268269

269270
.TP
270271
\fBRUST_TEST_NOCAPTURE\fR

src/libtest/lib.rs

+23-4
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ pub struct TestOpts {
300300
pub nocapture: bool,
301301
pub color: ColorConfig,
302302
pub quiet: bool,
303+
pub test_threads: Option<usize>,
303304
}
304305

305306
impl TestOpts {
@@ -314,6 +315,7 @@ impl TestOpts {
314315
nocapture: false,
315316
color: AutoColor,
316317
quiet: false,
318+
test_threads: None,
317319
}
318320
}
319321
}
@@ -331,6 +333,8 @@ fn optgroups() -> Vec<getopts::OptGroup> {
331333
of stdout", "PATH"),
332334
getopts::optflag("", "nocapture", "don't capture stdout/stderr of each \
333335
task, allow printing directly"),
336+
getopts::optopt("", "test-threads", "Number of threads used for running tests \
337+
in parallel", "n_threads"),
334338
getopts::optflag("q", "quiet", "Display one character per test instead of one line"),
335339
getopts::optopt("", "color", "Configure coloring of output:
336340
auto = colorize if stdout is a tty and tests are run on serially (default);
@@ -346,7 +350,8 @@ The FILTER string is tested against the name of all tests, and only those
346350
tests whose names contain the filter are run.
347351
348352
By default, all tests are run in parallel. This can be altered with the
349-
RUST_TEST_THREADS environment variable when running tests (set it to 1).
353+
--test-threads flag or the RUST_TEST_THREADS environment variable when running
354+
tests (set it to 1).
350355
351356
All tests have their standard output and standard error captured by default.
352357
This can be overridden with the --nocapture flag or setting RUST_TEST_NOCAPTURE
@@ -405,6 +410,18 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
405410
};
406411
}
407412

413+
let test_threads = match matches.opt_str("test-threads") {
414+
Some(n_str) =>
415+
match n_str.parse::<usize>() {
416+
Ok(n) => Some(n),
417+
Err(e) =>
418+
return Some(Err(format!("argument for --test-threads must be a number > 0 \
419+
(error: {})", e)))
420+
},
421+
None =>
422+
None,
423+
};
424+
408425
let color = match matches.opt_str("color").as_ref().map(|s| &**s) {
409426
Some("auto") | None => AutoColor,
410427
Some("always") => AlwaysColor,
@@ -426,6 +443,7 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
426443
nocapture: nocapture,
427444
color: color,
428445
quiet: quiet,
446+
test_threads: test_threads,
429447
};
430448

431449
Some(Ok(test_opts))
@@ -857,9 +875,10 @@ fn run_tests<F>(opts: &TestOpts, tests: Vec<TestDescAndFn>, mut callback: F) ->
857875
}
858876
});
859877

860-
// It's tempting to just spawn all the tests at once, but since we have
861-
// many tests that run in other processes we would be making a big mess.
862-
let concurrency = get_concurrency();
878+
let concurrency = match opts.test_threads {
879+
Some(n) => n,
880+
None => get_concurrency(),
881+
};
863882

864883
let mut remaining = filtered_tests;
865884
remaining.reverse();

src/tools/compiletest/src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ pub fn test_opts(config: &Config) -> test::TestOpts {
310310
Err(_) => false
311311
},
312312
color: test::AutoColor,
313+
test_threads: None,
313314
}
314315
}
315316

0 commit comments

Comments
 (0)