2019-03-02 12:40:15 -06:00
|
|
|
//! Support code for rustc's built in unit-test and micro-benchmarking
|
|
|
|
//! framework.
|
|
|
|
//!
|
|
|
|
//! Almost all user code will only be interested in `Bencher` and
|
|
|
|
//! `black_box`. All other interactions (such as writing tests and
|
|
|
|
//! benchmarks themselves) should be done via the `#[test]` and
|
|
|
|
//! `#[bench]` attributes.
|
|
|
|
//!
|
|
|
|
//! See the [Testing Chapter](../book/ch11-00-testing.html) of the book for more details.
|
|
|
|
|
2019-04-07 00:48:59 -05:00
|
|
|
// Currently, not much of this is meant for users. It is intended to
|
|
|
|
// support the simplest interface possible for representing and
|
|
|
|
// running tests while providing a base that other test frameworks may
|
|
|
|
// build off of.
|
|
|
|
|
|
|
|
// N.B., this is also specified in this crate's Cargo.toml, but libsyntax contains logic specific to
|
|
|
|
// this crate, which relies on this attribute (rather than the value of `--crate-name` passed by
|
|
|
|
// cargo) to detect this crate.
|
|
|
|
|
2019-03-02 12:40:15 -06:00
|
|
|
#![crate_name = "test"]
|
2019-07-27 07:06:49 -05:00
|
|
|
#![unstable(feature = "test", issue = "50297")]
|
2019-04-07 00:48:59 -05:00
|
|
|
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/", test(attr(deny(warnings))))]
|
2019-03-04 11:04:08 -06:00
|
|
|
#![feature(asm)]
|
2019-09-11 19:13:34 -05:00
|
|
|
#![cfg_attr(any(unix, target_os = "cloudabi"), feature(libc))]
|
|
|
|
#![feature(rustc_private)]
|
2019-04-07 00:48:59 -05:00
|
|
|
#![feature(nll)]
|
|
|
|
#![feature(set_stdio)]
|
|
|
|
#![feature(panic_unwind)]
|
2019-03-03 10:39:57 -06:00
|
|
|
#![feature(staged_api)]
|
2019-04-07 00:48:59 -05:00
|
|
|
#![feature(termination_trait_lib)]
|
2019-03-03 08:50:52 -06:00
|
|
|
#![feature(test)]
|
2019-03-02 12:40:15 -06:00
|
|
|
|
2019-04-07 00:48:59 -05:00
|
|
|
use getopts;
|
|
|
|
#[cfg(any(unix, target_os = "cloudabi"))]
|
|
|
|
extern crate libc;
|
|
|
|
use term;
|
|
|
|
|
|
|
|
pub use self::ColorConfig::*;
|
|
|
|
use self::NamePadding::*;
|
|
|
|
use self::OutputLocation::*;
|
|
|
|
use self::TestEvent::*;
|
|
|
|
pub use self::TestFn::*;
|
|
|
|
pub use self::TestName::*;
|
|
|
|
pub use self::TestResult::*;
|
|
|
|
|
|
|
|
use std::any::Any;
|
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::cmp;
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use std::env;
|
|
|
|
use std::fmt;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io;
|
|
|
|
use std::io::prelude::*;
|
2019-09-11 19:13:34 -05:00
|
|
|
use std::panic::{self, catch_unwind, AssertUnwindSafe, PanicInfo};
|
2019-04-07 00:48:59 -05:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::process;
|
2019-09-11 19:13:34 -05:00
|
|
|
use std::process::{ExitStatus, Command, Termination};
|
2019-09-28 09:07:18 -05:00
|
|
|
use std::str::FromStr;
|
2019-04-07 00:48:59 -05:00
|
|
|
use std::sync::mpsc::{channel, Sender};
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
use std::thread;
|
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
|
2019-07-31 18:28:43 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2019-04-07 00:48:59 -05:00
|
|
|
const TEST_WARN_TIMEOUT_S: u64 = 60;
|
|
|
|
const QUIET_MODE_MAX_COLUMN: usize = 100; // insert a '\n' after 100 tests in quiet mode
|
|
|
|
|
2019-09-11 19:13:34 -05:00
|
|
|
const SECONDARY_TEST_INVOKER_VAR: &'static str = "__RUST_TEST_INVOKE";
|
|
|
|
|
|
|
|
// Return codes for secondary process.
|
|
|
|
// Start somewhere other than 0 so we know the return code means what we think
|
|
|
|
// it means.
|
|
|
|
const TR_OK: i32 = 50;
|
|
|
|
const TR_FAILED: i32 = 51;
|
|
|
|
|
2019-09-28 09:07:18 -05:00
|
|
|
/// This small module contains constants used by `report-time` option.
|
|
|
|
/// Those constants values will be used if corresponding environment variables are not set.
|
|
|
|
///
|
|
|
|
/// To override values for unit-tests, use a constant `RUST_TEST_TIME_UNIT`,
|
|
|
|
/// To override values for integration tests, use a constant `RUST_TEST_TIME_INTEGRATION`,
|
|
|
|
/// To override values for doctests, use a constant `RUST_TEST_TIME_DOCTEST`.
|
|
|
|
///
|
|
|
|
/// Example of the expected format is `RUST_TEST_TIME_xxx=100,200`, where 100 means
|
|
|
|
/// warn time, and 200 means critical time.
|
|
|
|
pub mod time_constants {
|
|
|
|
use std::time::Duration;
|
|
|
|
use super::TEST_WARN_TIMEOUT_S;
|
|
|
|
|
|
|
|
/// Environment variable for overriding default threshold for unit-tests.
|
|
|
|
pub const UNIT_ENV_NAME: &str = "RUST_TEST_TIME_UNIT";
|
|
|
|
|
|
|
|
// Unit tests are supposed to be really quick.
|
|
|
|
pub const UNIT_WARN: Duration = Duration::from_millis(50);
|
|
|
|
pub const UNIT_CRITICAL: Duration = Duration::from_millis(100);
|
|
|
|
|
|
|
|
/// Environment variable for overriding default threshold for unit-tests.
|
|
|
|
pub const INTEGRATION_ENV_NAME: &str = "RUST_TEST_TIME_INTEGRATION";
|
|
|
|
|
|
|
|
// Integration tests may have a lot of work, so they can take longer to execute.
|
|
|
|
pub const INTEGRATION_WARN: Duration = Duration::from_millis(500);
|
|
|
|
pub const INTEGRATION_CRITICAL: Duration = Duration::from_millis(1000);
|
|
|
|
|
|
|
|
/// Environment variable for overriding default threshold for unit-tests.
|
|
|
|
pub const DOCTEST_ENV_NAME: &str = "RUST_TEST_TIME_DOCTEST";
|
|
|
|
|
|
|
|
// Doctests are similar to integration tests, because they can include a lot of
|
|
|
|
// initialization code.
|
|
|
|
pub const DOCTEST_WARN: Duration = INTEGRATION_WARN;
|
|
|
|
pub const DOCTEST_CRITICAL: Duration = INTEGRATION_CRITICAL;
|
|
|
|
|
|
|
|
// Do not suppose anything about unknown tests, base limits on the
|
|
|
|
// `TEST_WARN_TIMEOUT_S` constant.
|
|
|
|
pub const UNKNOWN_WARN: Duration = Duration::from_secs(TEST_WARN_TIMEOUT_S);
|
|
|
|
pub const UNKNOWN_CRITICAL: Duration = Duration::from_secs(TEST_WARN_TIMEOUT_S * 2);
|
|
|
|
}
|
|
|
|
|
2019-04-07 00:48:59 -05:00
|
|
|
// to be used by rustc to compile tests in libtest
|
|
|
|
pub mod test {
|
|
|
|
pub use crate::{
|
|
|
|
assert_test_result, filter_tests, parse_opts, run_test, test_main, test_main_static,
|
2019-09-11 19:13:34 -05:00
|
|
|
Bencher, DynTestFn, DynTestName, Metric, MetricMap, Options, RunIgnored, RunStrategy,
|
|
|
|
ShouldPanic, StaticBenchFn, StaticTestFn, StaticTestName, TestDesc, TestDescAndFn, TestName,
|
2019-09-28 09:07:18 -05:00
|
|
|
TestOpts, TestTimeOptions, TestType, TestResult, TrFailed, TrFailedMsg, TrIgnored, TrOk,
|
2019-04-07 00:48:59 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
mod formatters;
|
|
|
|
pub mod stats;
|
|
|
|
|
|
|
|
use crate::formatters::{JsonFormatter, OutputFormatter, PrettyFormatter, TerseFormatter};
|
|
|
|
|
|
|
|
/// Whether to execute tests concurrently or not
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub enum Concurrent {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
}
|
|
|
|
|
2019-09-28 09:07:18 -05:00
|
|
|
/// Type of the test according to the [rust book](https://doc.rust-lang.org/cargo/guide/tests.html)
|
|
|
|
/// conventions.
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
|
|
|
pub enum TestType {
|
|
|
|
/// Unit-tests are expected to be in the `src` folder of the crate.
|
|
|
|
UnitTest,
|
|
|
|
/// Integration-style tests are expected to be in the `tests` folder of the crate.
|
|
|
|
IntegrationTest,
|
|
|
|
/// Doctests are created by the `librustdoc` manually, so it's a different type of test.
|
|
|
|
DocTest,
|
|
|
|
/// Tests for the sources that don't follow the project layout convention
|
|
|
|
/// (e.g. tests in raw `main.rs` compiled by calling `rustc --test` directly).
|
|
|
|
Unknown,
|
|
|
|
}
|
|
|
|
|
2019-04-07 00:48:59 -05:00
|
|
|
// The name of a test. By convention this follows the rules for rust
|
|
|
|
// paths; i.e., it should be a series of identifiers separated by double
|
|
|
|
// colons. This way if some test runner wants to arrange the tests
|
|
|
|
// hierarchically it may.
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
|
|
|
|
pub enum TestName {
|
|
|
|
StaticTestName(&'static str),
|
|
|
|
DynTestName(String),
|
|
|
|
AlignedTestName(Cow<'static, str>, NamePadding),
|
|
|
|
}
|
|
|
|
impl TestName {
|
|
|
|
fn as_slice(&self) -> &str {
|
|
|
|
match *self {
|
|
|
|
StaticTestName(s) => s,
|
|
|
|
DynTestName(ref s) => s,
|
|
|
|
AlignedTestName(ref s, _) => &*s,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn padding(&self) -> NamePadding {
|
|
|
|
match self {
|
|
|
|
&AlignedTestName(_, p) => p,
|
|
|
|
_ => PadNone,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_padding(&self, padding: NamePadding) -> TestName {
|
|
|
|
let name = match self {
|
|
|
|
&TestName::StaticTestName(name) => Cow::Borrowed(name),
|
|
|
|
&TestName::DynTestName(ref name) => Cow::Owned(name.clone()),
|
|
|
|
&TestName::AlignedTestName(ref name, _) => name.clone(),
|
|
|
|
};
|
|
|
|
|
|
|
|
TestName::AlignedTestName(name, padding)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl fmt::Display for TestName {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
fmt::Display::fmt(self.as_slice(), f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
|
|
|
|
pub enum NamePadding {
|
|
|
|
PadNone,
|
|
|
|
PadOnRight,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestDesc {
|
|
|
|
fn padded_name(&self, column_count: usize, align: NamePadding) -> String {
|
|
|
|
let mut name = String::from(self.name.as_slice());
|
|
|
|
let fill = column_count.saturating_sub(name.len());
|
|
|
|
let pad = " ".repeat(fill);
|
|
|
|
match align {
|
|
|
|
PadNone => name,
|
|
|
|
PadOnRight => {
|
|
|
|
name.push_str(&pad);
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents a benchmark function.
|
|
|
|
pub trait TDynBenchFn: Send {
|
|
|
|
fn run(&self, harness: &mut Bencher);
|
|
|
|
}
|
|
|
|
|
|
|
|
// A function that runs a test. If the function returns successfully,
|
|
|
|
// the test succeeds; if the function panics then the test fails. We
|
|
|
|
// may need to come up with a more clever definition of test in order
|
|
|
|
// to support isolation of tests into threads.
|
|
|
|
pub enum TestFn {
|
|
|
|
StaticTestFn(fn()),
|
|
|
|
StaticBenchFn(fn(&mut Bencher)),
|
2019-05-24 00:51:48 -05:00
|
|
|
DynTestFn(Box<dyn FnOnce() + Send>),
|
2019-04-07 00:48:59 -05:00
|
|
|
DynBenchFn(Box<dyn TDynBenchFn + 'static>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestFn {
|
|
|
|
fn padding(&self) -> NamePadding {
|
|
|
|
match *self {
|
|
|
|
StaticTestFn(..) => PadNone,
|
|
|
|
StaticBenchFn(..) => PadOnRight,
|
|
|
|
DynTestFn(..) => PadNone,
|
|
|
|
DynBenchFn(..) => PadOnRight,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for TestFn {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
f.write_str(match *self {
|
|
|
|
StaticTestFn(..) => "StaticTestFn(..)",
|
|
|
|
StaticBenchFn(..) => "StaticBenchFn(..)",
|
|
|
|
DynTestFn(..) => "DynTestFn(..)",
|
|
|
|
DynBenchFn(..) => "DynBenchFn(..)",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Manager of the benchmarking runs.
|
|
|
|
///
|
|
|
|
/// This is fed into functions marked with `#[bench]` to allow for
|
|
|
|
/// set-up & tear-down before running a piece of code repeatedly via a
|
|
|
|
/// call to `iter`.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Bencher {
|
|
|
|
mode: BenchMode,
|
|
|
|
summary: Option<stats::Summary>,
|
|
|
|
pub bytes: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Eq)]
|
|
|
|
pub enum BenchMode {
|
|
|
|
Auto,
|
|
|
|
Single,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
|
|
|
pub enum ShouldPanic {
|
|
|
|
No,
|
|
|
|
Yes,
|
|
|
|
YesWithMessage(&'static str),
|
|
|
|
}
|
|
|
|
|
|
|
|
// The definition of a single test. A test runner will run a list of
|
|
|
|
// these.
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
|
|
|
pub struct TestDesc {
|
|
|
|
pub name: TestName,
|
|
|
|
pub ignore: bool,
|
|
|
|
pub should_panic: ShouldPanic,
|
|
|
|
pub allow_fail: bool,
|
2019-09-28 09:07:18 -05:00
|
|
|
pub test_type: TestType,
|
2019-04-07 00:48:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct TestDescAndFn {
|
|
|
|
pub desc: TestDesc,
|
|
|
|
pub testfn: TestFn,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Debug, Copy)]
|
|
|
|
pub struct Metric {
|
|
|
|
value: f64,
|
|
|
|
noise: f64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Metric {
|
|
|
|
pub fn new(value: f64, noise: f64) -> Metric {
|
|
|
|
Metric { value, noise }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// In case we want to add other options as well, just add them in this struct.
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub struct Options {
|
|
|
|
display_output: bool,
|
2019-09-11 19:13:34 -05:00
|
|
|
panic_abort: bool,
|
2019-04-07 00:48:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Options {
|
|
|
|
pub fn new() -> Options {
|
|
|
|
Options {
|
|
|
|
display_output: false,
|
2019-09-11 19:13:34 -05:00
|
|
|
panic_abort: false,
|
2019-04-07 00:48:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn display_output(mut self, display_output: bool) -> Options {
|
|
|
|
self.display_output = display_output;
|
|
|
|
self
|
|
|
|
}
|
2019-09-11 19:13:34 -05:00
|
|
|
|
|
|
|
pub fn panic_abort(mut self, panic_abort: bool) -> Options {
|
|
|
|
self.panic_abort = panic_abort;
|
|
|
|
self
|
|
|
|
}
|
2019-04-07 00:48:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// The default console test runner. It accepts the command line
|
|
|
|
// arguments and a vector of test_descs.
|
2019-07-11 15:23:00 -05:00
|
|
|
pub fn test_main(args: &[String], tests: Vec<TestDescAndFn>, options: Option<Options>) {
|
2019-04-07 00:48:59 -05:00
|
|
|
let mut opts = match parse_opts(args) {
|
|
|
|
Some(Ok(o)) => o,
|
|
|
|
Some(Err(msg)) => {
|
|
|
|
eprintln!("error: {}", msg);
|
|
|
|
process::exit(101);
|
|
|
|
}
|
|
|
|
None => return,
|
|
|
|
};
|
2019-07-11 15:23:00 -05:00
|
|
|
if let Some(options) = options {
|
|
|
|
opts.options = options;
|
|
|
|
}
|
2019-04-07 00:48:59 -05:00
|
|
|
if opts.list {
|
|
|
|
if let Err(e) = list_tests_console(&opts, tests) {
|
|
|
|
eprintln!("error: io error when listing tests: {:?}", e);
|
|
|
|
process::exit(101);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match run_tests_console(&opts, tests) {
|
|
|
|
Ok(true) => {}
|
|
|
|
Ok(false) => process::exit(101),
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("error: io error when listing tests: {:?}", e);
|
|
|
|
process::exit(101);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-11 19:13:34 -05:00
|
|
|
/// A variant optimized for invocation with a static test vector.
|
|
|
|
/// This will panic (intentionally) when fed any dynamic tests.
|
|
|
|
///
|
|
|
|
/// This is the entry point for the main function generated by `rustc --test`
|
|
|
|
/// when panic=unwind.
|
2019-04-07 00:48:59 -05:00
|
|
|
pub fn test_main_static(tests: &[&TestDescAndFn]) {
|
|
|
|
let args = env::args().collect::<Vec<_>>();
|
2019-09-11 19:13:34 -05:00
|
|
|
let owned_tests: Vec<_> = tests.iter().map(make_owned_test).collect();
|
2019-07-11 15:23:00 -05:00
|
|
|
test_main(&args, owned_tests, None)
|
2019-04-07 00:48:59 -05:00
|
|
|
}
|
|
|
|
|
2019-09-11 19:13:34 -05:00
|
|
|
/// A variant optimized for invocation with a static test vector.
|
|
|
|
/// This will panic (intentionally) when fed any dynamic tests.
|
|
|
|
///
|
|
|
|
/// Runs tests in panic=abort mode, which involves spawning subprocesses for
|
|
|
|
/// tests.
|
|
|
|
///
|
|
|
|
/// This is the entry point for the main function generated by `rustc --test`
|
|
|
|
/// when panic=abort.
|
|
|
|
pub fn test_main_static_abort(tests: &[&TestDescAndFn]) {
|
|
|
|
// If we're being run in SpawnedSecondary mode, run the test here. run_test
|
|
|
|
// will then exit the process.
|
|
|
|
if let Ok(name) = env::var(SECONDARY_TEST_INVOKER_VAR) {
|
|
|
|
let test = tests
|
|
|
|
.iter()
|
|
|
|
.filter(|test| test.desc.name.as_slice() == name)
|
|
|
|
.map(make_owned_test)
|
|
|
|
.next()
|
|
|
|
.expect("couldn't find a test with the provided name");
|
|
|
|
let TestDescAndFn { desc, testfn } = test;
|
|
|
|
let testfn = match testfn {
|
|
|
|
StaticTestFn(f) => f,
|
|
|
|
_ => panic!("only static tests are supported"),
|
|
|
|
};
|
|
|
|
run_test_in_spawned_subprocess(desc, Box::new(testfn));
|
|
|
|
}
|
|
|
|
|
|
|
|
let args = env::args().collect::<Vec<_>>();
|
|
|
|
let owned_tests: Vec<_> = tests.iter().map(make_owned_test).collect();
|
|
|
|
test_main(&args, owned_tests, Some(Options::new().panic_abort(true)))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clones static values for putting into a dynamic vector, which test_main()
|
|
|
|
/// needs to hand out ownership of tests to parallel test runners.
|
|
|
|
///
|
|
|
|
/// This will panic when fed any dynamic tests, because they cannot be cloned.
|
|
|
|
fn make_owned_test(test: &&TestDescAndFn) -> TestDescAndFn {
|
|
|
|
match test.testfn {
|
|
|
|
StaticTestFn(f) => TestDescAndFn {
|
|
|
|
testfn: StaticTestFn(f),
|
|
|
|
desc: test.desc.clone(),
|
|
|
|
},
|
|
|
|
StaticBenchFn(f) => TestDescAndFn {
|
|
|
|
testfn: StaticBenchFn(f),
|
|
|
|
desc: test.desc.clone(),
|
|
|
|
},
|
|
|
|
_ => panic!("non-static tests passed to test::test_main_static"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 00:48:59 -05:00
|
|
|
/// Invoked when unit tests terminate. Should panic if the unit
|
|
|
|
/// Tests is considered a failure. By default, invokes `report()`
|
|
|
|
/// and checks for a `0` result.
|
|
|
|
pub fn assert_test_result<T: Termination>(result: T) {
|
|
|
|
let code = result.report();
|
|
|
|
assert_eq!(
|
|
|
|
code, 0,
|
|
|
|
"the test returned a termination value with a non-zero status code ({}) \
|
|
|
|
which indicates a failure",
|
|
|
|
code
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub enum ColorConfig {
|
|
|
|
AutoColor,
|
|
|
|
AlwaysColor,
|
|
|
|
NeverColor,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub enum OutputFormat {
|
|
|
|
Pretty,
|
|
|
|
Terse,
|
|
|
|
Json,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub enum RunIgnored {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
Only,
|
|
|
|
}
|
|
|
|
|
2019-09-28 09:07:18 -05:00
|
|
|
/// Structure denoting time limits for test execution.
|
|
|
|
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
|
|
|
|
pub struct TimeThreshold {
|
|
|
|
pub warn: Duration,
|
|
|
|
pub critical: Duration,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TimeThreshold {
|
|
|
|
/// Creates a new `TimeThreshold` instance with provided durations.
|
|
|
|
pub fn new(warn: Duration, critical: Duration) -> Self {
|
|
|
|
Self {
|
|
|
|
warn,
|
|
|
|
critical,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to create a `TimeThreshold` instance with values obtained
|
|
|
|
/// from the environment variable, and returns `None` if the variable
|
|
|
|
/// is not set.
|
|
|
|
/// Environment variable format is expected to match `\d+,\d+`.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if variable with provided name is set but contains inappropriate
|
|
|
|
/// value.
|
|
|
|
pub fn from_env_var(env_var_name: &str) -> Option<Self> {
|
|
|
|
let durations_str = env::var(env_var_name).ok()?;
|
|
|
|
|
|
|
|
// Split string into 2 substrings by comma and try to parse numbers.
|
2019-09-29 00:52:32 -05:00
|
|
|
let mut durations = durations_str
|
2019-09-28 09:07:18 -05:00
|
|
|
.splitn(2, ',')
|
|
|
|
.map(|v| {
|
|
|
|
u64::from_str(v).unwrap_or_else(|_| {
|
|
|
|
panic!(
|
|
|
|
"Duration value in variable {} is expected to be a number, but got {}",
|
|
|
|
env_var_name, v
|
|
|
|
)
|
|
|
|
})
|
2019-09-29 00:52:32 -05:00
|
|
|
});
|
2019-09-28 09:07:18 -05:00
|
|
|
|
2019-09-29 00:52:32 -05:00
|
|
|
// Callback to be called if the environment variable has unexpected structure.
|
|
|
|
let panic_on_incorrect_value = || {
|
2019-09-28 09:07:18 -05:00
|
|
|
panic!(
|
|
|
|
"Duration variable {} expected to have 2 numbers separated by comma, but got {}",
|
2019-09-29 00:52:32 -05:00
|
|
|
env_var_name, durations_str
|
2019-09-28 09:07:18 -05:00
|
|
|
);
|
2019-09-29 00:52:32 -05:00
|
|
|
};
|
2019-09-28 09:07:18 -05:00
|
|
|
|
2019-09-29 00:52:32 -05:00
|
|
|
let (warn, critical) = (
|
|
|
|
durations.next().unwrap_or_else(panic_on_incorrect_value),
|
|
|
|
durations.next().unwrap_or_else(panic_on_incorrect_value)
|
|
|
|
);
|
2019-09-28 09:07:18 -05:00
|
|
|
|
2019-10-09 02:03:08 -05:00
|
|
|
if warn > critical {
|
|
|
|
panic!("Test execution warn time should be less or equal to the critical time");
|
|
|
|
}
|
|
|
|
|
2019-09-28 09:07:18 -05:00
|
|
|
Some(Self::new(Duration::from_millis(warn), Duration::from_millis(critical)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Structure with parameters for calculating test execution time.
|
|
|
|
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
|
|
|
|
pub struct TestTimeOptions {
|
2019-09-29 00:52:32 -05:00
|
|
|
/// Denotes if the test critical execution time limit excess should be considered
|
|
|
|
/// a test failure.
|
2019-09-28 09:07:18 -05:00
|
|
|
pub error_on_excess: bool,
|
|
|
|
pub colored: bool,
|
|
|
|
pub unit_threshold: TimeThreshold,
|
|
|
|
pub integration_threshold: TimeThreshold,
|
|
|
|
pub doctest_threshold: TimeThreshold,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestTimeOptions {
|
|
|
|
pub fn new_from_env(error_on_excess: bool, colored: bool) -> Self {
|
|
|
|
let unit_threshold =
|
|
|
|
TimeThreshold::from_env_var(time_constants::UNIT_ENV_NAME)
|
|
|
|
.unwrap_or_else(Self::default_unit);
|
|
|
|
|
|
|
|
let integration_threshold =
|
|
|
|
TimeThreshold::from_env_var(time_constants::INTEGRATION_ENV_NAME)
|
|
|
|
.unwrap_or_else(Self::default_integration);
|
|
|
|
|
|
|
|
let doctest_threshold =
|
|
|
|
TimeThreshold::from_env_var(time_constants::DOCTEST_ENV_NAME)
|
|
|
|
.unwrap_or_else(Self::default_doctest);
|
|
|
|
|
|
|
|
Self {
|
|
|
|
error_on_excess,
|
|
|
|
colored,
|
|
|
|
unit_threshold,
|
|
|
|
integration_threshold,
|
|
|
|
doctest_threshold,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_warn(&self, test: &TestDesc, exec_time: &TestExecTime) -> bool {
|
|
|
|
exec_time.0 >= self.warn_time(test)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_critical(&self, test: &TestDesc, exec_time: &TestExecTime) -> bool {
|
|
|
|
exec_time.0 >= self.critical_time(test)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn warn_time(&self, test: &TestDesc) -> Duration {
|
|
|
|
match test.test_type {
|
|
|
|
TestType::UnitTest => self.unit_threshold.warn,
|
|
|
|
TestType::IntegrationTest => self.integration_threshold.warn,
|
|
|
|
TestType::DocTest => self.doctest_threshold.warn,
|
|
|
|
TestType::Unknown => time_constants::UNKNOWN_WARN,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn critical_time(&self, test: &TestDesc) -> Duration {
|
|
|
|
match test.test_type {
|
|
|
|
TestType::UnitTest => self.unit_threshold.critical,
|
|
|
|
TestType::IntegrationTest => self.integration_threshold.critical,
|
|
|
|
TestType::DocTest => self.doctest_threshold.critical,
|
|
|
|
TestType::Unknown => time_constants::UNKNOWN_CRITICAL,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_unit() -> TimeThreshold {
|
|
|
|
TimeThreshold::new(time_constants::UNIT_WARN, time_constants::UNIT_CRITICAL)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_integration() -> TimeThreshold {
|
|
|
|
TimeThreshold::new(time_constants::INTEGRATION_WARN, time_constants::INTEGRATION_CRITICAL)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_doctest() -> TimeThreshold {
|
|
|
|
TimeThreshold::new(time_constants::DOCTEST_WARN, time_constants::DOCTEST_CRITICAL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 00:48:59 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct TestOpts {
|
|
|
|
pub list: bool,
|
|
|
|
pub filter: Option<String>,
|
|
|
|
pub filter_exact: bool,
|
|
|
|
pub exclude_should_panic: bool,
|
|
|
|
pub run_ignored: RunIgnored,
|
|
|
|
pub run_tests: bool,
|
|
|
|
pub bench_benchmarks: bool,
|
|
|
|
pub logfile: Option<PathBuf>,
|
|
|
|
pub nocapture: bool,
|
|
|
|
pub color: ColorConfig,
|
|
|
|
pub format: OutputFormat,
|
|
|
|
pub test_threads: Option<usize>,
|
|
|
|
pub skip: Vec<String>,
|
2019-09-28 09:07:18 -05:00
|
|
|
pub time_options: Option<TestTimeOptions>,
|
2019-04-07 00:48:59 -05:00
|
|
|
pub options: Options,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Result of parsing the options.
|
|
|
|
pub type OptRes = Result<TestOpts, String>;
|
2019-09-29 00:52:32 -05:00
|
|
|
/// Result of parsing the option part.
|
|
|
|
type OptPartRes<T> = Result<Option<T>, String>;
|
2019-04-07 00:48:59 -05:00
|
|
|
|
|
|
|
fn optgroups() -> getopts::Options {
|
|
|
|
let mut opts = getopts::Options::new();
|
|
|
|
opts.optflag("", "include-ignored", "Run ignored and not ignored tests")
|
|
|
|
.optflag("", "ignored", "Run only ignored tests")
|
|
|
|
.optflag("", "exclude-should-panic", "Excludes tests marked as should_panic")
|
|
|
|
.optflag("", "test", "Run tests and not benchmarks")
|
|
|
|
.optflag("", "bench", "Run benchmarks instead of tests")
|
|
|
|
.optflag("", "list", "List all tests and benchmarks")
|
|
|
|
.optflag("h", "help", "Display this message (longer with --help)")
|
|
|
|
.optopt(
|
|
|
|
"",
|
|
|
|
"logfile",
|
|
|
|
"Write logs to the specified file instead \
|
|
|
|
of stdout",
|
|
|
|
"PATH",
|
|
|
|
)
|
|
|
|
.optflag(
|
|
|
|
"",
|
|
|
|
"nocapture",
|
|
|
|
"don't capture stdout/stderr of each \
|
|
|
|
task, allow printing directly",
|
|
|
|
)
|
|
|
|
.optopt(
|
|
|
|
"",
|
|
|
|
"test-threads",
|
|
|
|
"Number of threads used for running tests \
|
|
|
|
in parallel",
|
|
|
|
"n_threads",
|
|
|
|
)
|
|
|
|
.optmulti(
|
|
|
|
"",
|
|
|
|
"skip",
|
|
|
|
"Skip tests whose names contain FILTER (this flag can \
|
|
|
|
be used multiple times)",
|
|
|
|
"FILTER",
|
|
|
|
)
|
|
|
|
.optflag(
|
|
|
|
"q",
|
|
|
|
"quiet",
|
|
|
|
"Display one character per test instead of one line. \
|
|
|
|
Alias to --format=terse",
|
|
|
|
)
|
|
|
|
.optflag(
|
|
|
|
"",
|
|
|
|
"exact",
|
|
|
|
"Exactly match filters rather than by substring",
|
|
|
|
)
|
|
|
|
.optopt(
|
|
|
|
"",
|
|
|
|
"color",
|
|
|
|
"Configure coloring of output:
|
|
|
|
auto = colorize if stdout is a tty and tests are run on serially (default);
|
|
|
|
always = always colorize output;
|
|
|
|
never = never colorize output;",
|
|
|
|
"auto|always|never",
|
|
|
|
)
|
|
|
|
.optopt(
|
|
|
|
"",
|
|
|
|
"format",
|
|
|
|
"Configure formatting of output:
|
|
|
|
pretty = Print verbose output;
|
|
|
|
terse = Display one character per test;
|
|
|
|
json = Output a json document",
|
|
|
|
"pretty|terse|json",
|
|
|
|
)
|
2019-07-11 15:23:00 -05:00
|
|
|
.optflag(
|
|
|
|
"",
|
|
|
|
"show-output",
|
|
|
|
"Show captured stdout of successful tests"
|
|
|
|
)
|
2019-04-07 00:48:59 -05:00
|
|
|
.optopt(
|
|
|
|
"Z",
|
|
|
|
"",
|
|
|
|
"Enable nightly-only flags:
|
|
|
|
unstable-options = Allow use of experimental features",
|
|
|
|
"unstable-options",
|
2019-09-21 12:03:14 -05:00
|
|
|
)
|
2019-09-28 09:07:18 -05:00
|
|
|
.optflagopt(
|
2019-09-21 12:03:14 -05:00
|
|
|
"",
|
|
|
|
"report-time",
|
2019-09-28 09:07:18 -05:00
|
|
|
"Show execution time of each test. Awailable values:
|
|
|
|
plain = do not colorize the execution time (default);
|
|
|
|
colored = colorize output according to the `color` parameter value;
|
|
|
|
|
|
|
|
Threshold values for colorized output can be configured via
|
|
|
|
`RUST_TEST_TIME_UNIT`, `RUST_TEST_TIME_INTEGRATION` and
|
|
|
|
`RUST_TEST_TIME_DOCTEST` environment variables.
|
|
|
|
|
2019-10-09 02:03:08 -05:00
|
|
|
Expected format of environment variable is `VARIABLE=WARN_TIME,CRITICAL_TIME`.
|
|
|
|
|
2019-09-28 09:07:18 -05:00
|
|
|
Not available for --format=terse",
|
|
|
|
"plain|colored"
|
|
|
|
)
|
|
|
|
.optflag(
|
|
|
|
"",
|
2019-10-09 02:03:08 -05:00
|
|
|
"ensure-time",
|
2019-09-28 09:07:18 -05:00
|
|
|
"Treat excess of the test execution time limit as error.
|
|
|
|
|
|
|
|
Threshold values for this option can be configured via
|
|
|
|
`RUST_TEST_TIME_UNIT`, `RUST_TEST_TIME_INTEGRATION` and
|
2019-10-09 02:03:08 -05:00
|
|
|
`RUST_TEST_TIME_DOCTEST` environment variables.
|
|
|
|
|
|
|
|
Expected format of environment variable is `VARIABLE=WARN_TIME,CRITICAL_TIME`.
|
|
|
|
|
|
|
|
`CRITICAL_TIME` here means the limit that should not be exceeded by test.
|
|
|
|
"
|
2019-04-07 00:48:59 -05:00
|
|
|
);
|
|
|
|
return opts;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(binary: &str, options: &getopts::Options) {
|
|
|
|
let message = format!("Usage: {} [OPTIONS] [FILTER]", binary);
|
|
|
|
println!(
|
|
|
|
r#"{usage}
|
|
|
|
|
|
|
|
The FILTER string is tested against the name of all tests, and only those
|
|
|
|
tests whose names contain the filter are run.
|
|
|
|
|
|
|
|
By default, all tests are run in parallel. This can be altered with the
|
|
|
|
--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
|
|
|
|
environment variable to a value other than "0". Logging is not captured by default.
|
|
|
|
|
|
|
|
Test Attributes:
|
|
|
|
|
2019-07-31 18:28:43 -05:00
|
|
|
`#[test]` - Indicates a function is a test to be run. This function
|
|
|
|
takes no arguments.
|
|
|
|
`#[bench]` - Indicates a function is a benchmark to be run. This
|
|
|
|
function takes one argument (test::Bencher).
|
|
|
|
`#[should_panic]` - This function (also labeled with `#[test]`) will only pass if
|
|
|
|
the code causes a panic (an assertion failure or panic!)
|
|
|
|
A message may be provided, which the failure string must
|
|
|
|
contain: #[should_panic(expected = "foo")].
|
|
|
|
`#[ignore]` - When applied to a function which is already attributed as a
|
|
|
|
test, then the test runner will ignore these tests during
|
|
|
|
normal test runs. Running with --ignored or --include-ignored will run
|
|
|
|
these tests."#,
|
2019-04-07 00:48:59 -05:00
|
|
|
usage = options.usage(&message)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Copied from libsyntax until linkage errors are resolved. Issue #47566
|
|
|
|
fn is_nightly() -> bool {
|
|
|
|
// Whether this is a feature-staged build, i.e., on the beta or stable channel
|
|
|
|
let disable_unstable_features = option_env!("CFG_DISABLE_UNSTABLE_FEATURES").is_some();
|
|
|
|
// Whether we should enable unstable features for bootstrapping
|
|
|
|
let bootstrap = env::var("RUSTC_BOOTSTRAP").is_ok();
|
|
|
|
|
|
|
|
bootstrap || !disable_unstable_features
|
|
|
|
}
|
|
|
|
|
2019-09-28 09:07:18 -05:00
|
|
|
// Gets the option value and checks if unstable features are enabled.
|
|
|
|
macro_rules! unstable_optflag {
|
|
|
|
($matches:ident, $allow_unstable:ident, $option_name:literal) => {{
|
|
|
|
let opt = $matches.opt_present($option_name);
|
|
|
|
if !$allow_unstable && opt {
|
|
|
|
return Some(Err(format!(
|
|
|
|
"The \"{}\" flag is only accepted on the nightly compiler",
|
|
|
|
$option_name
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
opt
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2019-09-29 00:52:32 -05:00
|
|
|
// Gets the CLI options assotiated with `report-time` feature.
|
|
|
|
fn get_time_options(
|
|
|
|
matches: &getopts::Matches,
|
|
|
|
allow_unstable: bool)
|
|
|
|
-> Option<OptPartRes<TestTimeOptions>> {
|
|
|
|
let report_time = unstable_optflag!(matches, allow_unstable, "report-time");
|
|
|
|
let colored_opt_str = matches.opt_str("report-time");
|
2019-10-09 02:03:08 -05:00
|
|
|
let mut report_time_colored = report_time && colored_opt_str == Some("colored".into());
|
|
|
|
let ensure_test_time = unstable_optflag!(matches, allow_unstable, "ensure-time");
|
2019-09-29 00:52:32 -05:00
|
|
|
|
|
|
|
// If `ensure-test-time` option is provided, time output is enforced,
|
|
|
|
// so user won't be confused if any of tests will silently fail.
|
|
|
|
let options = if report_time || ensure_test_time {
|
2019-10-09 02:03:08 -05:00
|
|
|
if ensure_test_time && !report_time {
|
|
|
|
report_time_colored = true;
|
|
|
|
}
|
2019-09-29 00:52:32 -05:00
|
|
|
Some(TestTimeOptions::new_from_env(ensure_test_time, report_time_colored))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(Ok(options))
|
|
|
|
}
|
|
|
|
|
2019-04-07 00:48:59 -05:00
|
|
|
// Parses command line arguments into test options
|
|
|
|
pub fn parse_opts(args: &[String]) -> Option<OptRes> {
|
|
|
|
let mut allow_unstable = false;
|
|
|
|
let opts = optgroups();
|
|
|
|
let args = args.get(1..).unwrap_or(args);
|
|
|
|
let matches = match opts.parse(args) {
|
|
|
|
Ok(m) => m,
|
|
|
|
Err(f) => return Some(Err(f.to_string())),
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(opt) = matches.opt_str("Z") {
|
|
|
|
if !is_nightly() {
|
|
|
|
return Some(Err(
|
|
|
|
"the option `Z` is only accepted on the nightly compiler".into(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
match &*opt {
|
|
|
|
"unstable-options" => {
|
|
|
|
allow_unstable = true;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return Some(Err("Unrecognized option to `Z`".into()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if matches.opt_present("h") {
|
|
|
|
usage(&args[0], &opts);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let filter = if !matches.free.is_empty() {
|
|
|
|
Some(matches.free[0].clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2019-09-28 09:07:18 -05:00
|
|
|
let exclude_should_panic = unstable_optflag!(matches, allow_unstable, "exclude-should-panic");
|
2019-04-07 00:48:59 -05:00
|
|
|
|
2019-09-28 09:07:18 -05:00
|
|
|
let include_ignored = unstable_optflag!(matches, allow_unstable, "include-ignored");
|
2019-09-22 15:20:30 -05:00
|
|
|
|
2019-04-07 00:48:59 -05:00
|
|
|
let run_ignored = match (include_ignored, matches.opt_present("ignored")) {
|
|
|
|
(true, true) => {
|
|
|
|
return Some(Err(
|
|
|
|
"the options --include-ignored and --ignored are mutually exclusive".into(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
(true, false) => RunIgnored::Yes,
|
|
|
|
(false, true) => RunIgnored::Only,
|
|
|
|
(false, false) => RunIgnored::No,
|
|
|
|
};
|
|
|
|
let quiet = matches.opt_present("quiet");
|
|
|
|
let exact = matches.opt_present("exact");
|
|
|
|
let list = matches.opt_present("list");
|
|
|
|
|
|
|
|
let logfile = matches.opt_str("logfile");
|
|
|
|
let logfile = logfile.map(|s| PathBuf::from(&s));
|
|
|
|
|
|
|
|
let bench_benchmarks = matches.opt_present("bench");
|
|
|
|
let run_tests = !bench_benchmarks || matches.opt_present("test");
|
|
|
|
|
|
|
|
let mut nocapture = matches.opt_present("nocapture");
|
|
|
|
if !nocapture {
|
|
|
|
nocapture = match env::var("RUST_TEST_NOCAPTURE") {
|
|
|
|
Ok(val) => &val != "0",
|
|
|
|
Err(_) => false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-29 00:52:32 -05:00
|
|
|
let time_options = match get_time_options(&matches, allow_unstable) {
|
|
|
|
Some(Ok(val)) => val,
|
|
|
|
Some(Err(e)) => return Some(Err(e)),
|
2019-10-09 02:03:08 -05:00
|
|
|
None => panic!("Unexpected output from `get_time_options`"),
|
2019-09-28 09:07:18 -05:00
|
|
|
};
|
|
|
|
|
2019-04-07 00:48:59 -05:00
|
|
|
let test_threads = match matches.opt_str("test-threads") {
|
|
|
|
Some(n_str) => match n_str.parse::<usize>() {
|
|
|
|
Ok(0) => return Some(Err("argument for --test-threads must not be 0".to_string())),
|
|
|
|
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,
|
|
|
|
Some("never") => NeverColor,
|
|
|
|
|
|
|
|
Some(v) => {
|
|
|
|
return Some(Err(format!(
|
|
|
|
"argument for --color must be auto, always, or never (was \
|
|
|
|
{})",
|
|
|
|
v
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let format = match matches.opt_str("format").as_ref().map(|s| &**s) {
|
|
|
|
None if quiet => OutputFormat::Terse,
|
|
|
|
Some("pretty") | None => OutputFormat::Pretty,
|
|
|
|
Some("terse") => OutputFormat::Terse,
|
|
|
|
Some("json") => {
|
|
|
|
if !allow_unstable {
|
|
|
|
return Some(Err(
|
|
|
|
"The \"json\" format is only accepted on the nightly compiler".into(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
OutputFormat::Json
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(v) => {
|
|
|
|
return Some(Err(format!(
|
|
|
|
"argument for --format must be pretty, terse, or json (was \
|
|
|
|
{})",
|
|
|
|
v
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let test_opts = TestOpts {
|
|
|
|
list,
|
|
|
|
filter,
|
|
|
|
filter_exact: exact,
|
|
|
|
exclude_should_panic,
|
|
|
|
run_ignored,
|
|
|
|
run_tests,
|
|
|
|
bench_benchmarks,
|
|
|
|
logfile,
|
|
|
|
nocapture,
|
|
|
|
color,
|
|
|
|
format,
|
|
|
|
test_threads,
|
|
|
|
skip: matches.opt_strs("skip"),
|
2019-09-28 09:07:18 -05:00
|
|
|
time_options,
|
2019-07-11 15:23:00 -05:00
|
|
|
options: Options::new().display_output(matches.opt_present("show-output")),
|
2019-04-07 00:48:59 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
Some(Ok(test_opts))
|
|
|
|
}
|
|
|
|
|
2019-09-28 09:07:18 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2019-04-07 00:48:59 -05:00
|
|
|
pub struct BenchSamples {
|
|
|
|
ns_iter_summ: stats::Summary,
|
|
|
|
mb_s: usize,
|
|
|
|
}
|
|
|
|
|
2019-09-28 09:07:18 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2019-04-07 00:48:59 -05:00
|
|
|
pub enum TestResult {
|
|
|
|
TrOk,
|
|
|
|
TrFailed,
|
|
|
|
TrFailedMsg(String),
|
|
|
|
TrIgnored,
|
|
|
|
TrAllowedFail,
|
|
|
|
TrBench(BenchSamples),
|
2019-09-28 09:07:18 -05:00
|
|
|
TrTimedFail,
|
2019-04-07 00:48:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl Send for TestResult {}
|
|
|
|
|
2019-09-21 12:03:14 -05:00
|
|
|
/// The meassured execution time of a unit test.
|
|
|
|
#[derive(Clone, PartialEq)]
|
|
|
|
pub struct TestExecTime(Duration);
|
|
|
|
|
|
|
|
impl fmt::Display for TestExecTime {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{:.3}s", self.0.as_secs_f64())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 00:48:59 -05:00
|
|
|
enum OutputLocation<T> {
|
|
|
|
Pretty(Box<term::StdoutTerminal>),
|
|
|
|
Raw(T),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Write> Write for OutputLocation<T> {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
|
|
|
match *self {
|
|
|
|
Pretty(ref mut term) => term.write(buf),
|
|
|
|
Raw(ref mut stdout) => stdout.write(buf),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
match *self {
|
|
|
|
Pretty(ref mut term) => term.flush(),
|
|
|
|
Raw(ref mut stdout) => stdout.flush(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ConsoleTestState {
|
|
|
|
log_out: Option<File>,
|
|
|
|
total: usize,
|
|
|
|
passed: usize,
|
|
|
|
failed: usize,
|
|
|
|
ignored: usize,
|
|
|
|
allowed_fail: usize,
|
|
|
|
filtered_out: usize,
|
|
|
|
measured: usize,
|
|
|
|
metrics: MetricMap,
|
|
|
|
failures: Vec<(TestDesc, Vec<u8>)>,
|
|
|
|
not_failures: Vec<(TestDesc, Vec<u8>)>,
|
2019-09-28 09:07:18 -05:00
|
|
|
time_failures: Vec<(TestDesc, Vec<u8>)>,
|
2019-04-07 00:48:59 -05:00
|
|
|
options: Options,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConsoleTestState {
|
|
|
|
pub fn new(opts: &TestOpts) -> io::Result<ConsoleTestState> {
|
|
|
|
let log_out = match opts.logfile {
|
|
|
|
Some(ref path) => Some(File::create(path)?),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(ConsoleTestState {
|
|
|
|
log_out,
|
|
|
|
total: 0,
|
|
|
|
passed: 0,
|
|
|
|
failed: 0,
|
|
|
|
ignored: 0,
|
|
|
|
allowed_fail: 0,
|
|
|
|
filtered_out: 0,
|
|
|
|
measured: 0,
|
|
|
|
metrics: MetricMap::new(),
|
|
|
|
failures: Vec::new(),
|
|
|
|
not_failures: Vec::new(),
|
2019-09-28 09:07:18 -05:00
|
|
|
time_failures: Vec::new(),
|
2019-04-07 00:48:59 -05:00
|
|
|
options: opts.options,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-21 12:03:14 -05:00
|
|
|
pub fn write_log<F, S>(
|
|
|
|
&mut self,
|
|
|
|
msg: F,
|
|
|
|
) -> io::Result<()>
|
|
|
|
where
|
|
|
|
S: AsRef<str>,
|
|
|
|
F: FnOnce() -> S,
|
|
|
|
{
|
2019-04-07 00:48:59 -05:00
|
|
|
match self.log_out {
|
|
|
|
None => Ok(()),
|
2019-09-21 12:03:14 -05:00
|
|
|
Some(ref mut o) => {
|
|
|
|
let msg = msg();
|
|
|
|
let msg = msg.as_ref();
|
|
|
|
o.write_all(msg.as_bytes())
|
|
|
|
},
|
2019-04-07 00:48:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-21 12:03:14 -05:00
|
|
|
pub fn write_log_result(&mut self,test: &TestDesc,
|
|
|
|
result: &TestResult,
|
|
|
|
exec_time: Option<&TestExecTime>,
|
|
|
|
) -> io::Result<()> {
|
|
|
|
self.write_log(|| format!(
|
|
|
|
"{} {}",
|
2019-04-07 00:48:59 -05:00
|
|
|
match *result {
|
|
|
|
TrOk => "ok".to_owned(),
|
|
|
|
TrFailed => "failed".to_owned(),
|
|
|
|
TrFailedMsg(ref msg) => format!("failed: {}", msg),
|
|
|
|
TrIgnored => "ignored".to_owned(),
|
|
|
|
TrAllowedFail => "failed (allowed)".to_owned(),
|
|
|
|
TrBench(ref bs) => fmt_bench_samples(bs),
|
2019-09-28 09:07:18 -05:00
|
|
|
TrTimedFail => "failed (time limit exceeded)".to_owned(),
|
2019-04-07 00:48:59 -05:00
|
|
|
},
|
2019-09-21 12:03:14 -05:00
|
|
|
test.name,
|
|
|
|
))?;
|
|
|
|
if let Some(exec_time) = exec_time {
|
2019-09-28 09:07:18 -05:00
|
|
|
self.write_log(|| format!(" <{}>", exec_time))?;
|
2019-09-21 12:03:14 -05:00
|
|
|
}
|
|
|
|
self.write_log(|| "\n")
|
2019-04-07 00:48:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn current_test_count(&self) -> usize {
|
|
|
|
self.passed + self.failed + self.ignored + self.measured + self.allowed_fail
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format a number with thousands separators
|
|
|
|
fn fmt_thousands_sep(mut n: usize, sep: char) -> String {
|
|
|
|
use std::fmt::Write;
|
|
|
|
let mut output = String::new();
|
|
|
|
let mut trailing = false;
|
|
|
|
for &pow in &[9, 6, 3, 0] {
|
|
|
|
let base = 10_usize.pow(pow);
|
|
|
|
if pow == 0 || trailing || n / base != 0 {
|
|
|
|
if !trailing {
|
|
|
|
output.write_fmt(format_args!("{}", n / base)).unwrap();
|
|
|
|
} else {
|
|
|
|
output.write_fmt(format_args!("{:03}", n / base)).unwrap();
|
|
|
|
}
|
|
|
|
if pow != 0 {
|
|
|
|
output.push(sep);
|
|
|
|
}
|
|
|
|
trailing = true;
|
|
|
|
}
|
|
|
|
n %= base;
|
|
|
|
}
|
|
|
|
|
|
|
|
output
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fmt_bench_samples(bs: &BenchSamples) -> String {
|
|
|
|
use std::fmt::Write;
|
|
|
|
let mut output = String::new();
|
|
|
|
|
|
|
|
let median = bs.ns_iter_summ.median as usize;
|
|
|
|
let deviation = (bs.ns_iter_summ.max - bs.ns_iter_summ.min) as usize;
|
|
|
|
|
|
|
|
output
|
|
|
|
.write_fmt(format_args!(
|
|
|
|
"{:>11} ns/iter (+/- {})",
|
|
|
|
fmt_thousands_sep(median, ','),
|
|
|
|
fmt_thousands_sep(deviation, ',')
|
|
|
|
))
|
|
|
|
.unwrap();
|
|
|
|
if bs.mb_s != 0 {
|
|
|
|
output
|
|
|
|
.write_fmt(format_args!(" = {} MB/s", bs.mb_s))
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
output
|
|
|
|
}
|
|
|
|
|
|
|
|
// List the tests to console, and optionally to logfile. Filters are honored.
|
|
|
|
pub fn list_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Result<()> {
|
|
|
|
let mut output = match term::stdout() {
|
|
|
|
None => Raw(io::stdout()),
|
|
|
|
Some(t) => Pretty(t),
|
|
|
|
};
|
|
|
|
|
|
|
|
let quiet = opts.format == OutputFormat::Terse;
|
|
|
|
let mut st = ConsoleTestState::new(opts)?;
|
|
|
|
|
|
|
|
let mut ntest = 0;
|
|
|
|
let mut nbench = 0;
|
|
|
|
|
|
|
|
for test in filter_tests(&opts, tests) {
|
|
|
|
use crate::TestFn::*;
|
|
|
|
|
|
|
|
let TestDescAndFn {
|
|
|
|
desc: TestDesc { name, .. },
|
|
|
|
testfn,
|
|
|
|
} = test;
|
|
|
|
|
|
|
|
let fntype = match testfn {
|
|
|
|
StaticTestFn(..) | DynTestFn(..) => {
|
|
|
|
ntest += 1;
|
|
|
|
"test"
|
|
|
|
}
|
|
|
|
StaticBenchFn(..) | DynBenchFn(..) => {
|
|
|
|
nbench += 1;
|
|
|
|
"benchmark"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
writeln!(output, "{}: {}", name, fntype)?;
|
2019-09-21 12:03:14 -05:00
|
|
|
st.write_log(|| format!("{} {}\n", fntype, name))?;
|
2019-04-07 00:48:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn plural(count: u32, s: &str) -> String {
|
|
|
|
match count {
|
|
|
|
1 => format!("{} {}", 1, s),
|
|
|
|
n => format!("{} {}s", n, s),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !quiet {
|
|
|
|
if ntest != 0 || nbench != 0 {
|
|
|
|
writeln!(output, "")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
writeln!(
|
|
|
|
output,
|
|
|
|
"{}, {}",
|
|
|
|
plural(ntest, "test"),
|
|
|
|
plural(nbench, "benchmark")
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
// A simple console test runner
|
|
|
|
pub fn run_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Result<bool> {
|
|
|
|
fn callback(
|
|
|
|
event: &TestEvent,
|
|
|
|
st: &mut ConsoleTestState,
|
|
|
|
out: &mut dyn OutputFormatter,
|
|
|
|
) -> io::Result<()> {
|
|
|
|
match (*event).clone() {
|
|
|
|
TeFiltered(ref filtered_tests) => {
|
|
|
|
st.total = filtered_tests.len();
|
|
|
|
out.write_run_start(filtered_tests.len())
|
|
|
|
}
|
|
|
|
TeFilteredOut(filtered_out) => Ok(st.filtered_out = filtered_out),
|
|
|
|
TeWait(ref test) => out.write_test_start(test),
|
|
|
|
TeTimeout(ref test) => out.write_timeout(test),
|
2019-09-21 12:03:14 -05:00
|
|
|
TeResult(test, result, exec_time, stdout) => {
|
|
|
|
st.write_log_result(&test, &result, exec_time.as_ref())?;
|
|
|
|
out.write_result(&test, &result, exec_time.as_ref(), &*stdout, &st)?;
|
2019-04-07 00:48:59 -05:00
|
|
|
match result {
|
|
|
|
TrOk => {
|
|
|
|
st.passed += 1;
|
|
|
|
st.not_failures.push((test, stdout));
|
|
|
|
}
|
|
|
|
TrIgnored => st.ignored += 1,
|
|
|
|
TrAllowedFail => st.allowed_fail += 1,
|
|
|
|
TrBench(bs) => {
|
|
|
|
st.metrics.insert_metric(
|
|
|
|
test.name.as_slice(),
|
|
|
|
bs.ns_iter_summ.median,
|
|
|
|
bs.ns_iter_summ.max - bs.ns_iter_summ.min,
|
|
|
|
);
|
|
|
|
st.measured += 1
|
|
|
|
}
|
|
|
|
TrFailed => {
|
|
|
|
st.failed += 1;
|
|
|
|
st.failures.push((test, stdout));
|
|
|
|
}
|
|
|
|
TrFailedMsg(msg) => {
|
|
|
|
st.failed += 1;
|
|
|
|
let mut stdout = stdout;
|
|
|
|
stdout.extend_from_slice(format!("note: {}", msg).as_bytes());
|
|
|
|
st.failures.push((test, stdout));
|
|
|
|
}
|
2019-09-28 09:07:18 -05:00
|
|
|
TrTimedFail => {
|
|
|
|
st.failed += 1;
|
|
|
|
st.time_failures.push((test, stdout));
|
|
|
|
}
|
2019-04-07 00:48:59 -05:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let output = match term::stdout() {
|
|
|
|
None => Raw(io::stdout()),
|
|
|
|
Some(t) => Pretty(t),
|
|
|
|
};
|
|
|
|
|
|
|
|
let max_name_len = tests
|
|
|
|
.iter()
|
|
|
|
.max_by_key(|t| len_if_padded(*t))
|
|
|
|
.map(|t| t.desc.name.as_slice().len())
|
|
|
|
.unwrap_or(0);
|
|
|
|
|
|
|
|
let is_multithreaded = opts.test_threads.unwrap_or_else(get_concurrency) > 1;
|
|
|
|
|
|
|
|
let mut out: Box<dyn OutputFormatter> = match opts.format {
|
|
|
|
OutputFormat::Pretty => Box::new(PrettyFormatter::new(
|
|
|
|
output,
|
|
|
|
use_color(opts),
|
|
|
|
max_name_len,
|
|
|
|
is_multithreaded,
|
2019-09-28 09:07:18 -05:00
|
|
|
opts.time_options,
|
2019-04-07 00:48:59 -05:00
|
|
|
)),
|
|
|
|
OutputFormat::Terse => Box::new(TerseFormatter::new(
|
|
|
|
output,
|
|
|
|
use_color(opts),
|
|
|
|
max_name_len,
|
|
|
|
is_multithreaded,
|
|
|
|
)),
|
|
|
|
OutputFormat::Json => Box::new(JsonFormatter::new(output)),
|
|
|
|
};
|
|
|
|
let mut st = ConsoleTestState::new(opts)?;
|
|
|
|
fn len_if_padded(t: &TestDescAndFn) -> usize {
|
|
|
|
match t.testfn.padding() {
|
|
|
|
PadNone => 0,
|
|
|
|
PadOnRight => t.desc.name.as_slice().len(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
run_tests(opts, tests, |x| callback(&x, &mut st, &mut *out))?;
|
|
|
|
|
|
|
|
assert!(st.current_test_count() == st.total);
|
|
|
|
|
|
|
|
return out.write_run_finish(&st);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn use_color(opts: &TestOpts) -> bool {
|
|
|
|
match opts.color {
|
|
|
|
AutoColor => !opts.nocapture && stdout_isatty(),
|
|
|
|
AlwaysColor => true,
|
|
|
|
NeverColor => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(any(
|
|
|
|
target_os = "cloudabi",
|
|
|
|
all(target_arch = "wasm32", not(target_os = "emscripten")),
|
|
|
|
all(target_vendor = "fortanix", target_env = "sgx")
|
|
|
|
))]
|
|
|
|
fn stdout_isatty() -> bool {
|
2019-08-20 19:15:11 -05:00
|
|
|
// FIXME: Implement isatty on SGX
|
2019-04-07 00:48:59 -05:00
|
|
|
false
|
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn stdout_isatty() -> bool {
|
|
|
|
unsafe { libc::isatty(libc::STDOUT_FILENO) != 0 }
|
|
|
|
}
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn stdout_isatty() -> bool {
|
|
|
|
type DWORD = u32;
|
|
|
|
type BOOL = i32;
|
|
|
|
type HANDLE = *mut u8;
|
|
|
|
type LPDWORD = *mut u32;
|
|
|
|
const STD_OUTPUT_HANDLE: DWORD = -11i32 as DWORD;
|
|
|
|
extern "system" {
|
|
|
|
fn GetStdHandle(which: DWORD) -> HANDLE;
|
|
|
|
fn GetConsoleMode(hConsoleHandle: HANDLE, lpMode: LPDWORD) -> BOOL;
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
let handle = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
let mut out = 0;
|
|
|
|
GetConsoleMode(handle, &mut out) != 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum TestEvent {
|
|
|
|
TeFiltered(Vec<TestDesc>),
|
|
|
|
TeWait(TestDesc),
|
2019-09-21 12:03:14 -05:00
|
|
|
TeResult(TestDesc, TestResult, Option<TestExecTime>, Vec<u8>),
|
2019-04-07 00:48:59 -05:00
|
|
|
TeTimeout(TestDesc),
|
|
|
|
TeFilteredOut(usize),
|
|
|
|
}
|
|
|
|
|
2019-09-21 12:03:14 -05:00
|
|
|
pub type MonitorMsg = (TestDesc, TestResult, Option<TestExecTime>, Vec<u8>);
|
2019-03-04 11:17:41 -06:00
|
|
|
|
2019-04-07 00:48:59 -05:00
|
|
|
struct Sink(Arc<Mutex<Vec<u8>>>);
|
|
|
|
impl Write for Sink {
|
|
|
|
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
|
|
|
|
Write::write(&mut *self.0.lock().unwrap(), data)
|
|
|
|
}
|
|
|
|
fn flush(&mut self) -> io::Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-11 19:13:34 -05:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum RunStrategy {
|
|
|
|
/// Runs the test in the current process, and sends the result back over the
|
|
|
|
/// supplied channel.
|
|
|
|
InProcess,
|
|
|
|
|
|
|
|
/// Spawns a subprocess to run the test, and sends the result back over the
|
2019-10-02 07:24:59 -05:00
|
|
|
/// supplied channel. Requires `argv[0]` to exist and point to the binary
|
2019-09-11 19:13:34 -05:00
|
|
|
/// that's currently running.
|
|
|
|
SpawnPrimary,
|
|
|
|
}
|
|
|
|
|
2019-04-07 00:48:59 -05:00
|
|
|
pub fn run_tests<F>(opts: &TestOpts, tests: Vec<TestDescAndFn>, mut callback: F) -> io::Result<()>
|
|
|
|
where
|
|
|
|
F: FnMut(TestEvent) -> io::Result<()>,
|
|
|
|
{
|
|
|
|
use std::collections::{self, HashMap};
|
|
|
|
use std::hash::BuildHasherDefault;
|
|
|
|
use std::sync::mpsc::RecvTimeoutError;
|
|
|
|
// Use a deterministic hasher
|
|
|
|
type TestMap =
|
|
|
|
HashMap<TestDesc, Instant, BuildHasherDefault<collections::hash_map::DefaultHasher>>;
|
|
|
|
|
|
|
|
let tests_len = tests.len();
|
|
|
|
|
|
|
|
let mut filtered_tests = filter_tests(opts, tests);
|
|
|
|
if !opts.bench_benchmarks {
|
|
|
|
filtered_tests = convert_benchmarks_to_tests(filtered_tests);
|
|
|
|
}
|
|
|
|
|
|
|
|
let filtered_tests = {
|
|
|
|
let mut filtered_tests = filtered_tests;
|
|
|
|
for test in filtered_tests.iter_mut() {
|
|
|
|
test.desc.name = test.desc.name.with_padding(test.testfn.padding());
|
|
|
|
}
|
|
|
|
|
|
|
|
filtered_tests
|
|
|
|
};
|
|
|
|
|
|
|
|
let filtered_out = tests_len - filtered_tests.len();
|
|
|
|
callback(TeFilteredOut(filtered_out))?;
|
|
|
|
|
|
|
|
let filtered_descs = filtered_tests.iter().map(|t| t.desc.clone()).collect();
|
|
|
|
|
|
|
|
callback(TeFiltered(filtered_descs))?;
|
|
|
|
|
|
|
|
let (filtered_tests, filtered_benchs): (Vec<_>, _) =
|
|
|
|
filtered_tests.into_iter().partition(|e| match e.testfn {
|
|
|
|
StaticTestFn(_) | DynTestFn(_) => true,
|
|
|
|
_ => false,
|
|
|
|
});
|
|
|
|
|
|
|
|
let concurrency = opts.test_threads.unwrap_or_else(get_concurrency);
|
|
|
|
|
|
|
|
let mut remaining = filtered_tests;
|
|
|
|
remaining.reverse();
|
|
|
|
let mut pending = 0;
|
|
|
|
|
|
|
|
let (tx, rx) = channel::<MonitorMsg>();
|
2019-09-11 19:13:34 -05:00
|
|
|
let run_strategy = if opts.options.panic_abort {
|
|
|
|
RunStrategy::SpawnPrimary
|
|
|
|
} else {
|
|
|
|
RunStrategy::InProcess
|
|
|
|
};
|
2019-04-07 00:48:59 -05:00
|
|
|
|
|
|
|
let mut running_tests: TestMap = HashMap::default();
|
|
|
|
|
|
|
|
fn get_timed_out_tests(running_tests: &mut TestMap) -> Vec<TestDesc> {
|
|
|
|
let now = Instant::now();
|
|
|
|
let timed_out = running_tests
|
|
|
|
.iter()
|
|
|
|
.filter_map(|(desc, timeout)| {
|
|
|
|
if &now >= timeout {
|
|
|
|
Some(desc.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
for test in &timed_out {
|
|
|
|
running_tests.remove(test);
|
|
|
|
}
|
|
|
|
timed_out
|
|
|
|
};
|
|
|
|
|
|
|
|
fn calc_timeout(running_tests: &TestMap) -> Option<Duration> {
|
|
|
|
running_tests.values().min().map(|next_timeout| {
|
|
|
|
let now = Instant::now();
|
|
|
|
if *next_timeout >= now {
|
|
|
|
*next_timeout - now
|
|
|
|
} else {
|
|
|
|
Duration::new(0, 0)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
if concurrency == 1 {
|
|
|
|
while !remaining.is_empty() {
|
|
|
|
let test = remaining.pop().unwrap();
|
|
|
|
callback(TeWait(test.desc.clone()))?;
|
2019-09-11 19:13:34 -05:00
|
|
|
run_test(opts, !opts.run_tests, test, run_strategy, tx.clone(), Concurrent::No);
|
2019-09-21 12:03:14 -05:00
|
|
|
let (test, result, exec_time, stdout) = rx.recv().unwrap();
|
|
|
|
callback(TeResult(test, result, exec_time, stdout))?;
|
2019-04-07 00:48:59 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while pending > 0 || !remaining.is_empty() {
|
|
|
|
while pending < concurrency && !remaining.is_empty() {
|
|
|
|
let test = remaining.pop().unwrap();
|
|
|
|
let timeout = Instant::now() + Duration::from_secs(TEST_WARN_TIMEOUT_S);
|
|
|
|
running_tests.insert(test.desc.clone(), timeout);
|
|
|
|
callback(TeWait(test.desc.clone()))?; //here no pad
|
2019-09-11 19:13:34 -05:00
|
|
|
run_test(opts, !opts.run_tests, test, run_strategy, tx.clone(), Concurrent::Yes);
|
2019-04-07 00:48:59 -05:00
|
|
|
pending += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut res;
|
|
|
|
loop {
|
|
|
|
if let Some(timeout) = calc_timeout(&running_tests) {
|
|
|
|
res = rx.recv_timeout(timeout);
|
|
|
|
for test in get_timed_out_tests(&mut running_tests) {
|
|
|
|
callback(TeTimeout(test))?;
|
|
|
|
}
|
|
|
|
if res != Err(RecvTimeoutError::Timeout) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res = rx.recv().map_err(|_| RecvTimeoutError::Disconnected);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-21 12:03:14 -05:00
|
|
|
let (desc, result, exec_time, stdout) = res.unwrap();
|
2019-04-07 00:48:59 -05:00
|
|
|
running_tests.remove(&desc);
|
|
|
|
|
2019-09-21 12:03:14 -05:00
|
|
|
callback(TeResult(desc, result, exec_time, stdout))?;
|
2019-04-07 00:48:59 -05:00
|
|
|
pending -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.bench_benchmarks {
|
|
|
|
// All benchmarks run at the end, in serial.
|
|
|
|
for b in filtered_benchs {
|
|
|
|
callback(TeWait(b.desc.clone()))?;
|
2019-09-11 19:13:34 -05:00
|
|
|
run_test(opts, false, b, run_strategy, tx.clone(), Concurrent::No);
|
2019-09-21 12:03:14 -05:00
|
|
|
let (test, result, exec_time, stdout) = rx.recv().unwrap();
|
|
|
|
callback(TeResult(test, result, exec_time, stdout))?;
|
2019-04-07 00:48:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
fn get_concurrency() -> usize {
|
|
|
|
return match env::var("RUST_TEST_THREADS") {
|
|
|
|
Ok(s) => {
|
|
|
|
let opt_n: Option<usize> = s.parse().ok();
|
|
|
|
match opt_n {
|
|
|
|
Some(n) if n > 0 => n,
|
|
|
|
_ => panic!(
|
|
|
|
"RUST_TEST_THREADS is `{}`, should be a positive integer.",
|
|
|
|
s
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(..) => num_cpus(),
|
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
#[allow(nonstandard_style)]
|
|
|
|
fn num_cpus() -> usize {
|
|
|
|
#[repr(C)]
|
|
|
|
struct SYSTEM_INFO {
|
|
|
|
wProcessorArchitecture: u16,
|
|
|
|
wReserved: u16,
|
|
|
|
dwPageSize: u32,
|
|
|
|
lpMinimumApplicationAddress: *mut u8,
|
|
|
|
lpMaximumApplicationAddress: *mut u8,
|
|
|
|
dwActiveProcessorMask: *mut u8,
|
|
|
|
dwNumberOfProcessors: u32,
|
|
|
|
dwProcessorType: u32,
|
|
|
|
dwAllocationGranularity: u32,
|
|
|
|
wProcessorLevel: u16,
|
|
|
|
wProcessorRevision: u16,
|
|
|
|
}
|
|
|
|
extern "system" {
|
|
|
|
fn GetSystemInfo(info: *mut SYSTEM_INFO) -> i32;
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
let mut sysinfo = std::mem::zeroed();
|
|
|
|
GetSystemInfo(&mut sysinfo);
|
|
|
|
sysinfo.dwNumberOfProcessors as usize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 01:57:53 -05:00
|
|
|
#[cfg(target_os = "vxworks")]
|
|
|
|
fn num_cpus() -> usize {
|
|
|
|
// FIXME: Implement num_cpus on vxWorks
|
|
|
|
1
|
|
|
|
}
|
|
|
|
|
2019-08-21 12:16:52 -05:00
|
|
|
#[cfg(target_os = "redox")]
|
|
|
|
fn num_cpus() -> usize {
|
|
|
|
// FIXME: Implement num_cpus on Redox
|
|
|
|
1
|
|
|
|
}
|
|
|
|
|
2019-04-07 00:48:59 -05:00
|
|
|
#[cfg(any(
|
|
|
|
all(target_arch = "wasm32", not(target_os = "emscripten")),
|
|
|
|
all(target_vendor = "fortanix", target_env = "sgx")
|
|
|
|
))]
|
|
|
|
fn num_cpus() -> usize {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(any(
|
|
|
|
target_os = "android",
|
|
|
|
target_os = "cloudabi",
|
|
|
|
target_os = "emscripten",
|
|
|
|
target_os = "fuchsia",
|
|
|
|
target_os = "ios",
|
|
|
|
target_os = "linux",
|
|
|
|
target_os = "macos",
|
2019-08-20 19:18:05 -05:00
|
|
|
target_os = "solaris",
|
2019-04-07 00:48:59 -05:00
|
|
|
))]
|
|
|
|
fn num_cpus() -> usize {
|
|
|
|
unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) as usize }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(any(
|
|
|
|
target_os = "freebsd",
|
|
|
|
target_os = "dragonfly",
|
|
|
|
target_os = "netbsd"
|
|
|
|
))]
|
|
|
|
fn num_cpus() -> usize {
|
|
|
|
use std::ptr;
|
|
|
|
|
|
|
|
let mut cpus: libc::c_uint = 0;
|
|
|
|
let mut cpus_size = std::mem::size_of_val(&cpus);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
cpus = libc::sysconf(libc::_SC_NPROCESSORS_ONLN) as libc::c_uint;
|
|
|
|
}
|
|
|
|
if cpus < 1 {
|
|
|
|
let mut mib = [libc::CTL_HW, libc::HW_NCPU, 0, 0];
|
|
|
|
unsafe {
|
|
|
|
libc::sysctl(
|
|
|
|
mib.as_mut_ptr(),
|
|
|
|
2,
|
|
|
|
&mut cpus as *mut _ as *mut _,
|
|
|
|
&mut cpus_size as *mut _ as *mut _,
|
|
|
|
ptr::null_mut(),
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if cpus < 1 {
|
|
|
|
cpus = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cpus as usize
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "openbsd")]
|
|
|
|
fn num_cpus() -> usize {
|
|
|
|
use std::ptr;
|
|
|
|
|
|
|
|
let mut cpus: libc::c_uint = 0;
|
|
|
|
let mut cpus_size = std::mem::size_of_val(&cpus);
|
|
|
|
let mut mib = [libc::CTL_HW, libc::HW_NCPU, 0, 0];
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
libc::sysctl(
|
|
|
|
mib.as_mut_ptr(),
|
|
|
|
2,
|
|
|
|
&mut cpus as *mut _ as *mut _,
|
|
|
|
&mut cpus_size as *mut _ as *mut _,
|
|
|
|
ptr::null_mut(),
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if cpus < 1 {
|
|
|
|
cpus = 1;
|
|
|
|
}
|
|
|
|
cpus as usize
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "haiku")]
|
|
|
|
fn num_cpus() -> usize {
|
|
|
|
// FIXME: implement
|
|
|
|
1
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "l4re")]
|
|
|
|
fn num_cpus() -> usize {
|
|
|
|
// FIXME: implement
|
|
|
|
1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn filter_tests(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> Vec<TestDescAndFn> {
|
|
|
|
let mut filtered = tests;
|
|
|
|
let matches_filter = |test: &TestDescAndFn, filter: &str| {
|
|
|
|
let test_name = test.desc.name.as_slice();
|
|
|
|
|
|
|
|
match opts.filter_exact {
|
|
|
|
true => test_name == filter,
|
|
|
|
false => test_name.contains(filter),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Remove tests that don't match the test filter
|
|
|
|
if let Some(ref filter) = opts.filter {
|
|
|
|
filtered.retain(|test| matches_filter(test, filter));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip tests that match any of the skip filters
|
|
|
|
filtered.retain(|test| !opts.skip.iter().any(|sf| matches_filter(test, sf)));
|
|
|
|
|
|
|
|
// Excludes #[should_panic] tests
|
|
|
|
if opts.exclude_should_panic {
|
|
|
|
filtered.retain(|test| test.desc.should_panic == ShouldPanic::No);
|
|
|
|
}
|
|
|
|
|
|
|
|
// maybe unignore tests
|
|
|
|
match opts.run_ignored {
|
|
|
|
RunIgnored::Yes => {
|
|
|
|
filtered
|
|
|
|
.iter_mut()
|
|
|
|
.for_each(|test| test.desc.ignore = false);
|
|
|
|
}
|
|
|
|
RunIgnored::Only => {
|
|
|
|
filtered.retain(|test| test.desc.ignore);
|
|
|
|
filtered
|
|
|
|
.iter_mut()
|
|
|
|
.for_each(|test| test.desc.ignore = false);
|
|
|
|
}
|
|
|
|
RunIgnored::No => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the tests alphabetically
|
|
|
|
filtered.sort_by(|t1, t2| t1.desc.name.as_slice().cmp(t2.desc.name.as_slice()));
|
|
|
|
|
|
|
|
filtered
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn convert_benchmarks_to_tests(tests: Vec<TestDescAndFn>) -> Vec<TestDescAndFn> {
|
|
|
|
// convert benchmarks to tests, if we're not benchmarking them
|
|
|
|
tests
|
|
|
|
.into_iter()
|
|
|
|
.map(|x| {
|
|
|
|
let testfn = match x.testfn {
|
|
|
|
DynBenchFn(bench) => DynTestFn(Box::new(move || {
|
|
|
|
bench::run_once(|b| __rust_begin_short_backtrace(|| bench.run(b)))
|
|
|
|
})),
|
|
|
|
StaticBenchFn(benchfn) => DynTestFn(Box::new(move || {
|
|
|
|
bench::run_once(|b| __rust_begin_short_backtrace(|| benchfn(b)))
|
|
|
|
})),
|
|
|
|
f => f,
|
|
|
|
};
|
|
|
|
TestDescAndFn {
|
|
|
|
desc: x.desc,
|
|
|
|
testfn,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run_test(
|
|
|
|
opts: &TestOpts,
|
|
|
|
force_ignore: bool,
|
|
|
|
test: TestDescAndFn,
|
2019-09-11 19:13:34 -05:00
|
|
|
strategy: RunStrategy,
|
2019-04-07 00:48:59 -05:00
|
|
|
monitor_ch: Sender<MonitorMsg>,
|
|
|
|
concurrency: Concurrent,
|
|
|
|
) {
|
|
|
|
let TestDescAndFn { desc, testfn } = test;
|
|
|
|
|
2019-08-17 00:08:01 -05:00
|
|
|
// FIXME: Re-enable emscripten once it can catch panics again
|
|
|
|
let ignore_because_no_process_support = desc.should_panic != ShouldPanic::No
|
|
|
|
&& (cfg!(target_arch = "wasm32") || cfg!(target_os = "emscripten"));
|
2019-04-07 00:48:59 -05:00
|
|
|
|
2019-09-11 19:13:34 -05:00
|
|
|
if force_ignore || desc.ignore || ignore_because_no_process_support {
|
2019-09-21 12:03:14 -05:00
|
|
|
monitor_ch.send((desc, TrIgnored, None, Vec::new())).unwrap();
|
2019-04-07 00:48:59 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-28 09:07:18 -05:00
|
|
|
struct TestRunOpts {
|
2019-10-09 01:52:08 -05:00
|
|
|
pub strategy: RunStrategy,
|
2019-09-28 09:07:18 -05:00
|
|
|
pub nocapture: bool,
|
|
|
|
pub concurrency: Concurrent,
|
|
|
|
pub time: Option<TestTimeOptions>,
|
|
|
|
}
|
|
|
|
|
2019-04-07 00:48:59 -05:00
|
|
|
fn run_test_inner(
|
|
|
|
desc: TestDesc,
|
2019-09-11 19:13:34 -05:00
|
|
|
monitor_ch: Sender<MonitorMsg>,
|
2019-05-24 00:51:48 -05:00
|
|
|
testfn: Box<dyn FnOnce() + Send>,
|
2019-09-28 09:07:18 -05:00
|
|
|
opts: TestRunOpts,
|
2019-04-07 00:48:59 -05:00
|
|
|
) {
|
2019-09-28 09:07:18 -05:00
|
|
|
let concurrency = opts.concurrency;
|
2019-04-07 00:48:59 -05:00
|
|
|
let name = desc.name.clone();
|
|
|
|
|
2019-09-11 19:13:34 -05:00
|
|
|
let runtest = move || {
|
2019-10-09 01:52:08 -05:00
|
|
|
match opts.strategy {
|
2019-09-11 19:13:34 -05:00
|
|
|
RunStrategy::InProcess =>
|
2019-10-09 02:29:20 -05:00
|
|
|
run_test_in_process(
|
|
|
|
desc,
|
|
|
|
opts.nocapture,
|
|
|
|
opts.time.is_some(),
|
|
|
|
testfn,
|
|
|
|
monitor_ch,
|
|
|
|
opts.time
|
|
|
|
),
|
|
|
|
RunStrategy::SpawnPrimary =>
|
|
|
|
spawn_test_subprocess(desc, opts.time.is_some(), monitor_ch, opts.time),
|
2019-09-11 19:13:34 -05:00
|
|
|
}
|
2019-04-07 00:48:59 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// If the platform is single-threaded we're just going to run
|
|
|
|
// the test synchronously, regardless of the concurrency
|
|
|
|
// level.
|
|
|
|
let supports_threads = !cfg!(target_os = "emscripten") && !cfg!(target_arch = "wasm32");
|
|
|
|
if concurrency == Concurrent::Yes && supports_threads {
|
|
|
|
let cfg = thread::Builder::new().name(name.as_slice().to_owned());
|
|
|
|
cfg.spawn(runtest).unwrap();
|
|
|
|
} else {
|
|
|
|
runtest();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-28 09:07:18 -05:00
|
|
|
let test_run_opts = TestRunOpts {
|
2019-10-09 01:52:08 -05:00
|
|
|
strategy,
|
2019-09-28 09:07:18 -05:00
|
|
|
nocapture: opts.nocapture,
|
|
|
|
concurrency,
|
|
|
|
time: opts.time_options
|
|
|
|
};
|
|
|
|
|
2019-04-07 00:48:59 -05:00
|
|
|
match testfn {
|
|
|
|
DynBenchFn(bencher) => {
|
2019-09-11 19:13:34 -05:00
|
|
|
// Benchmarks aren't expected to panic, so we run them all in-process.
|
2019-04-07 00:48:59 -05:00
|
|
|
crate::bench::benchmark(desc, monitor_ch, opts.nocapture, |harness| {
|
|
|
|
bencher.run(harness)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
StaticBenchFn(benchfn) => {
|
2019-09-11 19:13:34 -05:00
|
|
|
// Benchmarks aren't expected to panic, so we run them all in-process.
|
2019-04-07 00:48:59 -05:00
|
|
|
crate::bench::benchmark(desc, monitor_ch, opts.nocapture, |harness| {
|
|
|
|
(benchfn.clone())(harness)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
DynTestFn(f) => {
|
2019-09-11 19:13:34 -05:00
|
|
|
match strategy {
|
|
|
|
RunStrategy::InProcess => (),
|
|
|
|
_ => panic!("Cannot run dynamic test fn out-of-process"),
|
|
|
|
};
|
2019-09-21 12:03:14 -05:00
|
|
|
run_test_inner(
|
|
|
|
desc,
|
2019-09-11 19:13:34 -05:00
|
|
|
monitor_ch,
|
|
|
|
Box::new(move || __rust_begin_short_backtrace(f)),
|
2019-10-09 01:52:08 -05:00
|
|
|
test_run_opts,
|
2019-09-11 19:13:34 -05:00
|
|
|
);
|
2019-04-07 00:48:59 -05:00
|
|
|
}
|
|
|
|
StaticTestFn(f) => run_test_inner(
|
|
|
|
desc,
|
2019-09-11 19:13:34 -05:00
|
|
|
monitor_ch,
|
2019-04-07 00:48:59 -05:00
|
|
|
Box::new(move || __rust_begin_short_backtrace(f)),
|
2019-09-28 09:07:18 -05:00
|
|
|
test_run_opts,
|
2019-04-07 00:48:59 -05:00
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Fixed frame used to clean the backtrace with `RUST_BACKTRACE=1`.
|
|
|
|
#[inline(never)]
|
|
|
|
fn __rust_begin_short_backtrace<F: FnOnce()>(f: F) {
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
|
2019-09-28 09:07:18 -05:00
|
|
|
fn calc_result<'a>(
|
|
|
|
desc: &TestDesc,
|
2019-10-09 01:52:08 -05:00
|
|
|
task_result: Result<(), &'a (dyn Any + 'static + Send)>,
|
2019-09-28 09:07:18 -05:00
|
|
|
time_opts: &Option<TestTimeOptions>,
|
2019-10-09 01:52:08 -05:00
|
|
|
exec_time: &Option<TestExecTime>
|
|
|
|
) -> TestResult {
|
2019-09-28 09:07:18 -05:00
|
|
|
let result = match (&desc.should_panic, task_result) {
|
2019-04-07 00:48:59 -05:00
|
|
|
(&ShouldPanic::No, Ok(())) | (&ShouldPanic::Yes, Err(_)) => TrOk,
|
|
|
|
(&ShouldPanic::YesWithMessage(msg), Err(ref err)) => {
|
|
|
|
if err
|
|
|
|
.downcast_ref::<String>()
|
|
|
|
.map(|e| &**e)
|
|
|
|
.or_else(|| err.downcast_ref::<&'static str>().map(|e| *e))
|
|
|
|
.map(|e| e.contains(msg))
|
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
|
|
|
TrOk
|
|
|
|
} else {
|
|
|
|
if desc.allow_fail {
|
|
|
|
TrAllowedFail
|
|
|
|
} else {
|
2019-05-30 15:58:33 -05:00
|
|
|
TrFailedMsg(format!("panic did not include expected string '{}'", msg))
|
2019-04-07 00:48:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-24 13:20:52 -05:00
|
|
|
(&ShouldPanic::Yes, Ok(())) => TrFailedMsg("test did not panic as expected".to_string()),
|
2019-04-07 00:48:59 -05:00
|
|
|
_ if desc.allow_fail => TrAllowedFail,
|
|
|
|
_ => TrFailed,
|
2019-09-28 09:07:18 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// If test is already failed (or allowed to fail), do not change the result.
|
|
|
|
if result != TrOk {
|
|
|
|
return result;
|
2019-04-07 00:48:59 -05:00
|
|
|
}
|
2019-09-28 09:07:18 -05:00
|
|
|
|
|
|
|
// Check if test is failed due to timeout.
|
|
|
|
if let (Some(opts), Some(time)) = (time_opts, exec_time) {
|
|
|
|
if opts.error_on_excess && opts.is_critical(desc, time) {
|
|
|
|
return TrTimedFail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result
|
2019-04-07 00:48:59 -05:00
|
|
|
}
|
|
|
|
|
2019-10-09 01:52:08 -05:00
|
|
|
fn get_result_from_exit_code(
|
|
|
|
desc: &TestDesc,
|
|
|
|
code: i32,
|
|
|
|
time_opts: &Option<TestTimeOptions>,
|
|
|
|
exec_time: &Option<TestExecTime>,
|
|
|
|
) -> TestResult {
|
|
|
|
let result = match (desc.allow_fail, code) {
|
2019-09-11 19:13:34 -05:00
|
|
|
(_, TR_OK) => TrOk,
|
|
|
|
(true, TR_FAILED) => TrAllowedFail,
|
|
|
|
(false, TR_FAILED) => TrFailed,
|
|
|
|
(_, _) => TrFailedMsg(format!("got unexpected return code {}", code)),
|
2019-10-09 01:52:08 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// If test is already failed (or allowed to fail), do not change the result.
|
|
|
|
if result != TrOk {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if test is failed due to timeout.
|
|
|
|
if let (Some(opts), Some(time)) = (time_opts, exec_time) {
|
|
|
|
if opts.error_on_excess && opts.is_critical(desc, time) {
|
|
|
|
return TrTimedFail;
|
|
|
|
}
|
2019-09-11 19:13:34 -05:00
|
|
|
}
|
2019-10-09 01:52:08 -05:00
|
|
|
|
|
|
|
result
|
2019-09-11 19:13:34 -05:00
|
|
|
}
|
|
|
|
|
2019-10-09 01:52:08 -05:00
|
|
|
fn run_test_in_process(
|
|
|
|
desc: TestDesc,
|
|
|
|
nocapture: bool,
|
|
|
|
report_time: bool,
|
|
|
|
testfn: Box<dyn FnOnce() + Send>,
|
|
|
|
monitor_ch: Sender<MonitorMsg>,
|
|
|
|
time_opts: Option<TestTimeOptions>,
|
|
|
|
) {
|
2019-09-11 19:13:34 -05:00
|
|
|
// Buffer for capturing standard I/O
|
|
|
|
let data = Arc::new(Mutex::new(Vec::new()));
|
|
|
|
|
|
|
|
let oldio = if !nocapture {
|
|
|
|
Some((
|
|
|
|
io::set_print(Some(Box::new(Sink(data.clone())))),
|
|
|
|
io::set_panic(Some(Box::new(Sink(data.clone())))),
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let start = if report_time {
|
|
|
|
Some(Instant::now())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
let result = catch_unwind(AssertUnwindSafe(testfn));
|
|
|
|
let exec_time = start.map(|start| {
|
|
|
|
let duration = start.elapsed();
|
|
|
|
TestExecTime(duration)
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some((printio, panicio)) = oldio {
|
|
|
|
io::set_print(printio);
|
|
|
|
io::set_panic(panicio);
|
|
|
|
}
|
|
|
|
|
|
|
|
let test_result = match result {
|
2019-10-09 01:52:08 -05:00
|
|
|
Ok(()) => calc_result(&desc, Ok(()), &time_opts, &exec_time),
|
|
|
|
Err(e) => calc_result(&desc, Err(e.as_ref()), &time_opts, &exec_time),
|
2019-09-11 19:13:34 -05:00
|
|
|
};
|
|
|
|
let stdout = data.lock().unwrap().to_vec();
|
|
|
|
monitor_ch.send((desc.clone(), test_result, exec_time, stdout)).unwrap();
|
|
|
|
}
|
|
|
|
|
2019-10-09 01:52:08 -05:00
|
|
|
fn spawn_test_subprocess(
|
|
|
|
desc: TestDesc,
|
|
|
|
report_time: bool,
|
|
|
|
monitor_ch: Sender<MonitorMsg>,
|
|
|
|
time_opts: Option<TestTimeOptions>,
|
|
|
|
) {
|
2019-09-11 19:13:34 -05:00
|
|
|
let (result, test_output, exec_time) = (|| {
|
|
|
|
let args = env::args().collect::<Vec<_>>();
|
|
|
|
let current_exe = &args[0];
|
|
|
|
|
|
|
|
let start = if report_time {
|
|
|
|
Some(Instant::now())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
let output = match Command::new(current_exe)
|
|
|
|
.env(SECONDARY_TEST_INVOKER_VAR, desc.name.as_slice())
|
|
|
|
.output() {
|
|
|
|
Ok(out) => out,
|
|
|
|
Err(e) => {
|
|
|
|
let err = format!("Failed to spawn {} as child for test: {:?}", args[0], e);
|
|
|
|
return (TrFailed, err.into_bytes(), None);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let exec_time = start.map(|start| {
|
|
|
|
let duration = start.elapsed();
|
|
|
|
TestExecTime(duration)
|
|
|
|
});
|
|
|
|
|
|
|
|
let std::process::Output { stdout, stderr, status } = output;
|
|
|
|
let mut test_output = stdout;
|
|
|
|
formatters::write_stderr_delimiter(&mut test_output, &desc.name);
|
|
|
|
test_output.extend_from_slice(&stderr);
|
|
|
|
|
|
|
|
let result = match (|| -> Result<TestResult, String> {
|
|
|
|
let exit_code = get_exit_code(status)?;
|
2019-10-09 01:52:08 -05:00
|
|
|
Ok(get_result_from_exit_code(&desc, exit_code, &time_opts, &exec_time))
|
2019-09-11 19:13:34 -05:00
|
|
|
})() {
|
|
|
|
Ok(r) => r,
|
|
|
|
Err(e) => {
|
|
|
|
write!(&mut test_output, "Unexpected error: {}", e).unwrap();
|
|
|
|
TrFailed
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
(result, test_output, exec_time)
|
|
|
|
})();
|
|
|
|
|
|
|
|
monitor_ch.send((desc.clone(), result, exec_time, test_output)).unwrap();
|
|
|
|
}
|
|
|
|
|
2019-10-09 01:52:08 -05:00
|
|
|
fn run_test_in_spawned_subprocess(
|
|
|
|
desc: TestDesc,
|
|
|
|
testfn: Box<dyn FnOnce() + Send>,
|
|
|
|
) -> ! {
|
2019-09-11 19:13:34 -05:00
|
|
|
let builtin_panic_hook = panic::take_hook();
|
|
|
|
let record_result = Arc::new(move |panic_info: Option<&'_ PanicInfo<'_>>| {
|
|
|
|
let test_result = match panic_info {
|
2019-10-09 01:52:08 -05:00
|
|
|
Some(info) => calc_result(&desc, Err(info.payload()), &None, &None),
|
|
|
|
None => calc_result(&desc, Ok(()), &None, &None),
|
2019-09-11 19:13:34 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// We don't support serializing TrFailedMsg, so just
|
|
|
|
// print the message out to stderr.
|
|
|
|
if let TrFailedMsg(msg) = &test_result {
|
|
|
|
eprintln!("{}", msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(info) = panic_info {
|
|
|
|
builtin_panic_hook(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let TrOk = test_result {
|
|
|
|
process::exit(TR_OK);
|
|
|
|
} else {
|
|
|
|
process::exit(TR_FAILED);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let record_result2 = record_result.clone();
|
|
|
|
panic::set_hook(Box::new(move |info| record_result2(Some(&info))));
|
|
|
|
testfn();
|
|
|
|
record_result(None);
|
|
|
|
unreachable!("panic=abort callback should have exited the process")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(unix))]
|
|
|
|
fn get_exit_code(status: ExitStatus) -> Result<i32, String> {
|
|
|
|
status.code().ok_or("received no exit code from child process".into())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn get_exit_code(status: ExitStatus) -> Result<i32, String> {
|
|
|
|
use std::os::unix::process::ExitStatusExt;
|
|
|
|
match status.code() {
|
|
|
|
Some(code) => Ok(code),
|
|
|
|
None => match status.signal() {
|
|
|
|
Some(signal) => Err(format!("child process exited with signal {}", signal)),
|
|
|
|
None => Err("child process exited with unknown signal".into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 00:48:59 -05:00
|
|
|
#[derive(Clone, PartialEq)]
|
|
|
|
pub struct MetricMap(BTreeMap<String, Metric>);
|
|
|
|
|
|
|
|
impl MetricMap {
|
|
|
|
pub fn new() -> MetricMap {
|
|
|
|
MetricMap(BTreeMap::new())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Insert a named `value` (+/- `noise`) metric into the map. The value
|
|
|
|
/// must be non-negative. The `noise` indicates the uncertainty of the
|
|
|
|
/// metric, which doubles as the "noise range" of acceptable
|
|
|
|
/// pairwise-regressions on this named value, when comparing from one
|
|
|
|
/// metric to the next using `compare_to_old`.
|
|
|
|
///
|
|
|
|
/// If `noise` is positive, then it means this metric is of a value
|
|
|
|
/// you want to see grow smaller, so a change larger than `noise` in the
|
|
|
|
/// positive direction represents a regression.
|
|
|
|
///
|
|
|
|
/// If `noise` is negative, then it means this metric is of a value
|
|
|
|
/// you want to see grow larger, so a change larger than `noise` in the
|
|
|
|
/// negative direction represents a regression.
|
|
|
|
pub fn insert_metric(&mut self, name: &str, value: f64, noise: f64) {
|
|
|
|
let m = Metric { value, noise };
|
|
|
|
self.0.insert(name.to_owned(), m);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fmt_metrics(&self) -> String {
|
|
|
|
let v = self
|
|
|
|
.0
|
|
|
|
.iter()
|
|
|
|
.map(|(k, v)| format!("{}: {} (+/- {})", *k, v.value, v.noise))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
v.join(", ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Benchmarking
|
2019-03-04 11:04:08 -06:00
|
|
|
|
Moves test::black_box to core::hint
This changes removes a cyclic dependency between the "test" and "libtest"
crates, where "libtest" depends on "test" for "black_box", but "test" depends on
"libtest" for everything else.
I've chosen the "hint" module because there seems to be enough consensus in the
discussion of RFC2360 that this module is where such an intrinsic would belong,
but this PR does not implement that RFC! (note: if that RFC ever gets merged,
the API, docs, etc. of this API will need to change).
For backwards compatibility reasons I've chosen to also keep the "test" feature
gate for these instead of adding a new feature gate. If we change the feature
gate, we'll potentially all benchmarks, and while that's something that we could
do, it seems unnecessary to do that now - if RFC2360 gets merged, we'll need to
do that anyways.
2019-03-21 03:15:52 -05:00
|
|
|
pub use std::hint::black_box;
|
2019-03-19 19:22:19 -05:00
|
|
|
|
2019-04-07 00:48:59 -05:00
|
|
|
impl Bencher {
|
|
|
|
/// Callback for benchmark functions to run in their body.
|
|
|
|
pub fn iter<T, F>(&mut self, mut inner: F)
|
|
|
|
where
|
|
|
|
F: FnMut() -> T,
|
|
|
|
{
|
|
|
|
if self.mode == BenchMode::Single {
|
|
|
|
ns_iter_inner(&mut inner, 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.summary = Some(iter(&mut inner));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bench<F>(&mut self, mut f: F) -> Option<stats::Summary>
|
|
|
|
where
|
|
|
|
F: FnMut(&mut Bencher),
|
|
|
|
{
|
|
|
|
f(self);
|
|
|
|
return self.summary;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ns_from_dur(dur: Duration) -> u64 {
|
|
|
|
dur.as_secs() * 1_000_000_000 + (dur.subsec_nanos() as u64)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ns_iter_inner<T, F>(inner: &mut F, k: u64) -> u64
|
|
|
|
where
|
|
|
|
F: FnMut() -> T,
|
|
|
|
{
|
|
|
|
let start = Instant::now();
|
|
|
|
for _ in 0..k {
|
|
|
|
black_box(inner());
|
|
|
|
}
|
|
|
|
return ns_from_dur(start.elapsed());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn iter<T, F>(inner: &mut F) -> stats::Summary
|
|
|
|
where
|
|
|
|
F: FnMut() -> T,
|
|
|
|
{
|
|
|
|
// Initial bench run to get ballpark figure.
|
|
|
|
let ns_single = ns_iter_inner(inner, 1);
|
|
|
|
|
|
|
|
// Try to estimate iter count for 1ms falling back to 1m
|
|
|
|
// iterations if first run took < 1ns.
|
|
|
|
let ns_target_total = 1_000_000; // 1ms
|
|
|
|
let mut n = ns_target_total / cmp::max(1, ns_single);
|
|
|
|
|
|
|
|
// if the first run took more than 1ms we don't want to just
|
|
|
|
// be left doing 0 iterations on every loop. The unfortunate
|
|
|
|
// side effect of not being able to do as many runs is
|
|
|
|
// automatically handled by the statistical analysis below
|
|
|
|
// (i.e., larger error bars).
|
|
|
|
n = cmp::max(1, n);
|
|
|
|
|
|
|
|
let mut total_run = Duration::new(0, 0);
|
|
|
|
let samples: &mut [f64] = &mut [0.0_f64; 50];
|
|
|
|
loop {
|
|
|
|
let loop_start = Instant::now();
|
|
|
|
|
|
|
|
for p in &mut *samples {
|
|
|
|
*p = ns_iter_inner(inner, n) as f64 / n as f64;
|
|
|
|
}
|
|
|
|
|
|
|
|
stats::winsorize(samples, 5.0);
|
|
|
|
let summ = stats::Summary::new(samples);
|
|
|
|
|
|
|
|
for p in &mut *samples {
|
|
|
|
let ns = ns_iter_inner(inner, 5 * n);
|
|
|
|
*p = ns as f64 / (5 * n) as f64;
|
|
|
|
}
|
|
|
|
|
|
|
|
stats::winsorize(samples, 5.0);
|
|
|
|
let summ5 = stats::Summary::new(samples);
|
|
|
|
|
|
|
|
let loop_run = loop_start.elapsed();
|
|
|
|
|
|
|
|
// If we've run for 100ms and seem to have converged to a
|
|
|
|
// stable median.
|
|
|
|
if loop_run > Duration::from_millis(100)
|
|
|
|
&& summ.median_abs_dev_pct < 1.0
|
|
|
|
&& summ.median - summ5.median < summ5.median_abs_dev
|
|
|
|
{
|
|
|
|
return summ5;
|
|
|
|
}
|
|
|
|
|
|
|
|
total_run = total_run + loop_run;
|
|
|
|
// Longest we ever run for is 3s.
|
|
|
|
if total_run > Duration::from_secs(3) {
|
|
|
|
return summ5;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we overflow here just return the results so far. We check a
|
|
|
|
// multiplier of 10 because we're about to multiply by 2 and the
|
|
|
|
// next iteration of the loop will also multiply by 5 (to calculate
|
|
|
|
// the summ5 result)
|
|
|
|
n = match n.checked_mul(10) {
|
|
|
|
Some(_) => n * 2,
|
|
|
|
None => {
|
|
|
|
return summ5;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod bench {
|
2019-09-11 19:13:34 -05:00
|
|
|
use super::{
|
|
|
|
BenchMode, BenchSamples, Bencher, MonitorMsg, Sender, Sink, TestDesc, TestResult
|
|
|
|
};
|
2019-04-07 00:48:59 -05:00
|
|
|
use crate::stats;
|
|
|
|
use std::cmp;
|
|
|
|
use std::io;
|
|
|
|
use std::panic::{catch_unwind, AssertUnwindSafe};
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
|
|
|
pub fn benchmark<F>(desc: TestDesc, monitor_ch: Sender<MonitorMsg>, nocapture: bool, f: F)
|
|
|
|
where
|
|
|
|
F: FnMut(&mut Bencher),
|
|
|
|
{
|
|
|
|
let mut bs = Bencher {
|
|
|
|
mode: BenchMode::Auto,
|
|
|
|
summary: None,
|
|
|
|
bytes: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
let data = Arc::new(Mutex::new(Vec::new()));
|
|
|
|
let oldio = if !nocapture {
|
|
|
|
Some((
|
2019-09-11 19:13:34 -05:00
|
|
|
io::set_print(Some(Box::new(Sink(data.clone())))),
|
|
|
|
io::set_panic(Some(Box::new(Sink(data.clone())))),
|
2019-04-07 00:48:59 -05:00
|
|
|
))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let result = catch_unwind(AssertUnwindSafe(|| bs.bench(f)));
|
|
|
|
|
|
|
|
if let Some((printio, panicio)) = oldio {
|
|
|
|
io::set_print(printio);
|
|
|
|
io::set_panic(panicio);
|
2019-09-11 19:13:34 -05:00
|
|
|
}
|
2019-04-07 00:48:59 -05:00
|
|
|
|
|
|
|
let test_result = match result {
|
|
|
|
//bs.bench(f) {
|
|
|
|
Ok(Some(ns_iter_summ)) => {
|
|
|
|
let ns_iter = cmp::max(ns_iter_summ.median as u64, 1);
|
|
|
|
let mb_s = bs.bytes * 1000 / ns_iter;
|
|
|
|
|
|
|
|
let bs = BenchSamples {
|
|
|
|
ns_iter_summ,
|
|
|
|
mb_s: mb_s as usize,
|
|
|
|
};
|
|
|
|
TestResult::TrBench(bs)
|
|
|
|
}
|
|
|
|
Ok(None) => {
|
|
|
|
// iter not called, so no data.
|
|
|
|
// FIXME: error in this case?
|
|
|
|
let samples: &mut [f64] = &mut [0.0_f64; 1];
|
|
|
|
let bs = BenchSamples {
|
|
|
|
ns_iter_summ: stats::Summary::new(samples),
|
|
|
|
mb_s: 0,
|
|
|
|
};
|
|
|
|
TestResult::TrBench(bs)
|
|
|
|
}
|
|
|
|
Err(_) => TestResult::TrFailed,
|
|
|
|
};
|
|
|
|
|
|
|
|
let stdout = data.lock().unwrap().to_vec();
|
2019-09-21 12:03:14 -05:00
|
|
|
monitor_ch.send((desc, test_result, None, stdout)).unwrap();
|
2019-04-07 00:48:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run_once<F>(f: F)
|
|
|
|
where
|
|
|
|
F: FnMut(&mut Bencher),
|
|
|
|
{
|
|
|
|
let mut bs = Bencher {
|
|
|
|
mode: BenchMode::Single,
|
|
|
|
summary: None,
|
|
|
|
bytes: 0,
|
|
|
|
};
|
|
|
|
bs.bench(f);
|
|
|
|
}
|
|
|
|
}
|