Skip to content

Commit 46ce12f

Browse files
committed
update tests
1 parent ea6eead commit 46ce12f

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

.github/workflows/windows.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ jobs:
3232
with:
3333
command: generate-lockfile
3434

35-
- name: Cache vcpkg
36-
uses: actions/cache@v1
37-
with:
38-
path: C:\vcpkg\installed\x64-windows\
39-
key: x86_64-pc-windows-msvc-openssl-${{ hashFiles('**/Cargo.lock') }}
40-
4135
- name: Cache cargo registry
4236
uses: actions/cache@v1
4337
with:

ntex/src/util/keepalive.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ use crate::{Service, ServiceFactory};
1313

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

16+
/// KeepAlive service factory
17+
///
18+
/// Controls min time between requests.
1619
pub struct KeepAlive<R, E, F> {
1720
f: F,
1821
ka: Duration,
@@ -24,11 +27,15 @@ impl<R, E, F> KeepAlive<R, E, F>
2427
where
2528
F: Fn() -> E + Clone,
2629
{
27-
pub fn new(ka: Duration, time: LowResTime, f: F) -> Self {
30+
/// Construct KeepAlive service factory.
31+
///
32+
/// ka - keep-alive timeout
33+
/// err - error factory function
34+
pub fn new(ka: Duration, time: LowResTime, err: F) -> Self {
2835
KeepAlive {
29-
f,
3036
ka,
3137
time,
38+
f: err,
3239
_t: PhantomData,
3340
}
3441
}
@@ -134,3 +141,36 @@ where
134141
ok(req)
135142
}
136143
}
144+
145+
#[cfg(test)]
146+
mod tests {
147+
use futures::future::lazy;
148+
149+
use super::*;
150+
use crate::rt::time::delay_for;
151+
use crate::service::{Service, ServiceFactory};
152+
153+
#[derive(Debug, PartialEq)]
154+
struct TestErr;
155+
156+
#[ntex_rt::test]
157+
async fn test_ka() {
158+
let factory = KeepAlive::new(
159+
Duration::from_millis(100),
160+
LowResTime::with(Duration::from_millis(10)),
161+
|| TestErr,
162+
);
163+
let _ = factory.clone();
164+
165+
let service = factory.new_service(()).await.unwrap();
166+
167+
assert_eq!(service.call(1usize).await, Ok(1usize));
168+
assert!(lazy(|cx| service.poll_ready(cx)).await.is_ready());
169+
170+
delay_for(Duration::from_millis(500)).await;
171+
assert_eq!(
172+
lazy(|cx| service.poll_ready(cx)).await,
173+
Poll::Ready(Err(TestErr))
174+
);
175+
}
176+
}

0 commit comments

Comments
 (0)