File: builder.rs

package info (click to toggle)
rust-tower 0.5.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 988 kB
  • sloc: makefile: 2
file content (55 lines) | stat: -rw-r--r-- 1,556 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#![cfg(all(feature = "buffer", feature = "limit", feature = "retry"))]
mod support;
use futures_util::{future::Ready, pin_mut};
use std::time::Duration;
use tower::builder::ServiceBuilder;
use tower::retry::Policy;
use tower::util::ServiceExt;
use tower_service::*;
use tower_test::{assert_request_eq, mock};

#[tokio::test(flavor = "current_thread")]
async fn builder_service() {
    let _t = support::trace_init();

    let (service, handle) = mock::pair();
    pin_mut!(handle);

    let policy = MockPolicy::<&'static str, bool>::default();
    let mut client = ServiceBuilder::new()
        .buffer(5)
        .concurrency_limit(5)
        .rate_limit(5, Duration::from_secs(5))
        .retry(policy)
        .map_response(|r: &'static str| r == "world")
        .map_request(|r: &'static str| r == "hello")
        .service(service);

    // allow a request through
    handle.allow(1);

    let fut = client.ready().await.unwrap().call("hello");
    assert_request_eq!(handle, true).send_response("world");
    assert!(fut.await.unwrap());
}

#[derive(Debug, Clone, Default)]
struct MockPolicy<Req, Res> {
    _pd: std::marker::PhantomData<(Req, Res)>,
}

impl<Req, Res, E> Policy<Req, Res, E> for MockPolicy<Req, Res>
where
    Req: Clone,
    E: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
{
    type Future = Ready<()>;

    fn retry(&mut self, _req: &mut Req, _result: &mut Result<Res, E>) -> Option<Self::Future> {
        None
    }

    fn clone_request(&mut self, req: &Req) -> Option<Req> {
        Some(req.clone())
    }
}