From 46ce12f07a1fc2fcf7e01bf253cd857216422100 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Thu, 9 Apr 2020 22:20:21 +0600 Subject: [PATCH] update tests --- .github/workflows/windows.yml | 6 ----- ntex/src/util/keepalive.rs | 44 +++++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 958bbd26b..86ef70df3 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -32,12 +32,6 @@ jobs: with: command: generate-lockfile - - name: Cache vcpkg - uses: actions/cache@v1 - with: - path: C:\vcpkg\installed\x64-windows\ - key: x86_64-pc-windows-msvc-openssl-${{ hashFiles('**/Cargo.lock') }} - - name: Cache cargo registry uses: actions/cache@v1 with: diff --git a/ntex/src/util/keepalive.rs b/ntex/src/util/keepalive.rs index 5476337d6..060a4cb9a 100644 --- a/ntex/src/util/keepalive.rs +++ b/ntex/src/util/keepalive.rs @@ -13,6 +13,9 @@ use crate::{Service, ServiceFactory}; use super::time::{LowResTime, LowResTimeService}; +/// KeepAlive service factory +/// +/// Controls min time between requests. pub struct KeepAlive { f: F, ka: Duration, @@ -24,11 +27,15 @@ impl KeepAlive where F: Fn() -> E + Clone, { - pub fn new(ka: Duration, time: LowResTime, f: F) -> Self { + /// Construct KeepAlive service factory. + /// + /// ka - keep-alive timeout + /// err - error factory function + pub fn new(ka: Duration, time: LowResTime, err: F) -> Self { KeepAlive { - f, ka, time, + f: err, _t: PhantomData, } } @@ -134,3 +141,36 @@ where ok(req) } } + +#[cfg(test)] +mod tests { + use futures::future::lazy; + + use super::*; + use crate::rt::time::delay_for; + use crate::service::{Service, ServiceFactory}; + + #[derive(Debug, PartialEq)] + struct TestErr; + + #[ntex_rt::test] + async fn test_ka() { + let factory = KeepAlive::new( + Duration::from_millis(100), + LowResTime::with(Duration::from_millis(10)), + || TestErr, + ); + let _ = factory.clone(); + + let service = factory.new_service(()).await.unwrap(); + + assert_eq!(service.call(1usize).await, Ok(1usize)); + assert!(lazy(|cx| service.poll_ready(cx)).await.is_ready()); + + delay_for(Duration::from_millis(500)).await; + assert_eq!( + lazy(|cx| service.poll_ready(cx)).await, + Poll::Ready(Err(TestErr)) + ); + } +}