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.
This commit is contained in:
bors 2016-08-13 09:52:49 -07:00 committed by GitHub
commit 2b7ea14cc4
3 changed files with 26 additions and 5 deletions

View File

@ -264,7 +264,8 @@ which link to the standard library.
.TP .TP
\fBRUST_TEST_THREADS\fR \fBRUST_TEST_THREADS\fR
The test framework Rust provides executes tests in parallel. This variable sets 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 .TP
\fBRUST_TEST_NOCAPTURE\fR \fBRUST_TEST_NOCAPTURE\fR

View File

@ -303,6 +303,7 @@ pub struct TestOpts {
pub nocapture: bool, pub nocapture: bool,
pub color: ColorConfig, pub color: ColorConfig,
pub quiet: bool, pub quiet: bool,
pub test_threads: Option<usize>,
} }
impl TestOpts { impl TestOpts {
@ -317,6 +318,7 @@ impl TestOpts {
nocapture: false, nocapture: false,
color: AutoColor, color: AutoColor,
quiet: false, quiet: false,
test_threads: None,
} }
} }
} }
@ -334,6 +336,8 @@ fn optgroups() -> Vec<getopts::OptGroup> {
of stdout", "PATH"), of stdout", "PATH"),
getopts::optflag("", "nocapture", "don't capture stdout/stderr of each \ getopts::optflag("", "nocapture", "don't capture stdout/stderr of each \
task, allow printing directly"), 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::optflag("q", "quiet", "Display one character per test instead of one line"),
getopts::optopt("", "color", "Configure coloring of output: getopts::optopt("", "color", "Configure coloring of output:
auto = colorize if stdout is a tty and tests are run on serially (default); 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
tests whose names contain the filter are run. tests whose names contain the filter are run.
By default, all tests are run in parallel. This can be altered with the 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. 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 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> {
}; };
} }
let test_threads = match matches.opt_str("test-threads") {
Some(n_str) =>
match n_str.parse::<usize>() {
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) { let color = match matches.opt_str("color").as_ref().map(|s| &**s) {
Some("auto") | None => AutoColor, Some("auto") | None => AutoColor,
Some("always") => AlwaysColor, Some("always") => AlwaysColor,
@ -429,6 +446,7 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
nocapture: nocapture, nocapture: nocapture,
color: color, color: color,
quiet: quiet, quiet: quiet,
test_threads: test_threads,
}; };
Some(Ok(test_opts)) Some(Ok(test_opts))
@ -871,9 +889,10 @@ fn run_tests<F>(opts: &TestOpts, tests: Vec<TestDescAndFn>, mut callback: F) ->
} }
}); });
// It's tempting to just spawn all the tests at once, but since we have let concurrency = match opts.test_threads {
// many tests that run in other processes we would be making a big mess. Some(n) => n,
let concurrency = get_concurrency(); None => get_concurrency(),
};
let mut remaining = filtered_tests; let mut remaining = filtered_tests;
remaining.reverse(); remaining.reverse();

View File

@ -310,6 +310,7 @@ pub fn test_opts(config: &Config) -> test::TestOpts {
Err(_) => false Err(_) => false
}, },
color: test::AutoColor, color: test::AutoColor,
test_threads: None,
} }
} }