|
| 1 | +use integration_tests::pb::{test_client, test_server, Input, Output}; |
| 2 | +use std::{net::SocketAddr, time::Duration}; |
| 3 | +use tokio::net::TcpListener; |
| 4 | +use tonic::{transport::Server, Code, Request, Response, Status}; |
| 5 | + |
| 6 | +#[tokio::test] |
| 7 | +async fn cancelation_on_timeout() { |
| 8 | + let addr = run_service_in_background(Duration::from_secs(1), Duration::from_secs(100)).await; |
| 9 | + |
| 10 | + let mut client = test_client::TestClient::connect(format!("http://{}", addr)) |
| 11 | + .await |
| 12 | + .unwrap(); |
| 13 | + |
| 14 | + let mut req = Request::new(Input {}); |
| 15 | + req.metadata_mut() |
| 16 | + // 500 ms |
| 17 | + .insert("grpc-timeout", "500m".parse().unwrap()); |
| 18 | + |
| 19 | + let res = client.unary_call(req).await; |
| 20 | + |
| 21 | + let err = res.unwrap_err(); |
| 22 | + assert!(err.message().contains("Timeout expired")); |
| 23 | + assert_eq!(err.code(), Code::Cancelled); |
| 24 | +} |
| 25 | + |
| 26 | +#[tokio::test] |
| 27 | +async fn picks_server_timeout_if_thats_sorter() { |
| 28 | + let addr = run_service_in_background(Duration::from_secs(1), Duration::from_millis(100)).await; |
| 29 | + |
| 30 | + let mut client = test_client::TestClient::connect(format!("http://{}", addr)) |
| 31 | + .await |
| 32 | + .unwrap(); |
| 33 | + |
| 34 | + let mut req = Request::new(Input {}); |
| 35 | + req.metadata_mut() |
| 36 | + // 10 hours |
| 37 | + .insert("grpc-timeout", "10H".parse().unwrap()); |
| 38 | + |
| 39 | + let res = client.unary_call(req).await; |
| 40 | + let err = res.unwrap_err(); |
| 41 | + assert!(err.message().contains("Timeout expired")); |
| 42 | + assert_eq!(err.code(), Code::Cancelled); |
| 43 | +} |
| 44 | + |
| 45 | +#[tokio::test] |
| 46 | +async fn picks_client_timeout_if_thats_sorter() { |
| 47 | + let addr = run_service_in_background(Duration::from_secs(1), Duration::from_secs(100)).await; |
| 48 | + |
| 49 | + let mut client = test_client::TestClient::connect(format!("http://{}", addr)) |
| 50 | + .await |
| 51 | + .unwrap(); |
| 52 | + |
| 53 | + let mut req = Request::new(Input {}); |
| 54 | + req.metadata_mut() |
| 55 | + // 100 ms |
| 56 | + .insert("grpc-timeout", "100m".parse().unwrap()); |
| 57 | + |
| 58 | + let res = client.unary_call(req).await; |
| 59 | + let err = res.unwrap_err(); |
| 60 | + assert!(err.message().contains("Timeout expired")); |
| 61 | + assert_eq!(err.code(), Code::Cancelled); |
| 62 | +} |
| 63 | + |
| 64 | +async fn run_service_in_background(latency: Duration, server_timeout: Duration) -> SocketAddr { |
| 65 | + struct Svc { |
| 66 | + latency: Duration, |
| 67 | + } |
| 68 | + |
| 69 | + #[tonic::async_trait] |
| 70 | + impl test_server::Test for Svc { |
| 71 | + async fn unary_call(&self, _req: Request<Input>) -> Result<Response<Output>, Status> { |
| 72 | + tokio::time::sleep(self.latency).await; |
| 73 | + Ok(Response::new(Output {})) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + let svc = test_server::TestServer::new(Svc { latency }); |
| 78 | + |
| 79 | + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); |
| 80 | + let addr = listener.local_addr().unwrap(); |
| 81 | + |
| 82 | + tokio::spawn(async move { |
| 83 | + Server::builder() |
| 84 | + .timeout(server_timeout) |
| 85 | + .add_service(svc) |
| 86 | + .serve_with_incoming(tokio_stream::wrappers::TcpListenerStream::new(listener)) |
| 87 | + .await |
| 88 | + .unwrap(); |
| 89 | + }); |
| 90 | + |
| 91 | + addr |
| 92 | +} |
0 commit comments