From 6ca90942e744f79982a9b485e2367b0ecee14527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jupp=20M=C3=BCller?= Date: Sat, 6 Aug 2016 09:01:12 +0200 Subject: [PATCH] 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. --- man/rustc.1 | 3 ++- src/libtest/lib.rs | 27 +++++++++++++++++++++++---- src/tools/compiletest/src/main.rs | 1 + 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/man/rustc.1 b/man/rustc.1 index fa61afd3be58..95650255c5d2 100644 --- a/man/rustc.1 +++ b/man/rustc.1 @@ -264,7 +264,8 @@ which link to the standard library. .TP \fBRUST_TEST_THREADS\fR The test framework Rust provides executes tests in parallel. This variable sets -the maximum number of threads used for this purpose. +the maximum number of threads used for this purpose. This setting is overridden +by the --test-threads option. .TP \fBRUST_TEST_NOCAPTURE\fR diff --git a/src/libtest/lib.rs b/src/libtest/lib.rs index 248f6f98650a..710df24525ef 100644 --- a/src/libtest/lib.rs +++ b/src/libtest/lib.rs @@ -300,6 +300,7 @@ pub struct TestOpts { pub nocapture: bool, pub color: ColorConfig, pub quiet: bool, + pub test_threads: Option, } impl TestOpts { @@ -314,6 +315,7 @@ fn new() -> TestOpts { nocapture: false, color: AutoColor, quiet: false, + test_threads: None, } } } @@ -331,6 +333,8 @@ fn optgroups() -> Vec { of stdout", "PATH"), getopts::optflag("", "nocapture", "don't capture stdout/stderr of each \ task, allow printing directly"), + getopts::optopt("", "test-threads", "Number of threads used for running tests \ + in parallel", "n_threads"), getopts::optflag("q", "quiet", "Display one character per test instead of one line"), getopts::optopt("", "color", "Configure coloring of output: auto = colorize if stdout is a tty and tests are run on serially (default); @@ -346,7 +350,8 @@ fn usage(binary: &str) { tests whose names contain the filter are run. By default, all tests are run in parallel. This can be altered with the -RUST_TEST_THREADS environment variable when running tests (set it to 1). +--test-threads flag or the RUST_TEST_THREADS environment variable when running +tests (set it to 1). All tests have their standard output and standard error captured by default. This can be overridden with the --nocapture flag or setting RUST_TEST_NOCAPTURE @@ -405,6 +410,18 @@ pub fn parse_opts(args: &[String]) -> Option { }; } + let test_threads = match matches.opt_str("test-threads") { + Some(n_str) => + match n_str.parse::() { + Ok(n) => Some(n), + Err(e) => + return Some(Err(format!("argument for --test-threads must be a number > 0 \ + (error: {})", e))) + }, + None => + None, + }; + let color = match matches.opt_str("color").as_ref().map(|s| &**s) { Some("auto") | None => AutoColor, Some("always") => AlwaysColor, @@ -426,6 +443,7 @@ pub fn parse_opts(args: &[String]) -> Option { nocapture: nocapture, color: color, quiet: quiet, + test_threads: test_threads, }; Some(Ok(test_opts)) @@ -857,9 +875,10 @@ fn run_tests(opts: &TestOpts, tests: Vec, mut callback: F) -> } }); - // It's tempting to just spawn all the tests at once, but since we have - // many tests that run in other processes we would be making a big mess. - let concurrency = get_concurrency(); + let concurrency = match opts.test_threads { + Some(n) => n, + None => get_concurrency(), + }; let mut remaining = filtered_tests; remaining.reverse(); diff --git a/src/tools/compiletest/src/main.rs b/src/tools/compiletest/src/main.rs index cefcc11486fe..90641b5c476d 100644 --- a/src/tools/compiletest/src/main.rs +++ b/src/tools/compiletest/src/main.rs @@ -310,6 +310,7 @@ pub fn test_opts(config: &Config) -> test::TestOpts { Err(_) => false }, color: test::AutoColor, + test_threads: None, } }