Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tower-async]: patch: ensure features like make::MakeService can be used with async Service #8

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tower-async/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#![allow(incomplete_features)]
#![feature(async_fn_in_trait)]
#![feature(impl_trait_projections)]
#![feature(return_type_notation)]
#![allow(elided_lifetimes_in_paths, clippy::type_complexity)]
#![cfg_attr(test, allow(clippy::float_cmp))]
#![cfg_attr(docsrs, feature(doc_auto_cfg, doc_cfg))]
Expand Down
52 changes: 52 additions & 0 deletions tower-async/src/make/make_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,55 @@ where
self.make.make_service(target).await
}
}

#[cfg(test)]
mod tests {
use std::convert::Infallible;

use super::*;


#[derive(Debug, Clone)]
struct EchoService;

impl<R> Service<R> for EchoService {
type Response = R;
type Error = Infallible;

async fn call(&mut self, req: R) -> Result<Self::Response, Self::Error> {
Ok(req)
}
}

async fn higher_order_async_fn<F, R>(mut factory: F, req: R) -> R
where
F: MakeService<(), R, Response = R>,
F::MakeError: std::fmt::Debug,
F::Error: std::fmt::Debug,
F::Response: std::fmt::Debug,
F::Service: Service<R, call(): Send> + Send + 'static,
R: Send + 'static,
{
let mut svc = factory.make_service(()).await.unwrap();

let (tx, rx) = tokio::sync::oneshot::channel();

tokio::spawn(async move {
let resp = svc.call(req).await.unwrap();
tx.send(resp).unwrap();
})
.await
.unwrap();

rx.await.unwrap()
}

#[tokio::test]
async fn higher_order_async_fn_with_make_fn() {
let shared = |_: ()| async { Ok::<_, Infallible>(EchoService) };

let res = higher_order_async_fn(shared, "foo").await;

assert_eq!(res, "foo");
}
}
53 changes: 53 additions & 0 deletions tower-async/src/make/make_service/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,57 @@ mod tests {

assert_eq!(res, "foo");
}

#[derive(Debug, Clone)]
struct EchoService;

impl<R> Service<R> for EchoService {
type Response = R;
type Error = Infallible;

async fn call(&mut self, req: R) -> Result<Self::Response, Self::Error> {
Ok(req)
}
}

async fn higher_order_async_fn<F, R>(mut factory: F, req: R) -> R
where
F: MakeService<(), R, Response = R>,
F::MakeError: std::fmt::Debug,
F::Error: std::fmt::Debug,
F::Response: std::fmt::Debug,
F::Service: Service<R, call(): Send> + Send + 'static,
R: Send + 'static,
{
let mut svc = factory.make_service(()).await.unwrap();

let (tx, rx) = tokio::sync::oneshot::channel();

tokio::spawn(async move {
let resp = svc.call(req).await.unwrap();
tx.send(resp).unwrap();
})
.await
.unwrap();

rx.await.unwrap()
}

#[tokio::test]
async fn higher_order_async_fn_with_shared_service_struct() {
let shared = Shared::new(EchoService);

let res = higher_order_async_fn(shared, "foo").await;

assert_eq!(res, "foo");
}

#[tokio::test]
async fn higher_order_async_fn_with_shared_service_fn() {
let shared = Shared::new(service_fn(echo::<&'static str>));

let res = higher_order_async_fn(shared, "foo").await;

assert_eq!(res, "foo");
}
}