Skip to content

Commit 2b7ea14

Browse files
authored
Auto merge of #35414 - jupp0r:feature/test-threads-flag, r=alexcrichton
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.
2 parents e64f688 + 6ca9094 commit 2b7ea14

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
@@ -303,6 +303,7 @@ pub struct TestOpts {
303303
pub nocapture: bool,
304304
pub color: ColorConfig,
305305
pub quiet: bool,
306+
pub test_threads: Option<usize>,
306307
}
307308

308309
impl TestOpts {
@@ -317,6 +318,7 @@ impl TestOpts {
317318
nocapture: false,
318319
color: AutoColor,
319320
quiet: false,
321+
test_threads: None,
320322
}
321323
}
322324
}
@@ -334,6 +336,8 @@ fn optgroups() -> Vec<getopts::OptGroup> {
334336
of stdout", "PATH"),
335337
getopts::optflag("", "nocapture", "don't capture stdout/stderr of each \
336338
task, allow printing directly"),
339+
getopts::optopt("", "test-threads", "Number of threads used for running tests \
340+
in parallel", "n_threads"),
337341
getopts::optflag("q", "quiet", "Display one character per test instead of one line"),
338342
getopts::optopt("", "color", "Configure coloring of output:
339343
auto = colorize if stdout is a tty and tests are run on serially (default);
@@ -349,7 +353,8 @@ The FILTER string is tested against the name of all tests, and only those
349353
tests whose names contain the filter are run.
350354
351355
By default, all tests are run in parallel. This can be altered with the
352-
RUST_TEST_THREADS environment variable when running tests (set it to 1).
356+
--test-threads flag or the RUST_TEST_THREADS environment variable when running
357+
tests (set it to 1).
353358
354359
All tests have their standard output and standard error captured by default.
355360
This can be overridden with the --nocapture flag or setting RUST_TEST_NOCAPTURE
@@ -408,6 +413,18 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
408413
};
409414
}
410415

416+
let test_threads = match matches.opt_str("test-threads") {
417+
Some(n_str) =>
418+
match n_str.parse::<usize>() {
419+
Ok(n) => Some(n),
420+
Err(e) =>
421+
return Some(Err(format!("argument for --test-threads must be a number > 0 \
422+
(error: {})", e)))
423+
},
424+
None =>
425+
None,
426+
};
427+
411428
let color = match matches.opt_str("color").as_ref().map(|s| &**s) {
412429
Some("auto") | None => AutoColor,
413430
Some("always") => AlwaysColor,
@@ -429,6 +446,7 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
429446
nocapture: nocapture,
430447
color: color,
431448
quiet: quiet,
449+
test_threads: test_threads,
432450
};
433451

434452
Some(Ok(test_opts))
@@ -871,9 +889,10 @@ fn run_tests<F>(opts: &TestOpts, tests: Vec<TestDescAndFn>, mut callback: F) ->
871889
}
872890
});
873891

874-
// It's tempting to just spawn all the tests at once, but since we have
875-
// many tests that run in other processes we would be making a big mess.
876-
let concurrency = get_concurrency();
892+
let concurrency = match opts.test_threads {
893+
Some(n) => n,
894+
None => get_concurrency(),
895+
};
877896

878897
let mut remaining = filtered_tests;
879898
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)