Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Apr 9, 2020
1 parent ea6eead commit 46ce12f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
44 changes: 42 additions & 2 deletions ntex/src/util/keepalive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use crate::{Service, ServiceFactory};

use super::time::{LowResTime, LowResTimeService};

/// KeepAlive service factory
///
/// Controls min time between requests.
pub struct KeepAlive<R, E, F> {
f: F,
ka: Duration,
Expand All @@ -24,11 +27,15 @@ impl<R, E, F> KeepAlive<R, E, F>
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,
}
}
Expand Down Expand Up @@ -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))
);
}
}

0 comments on commit 46ce12f

Please sign in to comment.