From 2e66b4b3610b460d028ee533801f2afbe15b3d5f Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Mon, 26 Jun 2023 10:00:16 +0600 Subject: [PATCH] Update tests --- ntex-service/src/and_then.rs | 11 ++++++++++ ntex-service/src/ctx.rs | 36 +++++++++++++++++++++++++++++--- ntex-service/src/map_init_err.rs | 19 +++++++++++++++++ 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/ntex-service/src/and_then.rs b/ntex-service/src/and_then.rs index 910472c1..b4250b57 100644 --- a/ntex-service/src/and_then.rs +++ b/ntex-service/src/and_then.rs @@ -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)); diff --git a/ntex-service/src/ctx.rs b/ntex-service/src/ctx.rs index f01fa997..dc11252d 100644 --- a/ntex-service/src/ctx.rs +++ b/ntex-service/src/ctx.rs @@ -230,8 +230,8 @@ where let fut = svc.call( req.take().unwrap(), ServiceCtx { - idx: *idx, waiters, + idx: *idx, _t: marker::PhantomData, }, ); @@ -293,8 +293,8 @@ where let fut = svc.call( req.take().unwrap(), ServiceCtx { - idx: *idx, waiters, + idx: *idx, _t: marker::PhantomData, }, ); @@ -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) } } @@ -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(); + } } diff --git a/ntex-service/src/map_init_err.rs b/ntex-service/src/map_init_err.rs index 72eaaacf..0e4e9c3c 100644 --- a/ntex-service/src/map_init_err.rs +++ b/ntex-service/src/map_init_err.rs @@ -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()); + } }