Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Jun 26, 2023
1 parent eea5b3b commit 2e66b4b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
11 changes: 11 additions & 0 deletions ntex-service/src/and_then.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,17 @@ mod tests {
assert_eq!(res, Poll::Ready(()));
}

#[ntex::test]
async fn test_poll_ready2() {
let cnt = Rc::new(Cell::new(0));
let srv = Box::new(chain(Srv1(cnt.clone())).and_then(Srv2(cnt.clone())));
let res = lazy(|cx| srv.poll_ready(cx)).await;
assert_eq!(res, Poll::Ready(Ok(())));
assert_eq!(cnt.get(), 2);
let res = lazy(|cx| srv.poll_shutdown(cx)).await;
assert_eq!(res, Poll::Ready(()));
}

#[ntex::test]
async fn test_call() {
let cnt = Rc::new(Cell::new(0));
Expand Down
36 changes: 33 additions & 3 deletions ntex-service/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ where
let fut = svc.call(
req.take().unwrap(),
ServiceCtx {
idx: *idx,
waiters,
idx: *idx,
_t: marker::PhantomData,
},
);
Expand Down Expand Up @@ -293,8 +293,8 @@ where
let fut = svc.call(
req.take().unwrap(),
ServiceCtx {
idx: *idx,
waiters,
idx: *idx,
_t: marker::PhantomData,
},
);
Expand Down Expand Up @@ -340,8 +340,9 @@ mod tests {
fn call<'a>(
&'a self,
req: &'static str,
_: ServiceCtx<'a, Self>,
ctx: ServiceCtx<'a, Self>,
) -> Self::Future<'a> {
let _ = ctx.clone();
Ready::Ok(req)
}
}
Expand Down Expand Up @@ -409,4 +410,33 @@ mod tests {
assert_eq!(cnt.get(), 5);
assert_eq!(&*data.borrow(), &["srv2", "srv1"]);
}

#[ntex::test]
async fn test_advance_to_call() {
let cnt = Rc::new(Cell::new(0));
let con = condition::Condition::new();
let srv = Pipeline::from(Srv(cnt.clone(), con.wait()));

let mut fut = srv.service_call("test").advance_to_call();
let _ = lazy(|cx| Pin::new(&mut fut).poll(cx)).await;
con.notify();

let res = lazy(|cx| Pin::new(&mut fut).poll(cx)).await;
assert!(res.is_ready());
}

#[ntex::test]
#[should_panic]
async fn test_advance_to_call_panic() {
let cnt = Rc::new(Cell::new(0));
let con = condition::Condition::new();
let srv = Pipeline::from(Srv(cnt.clone(), con.wait()));

let mut fut = srv.service_call("test");
let _ = lazy(|cx| Pin::new(&mut fut).poll(cx)).await;
con.notify();

let _ = lazy(|cx| Pin::new(&mut fut).poll(cx)).await;
let _f = fut.advance_to_call();
}
}
19 changes: 19 additions & 0 deletions ntex-service/src/map_init_err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,23 @@ mod tests {
assert!(factory.create(&true).await.is_err());
assert!(factory.create(&false).await.is_ok());
}

#[ntex::test]
async fn map_init_err2() {
let factory = fn_factory_with_config(|err: &bool| {
let err = *err;
async move {
if err {
Err(())
} else {
Ok(into_service(|i: usize| async move { Ok::<_, ()>(i * 2) }))
}
}
})
.map_init_err(|_| std::io::Error::new(std::io::ErrorKind::Other, "err"))
.clone();

assert!(factory.create(&true).await.is_err());
assert!(factory.create(&false).await.is_ok());
}
}

0 comments on commit 2e66b4b

Please sign in to comment.