Skip to content

Commit f2b8478

Browse files
committed
libtest: encapsulate time-related logic and avoid unnecessary allocations
1 parent 75a2975 commit f2b8478

File tree

1 file changed

+39
-19
lines changed

1 file changed

+39
-19
lines changed

src/libtest/lib.rs

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ impl TimeThreshold {
490490
let durations_str = env::var(env_var_name).ok()?;
491491

492492
// Split string into 2 substrings by comma and try to parse numbers.
493-
let durations: Vec<u64> = durations_str
493+
let mut durations = durations_str
494494
.splitn(2, ',')
495495
.map(|v| {
496496
u64::from_str(v).unwrap_or_else(|_| {
@@ -499,18 +499,20 @@ impl TimeThreshold {
499499
env_var_name, v
500500
)
501501
})
502-
})
503-
.collect();
502+
});
504503

505-
// Check that we have exactly 2 numbers.
506-
if durations.len() != 2 {
504+
// Callback to be called if the environment variable has unexpected structure.
505+
let panic_on_incorrect_value = || {
507506
panic!(
508507
"Duration variable {} expected to have 2 numbers separated by comma, but got {}",
509-
env_var_name, durations.len()
508+
env_var_name, durations_str
510509
);
511-
}
510+
};
512511

513-
let (warn, critical) = (durations[0], durations[1]);
512+
let (warn, critical) = (
513+
durations.next().unwrap_or_else(panic_on_incorrect_value),
514+
durations.next().unwrap_or_else(panic_on_incorrect_value)
515+
);
514516

515517
Some(Self::new(Duration::from_millis(warn), Duration::from_millis(critical)))
516518
}
@@ -519,6 +521,8 @@ impl TimeThreshold {
519521
/// Structure with parameters for calculating test execution time.
520522
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
521523
pub struct TestTimeOptions {
524+
/// Denotes if the test critical execution time limit excess should be considered
525+
/// a test failure.
522526
pub error_on_excess: bool,
523527
pub colored: bool,
524528
pub unit_threshold: TimeThreshold,
@@ -609,6 +613,8 @@ pub struct TestOpts {
609613

610614
/// Result of parsing the options.
611615
pub type OptRes = Result<TestOpts, String>;
616+
/// Result of parsing the option part.
617+
type OptPartRes<T> = Result<Option<T>, String>;
612618

613619
fn optgroups() -> getopts::Options {
614620
let mut opts = getopts::Options::new();
@@ -772,6 +778,27 @@ macro_rules! unstable_optflag {
772778
}};
773779
}
774780

781+
// Gets the CLI options assotiated with `report-time` feature.
782+
fn get_time_options(
783+
matches: &getopts::Matches,
784+
allow_unstable: bool)
785+
-> Option<OptPartRes<TestTimeOptions>> {
786+
let report_time = unstable_optflag!(matches, allow_unstable, "report-time");
787+
let colored_opt_str = matches.opt_str("report-time");
788+
let report_time_colored = report_time && colored_opt_str == Some("colored".into());
789+
let ensure_test_time = unstable_optflag!(matches, allow_unstable, "ensure-test-time");
790+
791+
// If `ensure-test-time` option is provided, time output is enforced,
792+
// so user won't be confused if any of tests will silently fail.
793+
let options = if report_time || ensure_test_time {
794+
Some(TestTimeOptions::new_from_env(ensure_test_time, report_time_colored))
795+
} else {
796+
None
797+
};
798+
799+
Some(Ok(options))
800+
}
801+
775802
// Parses command line arguments into test options
776803
pub fn parse_opts(args: &[String]) -> Option<OptRes> {
777804
let mut allow_unstable = false;
@@ -842,17 +869,10 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
842869
};
843870
}
844871

845-
let report_time = unstable_optflag!(matches, allow_unstable, "report-time");
846-
let colored_opt_str = matches.opt_str("report-time");
847-
let report_time_colored = report_time && colored_opt_str == Some("colored".into());
848-
let ensure_test_time = unstable_optflag!(matches, allow_unstable, "ensure-test-time");
849-
850-
// If `ensure-test-time` option is provided, time output is enforced,
851-
// so user won't be confused if any of tests will silently fail.
852-
let time_options = if report_time || ensure_test_time {
853-
Some(TestTimeOptions::new_from_env(ensure_test_time, report_time_colored))
854-
} else {
855-
None
872+
let time_options = match get_time_options(&matches, allow_unstable) {
873+
Some(Ok(val)) => val,
874+
Some(Err(e)) => return Some(Err(e)),
875+
x => panic!("Unexpected output from `get_time_options`: {:?}", x),
856876
};
857877

858878
let test_threads = match matches.opt_str("test-threads") {

0 commit comments

Comments
 (0)