diff --git a/actix-utils/Cargo.toml b/actix-utils/Cargo.toml index 6beb5816..ceef0afe 100644 --- a/actix-utils/Cargo.toml +++ b/actix-utils/Cargo.toml @@ -24,4 +24,7 @@ bytes = "0.4" futures = "0.1" tokio-timer = "0.2.8" tokio-current-thread = "0.1" -log = "0.4" \ No newline at end of file +log = "0.4" + +[dev-dependencies] +actix-rt = "0.1" \ No newline at end of file diff --git a/actix-utils/src/time.rs b/actix-utils/src/time.rs index 2a83a5aa..d2bfe86a 100644 --- a/actix-utils/src/time.rs +++ b/actix-utils/src/time.rs @@ -153,3 +153,96 @@ impl SystemTimeService { } } } + +#[cfg(test)] +mod tests { + use super::*; + use futures::future; + use std::time::{Duration, SystemTime}; + + /// State Under Test: Two calls of `SystemTimeService::now()` return the same value if they are done within resolution interval of `SystemTimeService`. + /// + /// Expected Behavior: Two back-to-back calls of `SystemTimeService::now()` return the same value. + #[test] + fn system_time_service_time_does_not_immediately_change() { + let resolution = Duration::from_millis(50); + + let _ = actix_rt::System::new("test").block_on(future::lazy(|| { + let time_service = SystemTimeService::with(resolution); + + assert_eq!(time_service.now(), time_service.now()); + + Ok::<(), ()>(()) + })); + } + + /// State Under Test: Two calls of `LowResTimeService::now()` return the same value if they are done within resolution interval of `SystemTimeService`. + /// + /// Expected Behavior: Two back-to-back calls of `LowResTimeService::now()` return the same value. + #[test] + fn lowres_time_service_time_does_not_immediately_change() { + let resolution = Duration::from_millis(50); + + let _ = actix_rt::System::new("test").block_on(future::lazy(|| { + let time_service = LowResTimeService::with(resolution); + + assert_eq!(time_service.now(), time_service.now()); + + Ok::<(), ()>(()) + })); + } + + /// State Under Test: `SystemTimeService::now()` updates returned value every resolution period. + /// + /// Expected Behavior: Two calls of `LowResTimeService::now()` made in subsequent resolution interval return different values + /// and second value is greater than the first one at least by a resolution interval. + #[test] + fn system_time_service_time_updates_after_resolution_interval() { + let resolution = Duration::from_millis(100); + let wait_time = Duration::from_millis(150); + + let _ = actix_rt::System::new("test").block_on(future::lazy(|| { + let time_service = SystemTimeService::with(resolution); + + let first_time = time_service + .now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap(); + + sleep(wait_time).then(move |_| { + let second_time = time_service + .now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap(); + + assert!(second_time - first_time >= wait_time); + + Ok::<(), ()>(()) + }) + })); + } + + /// State Under Test: `LowResTimeService::now()` updates returned value every resolution period. + /// + /// Expected Behavior: Two calls of `LowResTimeService::now()` made in subsequent resolution interval return different values + /// and second value is greater than the first one at least by a resolution interval. + #[test] + fn lowres_time_service_time_updates_after_resolution_interval() { + let resolution = Duration::from_millis(100); + let wait_time = Duration::from_millis(150); + + let _ = actix_rt::System::new("test").block_on(future::lazy(|| { + let time_service = LowResTimeService::with(resolution); + + let first_time = time_service.now(); + + sleep(wait_time).then(move |_| { + let second_time = time_service.now(); + + assert!(second_time - first_time >= wait_time); + + Ok::<(), ()>(()) + }) + })); + } +}