1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-06-26 19:47:43 +02:00

add test and main macros

This commit is contained in:
Nikolay Kim
2019-11-25 21:49:11 +06:00
parent 1fddd1e75b
commit 4ceac79f2c
25 changed files with 265 additions and 267 deletions

View File

@ -137,36 +137,33 @@ mod tests {
}
}
#[test]
fn test_transform() {
#[actix_rt::test]
async fn test_transform() {
let wait_time = Duration::from_millis(50);
let _ = actix_rt::System::new("test").block_on(async {
let mut srv = InFlightService::new(1, SleepService(wait_time));
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
let res = srv.call(());
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Pending);
let mut srv = InFlightService::new(1, SleepService(wait_time));
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
let _ = res.await;
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
});
let res = srv.call(());
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Pending);
let _ = res.await;
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
}
#[test]
fn test_newtransform() {
#[actix_rt::test]
async fn test_newtransform() {
let wait_time = Duration::from_millis(50);
actix_rt::System::new("test").block_on(async {
let srv = apply(InFlight::new(1), factory_fn(|| ok(SleepService(wait_time))));
let srv = apply(InFlight::new(1), factory_fn(|| ok(SleepService(wait_time))));
let mut srv = srv.new_service(&()).await.unwrap();
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
let mut srv = srv.new_service(&()).await.unwrap();
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
let res = srv.call(());
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Pending);
let res = srv.call(());
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Pending);
let _ = res.await;
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
});
let _ = res.await;
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
}
}

View File

@ -230,8 +230,8 @@ mod tests {
}
}
#[test]
fn test_inorder() {
#[actix_rt::test]
async fn test_inorder() {
let (tx1, rx1) = oneshot::channel();
let (tx2, rx2) = oneshot::channel();
let (tx3, rx3) = oneshot::channel();
@ -269,7 +269,7 @@ mod tests {
let _ = tx2.send(2);
let _ = tx1.send(1);
let _ = actix_rt::System::new("test").block_on(rx_stop);
let _ = rx_stop.await;
let _ = h.join();
}
}

View File

@ -161,77 +161,65 @@ mod tests {
/// 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() {
#[actix_rt::test]
async fn system_time_service_time_does_not_immediately_change() {
let resolution = Duration::from_millis(50);
let _ = actix_rt::System::new("test").block_on(async {
let time_service = SystemTimeService::with(resolution);
assert_eq!(time_service.now(), time_service.now());
});
let time_service = SystemTimeService::with(resolution);
assert_eq!(time_service.now(), time_service.now());
}
/// 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() {
#[actix_rt::test]
async fn lowres_time_service_time_does_not_immediately_change() {
let resolution = Duration::from_millis(50);
let _ = actix_rt::System::new("test").block_on(async {
let time_service = LowResTimeService::with(resolution);
assert_eq!(time_service.now(), time_service.now());
});
let time_service = LowResTimeService::with(resolution);
assert_eq!(time_service.now(), time_service.now());
}
/// 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() {
#[actix_rt::test]
async fn system_time_service_time_updates_after_resolution_interval() {
let resolution = Duration::from_millis(100);
let wait_time = Duration::from_millis(150);
actix_rt::System::new("test").block_on(async {
let time_service = SystemTimeService::with(resolution);
let time_service = SystemTimeService::with(resolution);
let first_time = time_service
.now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap();
let first_time = time_service
.now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap();
delay_for(wait_time).await;
delay_for(wait_time).await;
let second_time = time_service
.now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap();
let second_time = time_service
.now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap();
assert!(second_time - first_time >= wait_time);
});
assert!(second_time - first_time >= wait_time);
}
/// 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() {
#[actix_rt::test]
async fn lowres_time_service_time_updates_after_resolution_interval() {
let resolution = Duration::from_millis(100);
let wait_time = Duration::from_millis(150);
let time_service = LowResTimeService::with(resolution);
let _ = actix_rt::System::new("test").block_on(async {
let time_service = LowResTimeService::with(resolution);
let first_time = time_service.now();
let first_time = time_service.now();
delay_for(wait_time).await;
delay_for(wait_time).await;
let second_time = time_service.now();
assert!(second_time - first_time >= wait_time);
});
let second_time = time_service.now();
assert!(second_time - first_time >= wait_time);
}
}

View File

@ -204,44 +204,35 @@ mod tests {
}
}
#[test]
fn test_success() {
#[actix_rt::test]
async fn test_success() {
let resolution = Duration::from_millis(100);
let wait_time = Duration::from_millis(50);
let res = actix_rt::System::new("test").block_on(async {
let mut timeout = TimeoutService::new(resolution, SleepService(wait_time));
timeout.call(()).await
});
assert_eq!(res, Ok(()));
let mut timeout = TimeoutService::new(resolution, SleepService(wait_time));
assert_eq!(timeout.call(()).await, Ok(()));
}
#[test]
fn test_timeout() {
#[actix_rt::test]
async fn test_timeout() {
let resolution = Duration::from_millis(100);
let wait_time = Duration::from_millis(150);
let res = actix_rt::System::new("test").block_on(async {
let mut timeout = TimeoutService::new(resolution, SleepService(wait_time));
timeout.call(()).await
});
assert_eq!(res, Err(TimeoutError::Timeout));
let mut timeout = TimeoutService::new(resolution, SleepService(wait_time));
assert_eq!(timeout.call(()).await, Err(TimeoutError::Timeout));
}
#[test]
fn test_timeout_newservice() {
#[actix_rt::test]
async fn test_timeout_newservice() {
let resolution = Duration::from_millis(100);
let wait_time = Duration::from_millis(150);
let res = actix_rt::System::new("test").block_on(async {
let timeout = apply(
Timeout::new(resolution),
factory_fn(|| ok::<_, ()>(SleepService(wait_time))),
);
let mut srv = timeout.new_service(&()).await.unwrap();
let timeout = apply(
Timeout::new(resolution),
factory_fn(|| ok::<_, ()>(SleepService(wait_time))),
);
let mut srv = timeout.new_service(&()).await.unwrap();
srv.call(()).await
});
assert_eq!(res, Err(TimeoutError::Timeout));
assert_eq!(srv.call(()).await, Err(TimeoutError::Timeout));
}
}