1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-01-18 09:41:49 +01:00

add time services tests

This commit is contained in:
Nikolay Kim 2019-01-26 13:21:56 -08:00
parent 84bd257b86
commit cabebb6b3f
2 changed files with 97 additions and 1 deletions

View File

@ -24,4 +24,7 @@ bytes = "0.4"
futures = "0.1"
tokio-timer = "0.2.8"
tokio-current-thread = "0.1"
log = "0.4"
log = "0.4"
[dev-dependencies]
actix-rt = "0.1"

View File

@ -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::<(), ()>(())
})
}));
}
}