1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-27 18:02:58 +01:00

update tests

This commit is contained in:
Nikolay Kim 2019-02-22 13:08:31 -08:00
parent 862be49e30
commit 43d2dd473f
4 changed files with 65 additions and 4 deletions

View File

@ -3,7 +3,7 @@ use std::marker::PhantomData;
use futures::future::{ok, FutureResult}; use futures::future::{ok, FutureResult};
use futures::{Async, IntoFuture, Poll}; use futures::{Async, IntoFuture, Poll};
use super::{IntoService, NewService, Service}; use super::{IntoNewService, IntoService, NewService, Service};
pub struct FnService<F, Req, Out> pub struct FnService<F, Req, Out>
where where
@ -112,3 +112,64 @@ where
Self::new(self.f.clone()) Self::new(self.f.clone())
} }
} }
impl<F, R, S, E> IntoNewService<FnNewServiceNoConfig<F, R, S, E>, ()> for F
where
F: Fn() -> R + Clone,
R: IntoFuture<Item = S, Error = E>,
S: Service,
{
fn into_new_service(self) -> FnNewServiceNoConfig<F, R, S, E> {
FnNewServiceNoConfig::new(self)
}
}
pub struct FnNewServiceNoConfig<F, R, S, E>
where
F: Fn() -> R + Clone,
R: IntoFuture<Item = S, Error = E>,
S: Service,
{
f: F,
}
impl<F, R, S, E> FnNewServiceNoConfig<F, R, S, E>
where
F: Fn() -> R + Clone,
R: IntoFuture<Item = S, Error = E>,
S: Service,
{
pub fn new(f: F) -> Self {
FnNewServiceNoConfig { f }
}
}
impl<F, R, S, E> NewService<()> for FnNewServiceNoConfig<F, R, S, E>
where
F: Fn() -> R + Clone,
R: IntoFuture<Item = S, Error = E>,
S: Service,
{
type Request = S::Request;
type Response = S::Response;
type Error = S::Error;
type Service = S;
type InitError = E;
type Future = R::Future;
fn new_service(&self, _: &()) -> Self::Future {
(self.f)().into_future()
}
}
impl<F, R, S, E> Clone for FnNewServiceNoConfig<F, R, S, E>
where
F: Fn() -> R + Clone,
R: IntoFuture<Item = S, Error = E>,
S: Service,
{
fn clone(&self) -> Self {
Self::new(self.f.clone())
}
}

View File

@ -229,7 +229,7 @@ mod tests {
#[test] #[test]
fn test_new_service() { fn test_new_service() {
let blank = || Ok::<_, ()>(Srv); let blank = |_: &()| Ok::<_, ()>(Srv);
let new_srv = blank.into_new_service().map_err(|_| "error"); let new_srv = blank.into_new_service().map_err(|_| "error");
if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() { if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() {
let res = srv.call(()).poll(); let res = srv.call(()).poll();

View File

@ -142,7 +142,7 @@ mod tests {
let srv = let srv =
BlankNewService::new().apply(InFlight::new(1), || Ok(SleepService(wait_time))); BlankNewService::new().apply(InFlight::new(1), || Ok(SleepService(wait_time)));
if let Async::Ready(mut srv) = srv.new_service().poll().unwrap() { if let Async::Ready(mut srv) = srv.new_service(&()).poll().unwrap() {
assert_eq!(srv.poll_ready(), Ok(Async::Ready(()))); assert_eq!(srv.poll_ready(), Ok(Async::Ready(())));
let mut res = srv.call(()); let mut res = srv.call(());

View File

@ -223,7 +223,7 @@ mod tests {
let res = actix_rt::System::new("test").block_on(lazy(|| { let res = actix_rt::System::new("test").block_on(lazy(|| {
let timeout = BlankNewService::<_, _, ()>::default() let timeout = BlankNewService::<_, _, ()>::default()
.apply(Timeout::new(resolution), || Ok(SleepService(wait_time))); .apply(Timeout::new(resolution), || Ok(SleepService(wait_time)));
if let Async::Ready(mut to) = timeout.new_service().poll().unwrap() { if let Async::Ready(mut to) = timeout.new_service(&()).poll().unwrap() {
to.call(()) to.call(())
} else { } else {
panic!() panic!()