-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Json Format, Intellij Options and make multi-threaded optional #34
Closed
Closed
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3bfc0f3
Add json output format
PaulWagener bbabdac
Merge remote-tracking branch 'original/master' into format-json
4e762f1
Add no-op args to support IntelliJ Rust
t-moe ace0ceb
Remove Send bound when non multithreaded cfg
t-moe 93690f4
Fix PR Comments of JSON PR (#33)
t-moe 024ad9c
Updated changelog
t-moe ae78e4a
Rustfmt on the entire project
t-moe deef524
Fix tests in singlethreaded mode
t-moe b6ad4d1
Add lifetime parameter to Trial, to allow non 'static bound test funcs
t-moe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,18 +71,15 @@ | |
|
||
#![forbid(unsafe_code)] | ||
|
||
use std::{process, sync::mpsc, fmt, time::Instant}; | ||
use std::{fmt, process, time::Instant}; | ||
|
||
mod args; | ||
mod printer; | ||
|
||
use printer::Printer; | ||
use threadpool::ThreadPool; | ||
|
||
pub use crate::args::{Arguments, ColorSetting, FormatSetting}; | ||
|
||
|
||
|
||
/// A single test or benchmark. | ||
/// | ||
/// The original `libtest` often calls benchmarks "tests", which is a bit | ||
|
@@ -95,7 +92,10 @@ pub use crate::args::{Arguments, ColorSetting, FormatSetting}; | |
/// `#[should_panic]` you need to catch the panic yourself. You likely want to | ||
/// compare the panic payload to an expected value anyway. | ||
pub struct Trial { | ||
#[cfg(feature = "multithreaded")] | ||
runner: Box<dyn FnOnce(bool) -> Outcome + Send>, | ||
#[cfg(not(feature = "multithreaded"))] | ||
runner: Box<dyn FnOnce(bool) -> Outcome>, | ||
info: TestInfo, | ||
} | ||
|
||
|
@@ -104,6 +104,7 @@ impl Trial { | |
/// | ||
/// The runner returning `Ok(())` is interpreted as the test passing. If the | ||
/// runner returns `Err(_)`, the test is considered failed. | ||
#[cfg(feature = "multithreaded")] | ||
pub fn test<R>(name: impl Into<String>, runner: R) -> Self | ||
where | ||
R: FnOnce() -> Result<(), Failed> + Send + 'static, | ||
|
@@ -122,6 +123,29 @@ impl Trial { | |
} | ||
} | ||
|
||
/// Creates a (non-benchmark) test with the given name and runner. | ||
/// | ||
/// The runner returning `Ok(())` is interpreted as the test passing. If the | ||
/// runner returns `Err(_)`, the test is considered failed. | ||
#[cfg(not(feature = "multithreaded"))] | ||
pub fn test<R>(name: impl Into<String>, runner: R) -> Self | ||
where | ||
R: FnOnce() -> Result<(), Failed> + 'static, | ||
{ | ||
Self { | ||
runner: Box::new(move |_test_mode| match runner() { | ||
Ok(()) => Outcome::Passed, | ||
Err(failed) => Outcome::Failed(failed), | ||
}), | ||
info: TestInfo { | ||
name: name.into(), | ||
kind: String::new(), | ||
is_ignored: false, | ||
is_bench: false, | ||
}, | ||
} | ||
} | ||
|
||
/// Creates a benchmark with the given name and runner. | ||
/// | ||
/// If the runner's parameter `test_mode` is `true`, the runner function | ||
|
@@ -134,6 +158,7 @@ impl Trial { | |
/// `test_mode` is `true` if neither `--bench` nor `--test` are set, and | ||
/// `false` when `--bench` is set. If `--test` is set, benchmarks are not | ||
/// ran at all, and both flags cannot be set at the same time. | ||
#[cfg(feature = "multithreaded")] | ||
pub fn bench<R>(name: impl Into<String>, runner: R) -> Self | ||
where | ||
R: FnOnce(bool) -> Result<Option<Measurement>, Failed> + Send + 'static, | ||
|
@@ -143,8 +168,44 @@ impl Trial { | |
Err(failed) => Outcome::Failed(failed), | ||
Ok(_) if test_mode => Outcome::Passed, | ||
Ok(Some(measurement)) => Outcome::Measured(measurement), | ||
Ok(None) | ||
=> Outcome::Failed("bench runner returned `Ok(None)` in bench mode".into()), | ||
Ok(None) => { | ||
Outcome::Failed("bench runner returned `Ok(None)` in bench mode".into()) | ||
} | ||
}), | ||
info: TestInfo { | ||
name: name.into(), | ||
kind: String::new(), | ||
is_ignored: false, | ||
is_bench: true, | ||
}, | ||
} | ||
} | ||
|
||
/// Creates a benchmark with the given name and runner. | ||
/// | ||
/// If the runner's parameter `test_mode` is `true`, the runner function | ||
/// should run all code just once, without measuring, just to make sure it | ||
/// does not panic. If the parameter is `false`, it should perform the | ||
/// actual benchmark. If `test_mode` is `true` you may return `Ok(None)`, | ||
/// but if it's `false`, you have to return a `Measurement`, or else the | ||
/// benchmark is considered a failure. | ||
/// | ||
/// `test_mode` is `true` if neither `--bench` nor `--test` are set, and | ||
/// `false` when `--bench` is set. If `--test` is set, benchmarks are not | ||
/// ran at all, and both flags cannot be set at the same time. | ||
#[cfg(not(feature = "multithreaded"))] | ||
pub fn bench<R>(name: impl Into<String>, runner: R) -> Self | ||
where | ||
R: FnOnce(bool) -> Result<Option<Measurement>, Failed> + 'static, | ||
{ | ||
Self { | ||
runner: Box::new(move |test_mode| match runner(test_mode) { | ||
Err(failed) => Outcome::Failed(failed), | ||
Ok(_) if test_mode => Outcome::Passed, | ||
Ok(Some(measurement)) => Outcome::Measured(measurement), | ||
Ok(None) => { | ||
Outcome::Failed("bench runner returned `Ok(None)` in bench mode".into()) | ||
} | ||
}), | ||
info: TestInfo { | ||
name: name.into(), | ||
|
@@ -274,13 +335,11 @@ impl Failed { | |
impl<M: std::fmt::Display> From<M> for Failed { | ||
fn from(msg: M) -> Self { | ||
Self { | ||
msg: Some(msg.to_string()) | ||
msg: Some(msg.to_string()), | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
/// The outcome of performing a test/benchmark. | ||
#[derive(Debug, Clone)] | ||
enum Outcome { | ||
|
@@ -403,7 +462,6 @@ impl Arguments { | |
pub fn run(args: &Arguments, mut tests: Vec<Trial>) -> Conclusion { | ||
let start_instant = Instant::now(); | ||
let mut conclusion = Conclusion::empty(); | ||
|
||
// Apply filtering | ||
if args.filter.is_some() || !args.skip.is_empty() || args.ignored { | ||
let len_before = tests.len() as u64; | ||
|
@@ -434,15 +492,15 @@ pub fn run(args: &Arguments, mut tests: Vec<Trial>) -> Conclusion { | |
Outcome::Failed(failed) => { | ||
failed_tests.push((test, failed.msg)); | ||
conclusion.num_failed += 1; | ||
}, | ||
} | ||
Outcome::Ignored => conclusion.num_ignored += 1, | ||
Outcome::Measured(_) => conclusion.num_measured += 1, | ||
} | ||
}; | ||
|
||
// Execute all tests. | ||
let test_mode = !args.bench; | ||
if args.test_threads == Some(1) { | ||
let mut sequentially = |tests: Vec<Trial>| { | ||
// Run test sequentially in main thread | ||
for test in tests { | ||
// Print `test foo ...`, run the test, then print the outcome in | ||
|
@@ -455,38 +513,50 @@ pub fn run(args: &Arguments, mut tests: Vec<Trial>) -> Conclusion { | |
}; | ||
handle_outcome(outcome, test.info, &mut printer); | ||
} | ||
} else { | ||
// Run test in thread pool. | ||
let pool = match args.test_threads { | ||
Some(num_threads) => ThreadPool::new(num_threads), | ||
None => ThreadPool::default() | ||
}; | ||
let (sender, receiver) = mpsc::channel(); | ||
}; | ||
|
||
let num_tests = tests.len(); | ||
for test in tests { | ||
if args.is_ignored(&test) { | ||
sender.send((Outcome::Ignored, test.info)).unwrap(); | ||
} else { | ||
let sender = sender.clone(); | ||
pool.execute(move || { | ||
// It's fine to ignore the result of sending. If the | ||
// receiver has hung up, everything will wind down soon | ||
// anyway. | ||
let outcome = run_single(test.runner, test_mode); | ||
let _ = sender.send((outcome, test.info)); | ||
}); | ||
#[cfg(not(feature = "multithreaded"))] | ||
sequentially(tests); | ||
|
||
#[cfg(feature = "multithreaded")] | ||
{ | ||
use std::sync::mpsc; | ||
use threadpool::ThreadPool; | ||
if args.test_threads == Some(1) { | ||
sequentially(tests); | ||
} else { | ||
// Run test in thread pool. | ||
let pool = match args.test_threads { | ||
Some(num_threads) => ThreadPool::new(num_threads), | ||
None => ThreadPool::default(), | ||
}; | ||
let (sender, receiver) = mpsc::channel(); | ||
|
||
let num_tests = tests.len(); | ||
for test in tests { | ||
if args.is_ignored(&test) { | ||
sender.send((Outcome::Ignored, test.info)).unwrap(); | ||
} else { | ||
let sender = sender.clone(); | ||
pool.execute(move || { | ||
// It's fine to ignore the result of sending. If the | ||
// receiver has hung up, everything will wind down soon | ||
// anyway. | ||
let outcome = run_single(test.runner, test_mode); | ||
let _ = sender.send((outcome, test.info)); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
for (outcome, test_info) in receiver.iter().take(num_tests) { | ||
// In multithreaded mode, we do only print the start of the line | ||
// after the test ran, as otherwise it would lead to terribly | ||
// interleaved output. | ||
printer.print_test(&test_info); | ||
handle_outcome(outcome, test_info, &mut printer); | ||
for (outcome, test_info) in receiver.iter().take(num_tests) { | ||
// In multithreaded mode, we do only print the start of the line | ||
// after the test ran, as otherwise it would lead to terribly | ||
// interleaved output. | ||
printer.print_test(&test_info); | ||
handle_outcome(outcome, test_info, &mut printer); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
// Print failures if there were any, and the final summary. | ||
if !failed_tests.is_empty() { | ||
|
@@ -499,14 +569,19 @@ pub fn run(args: &Arguments, mut tests: Vec<Trial>) -> Conclusion { | |
} | ||
|
||
/// Runs the given runner, catching any panics and treating them as a failed test. | ||
fn run_single(runner: Box<dyn FnOnce(bool) -> Outcome + Send>, test_mode: bool) -> Outcome { | ||
fn run_single( | ||
#[cfg(feature = "multithreaded")] runner: Box<dyn FnOnce(bool) -> Outcome + Send>, | ||
#[cfg(not(feature = "multithreaded"))] runner: Box<dyn FnOnce(bool) -> Outcome>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you just define a type alias somewhere (with cfg attributes) so that the cfg attributes only have to be in one place? |
||
test_mode: bool, | ||
) -> Outcome { | ||
use std::panic::{catch_unwind, AssertUnwindSafe}; | ||
|
||
catch_unwind(AssertUnwindSafe(move || runner(test_mode))).unwrap_or_else(|e| { | ||
// The `panic` information is just an `Any` object representing the | ||
// value the panic was invoked with. For most panics (which use | ||
// `panic!` like `println!`), this is either `&str` or `String`. | ||
let payload = e.downcast_ref::<String>() | ||
let payload = e | ||
.downcast_ref::<String>() | ||
.map(|s| s.as_str()) | ||
.or(e.downcast_ref::<&str>().map(|s| *s)); | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space before
}