Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpdrsn committed Apr 28, 2021
1 parent 214c948 commit be20e9d
Showing 1 changed file with 29 additions and 66 deletions.
95 changes: 29 additions & 66 deletions tests/integration_tests/tests/timeout.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,19 @@
use integration_tests::pb::{test_client, test_server, Input, Output};
use std::time::Duration;
use std::{net::SocketAddr, time::Duration};
use tokio::net::TcpListener;
use tonic::{transport::Server, Code, Request, Response, Status};

#[tokio::test]
async fn cancelation_on_timeout() {
struct Svc;

#[tonic::async_trait]
impl test_server::Test for Svc {
async fn unary_call(&self, _req: Request<Input>) -> Result<Response<Output>, Status> {
// Wait for a time longer than the timeout
tokio::time::sleep(Duration::from_millis(1_000)).await;
Ok(Response::new(Output {}))
}
}

let svc = test_server::TestServer::new(Svc);

let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();

tokio::spawn(async move {
Server::builder()
.add_service(svc)
.serve_with_incoming(tokio_stream::wrappers::TcpListenerStream::new(listener))
.await
.unwrap();
});
let addr = run_service_in_background(Duration::from_secs(1), Duration::from_secs(100)).await;

let mut client = test_client::TestClient::connect(format!("http://{}", addr))
.await
.unwrap();

let mut req = Request::new(Input {});
req.metadata_mut()
// 500 ms
.insert("grpc-timeout", "500m".parse().unwrap());

let res = client.unary_call(req).await;
Expand All @@ -46,30 +25,7 @@ async fn cancelation_on_timeout() {

#[tokio::test]
async fn picks_server_timeout_if_thats_sorter() {
struct Svc;

#[tonic::async_trait]
impl test_server::Test for Svc {
async fn unary_call(&self, _req: Request<Input>) -> Result<Response<Output>, Status> {
// Wait for a time longer than the timeout
tokio::time::sleep(Duration::from_secs(1)).await;
Ok(Response::new(Output {}))
}
}

let svc = test_server::TestServer::new(Svc);

let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();

tokio::spawn(async move {
Server::builder()
.timeout(Duration::from_millis(100))
.add_service(svc)
.serve_with_incoming(tokio_stream::wrappers::TcpListenerStream::new(listener))
.await
.unwrap();
});
let addr = run_service_in_background(Duration::from_secs(1), Duration::from_millis(100)).await;

let mut client = test_client::TestClient::connect(format!("http://{}", addr))
.await
Expand All @@ -88,42 +44,49 @@ async fn picks_server_timeout_if_thats_sorter() {

#[tokio::test]
async fn picks_client_timeout_if_thats_sorter() {
struct Svc;
let addr = run_service_in_background(Duration::from_secs(1), Duration::from_secs(100)).await;

let mut client = test_client::TestClient::connect(format!("http://{}", addr))
.await
.unwrap();

let mut req = Request::new(Input {});
req.metadata_mut()
// 100 ms
.insert("grpc-timeout", "100m".parse().unwrap());

let res = client.unary_call(req).await;
let err = res.unwrap_err();
assert!(err.message().contains("Timeout expired"));
assert_eq!(err.code(), Code::Cancelled);
}

async fn run_service_in_background(latency: Duration, server_timeout: Duration) -> SocketAddr {
struct Svc {
latency: Duration,
}

#[tonic::async_trait]
impl test_server::Test for Svc {
async fn unary_call(&self, _req: Request<Input>) -> Result<Response<Output>, Status> {
// Wait for a time longer than the timeout
tokio::time::sleep(Duration::from_secs(1)).await;
tokio::time::sleep(self.latency).await;
Ok(Response::new(Output {}))
}
}

let svc = test_server::TestServer::new(Svc);
let svc = test_server::TestServer::new(Svc { latency });

let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();

tokio::spawn(async move {
Server::builder()
.timeout(Duration::from_secs(9001))
.timeout(server_timeout)
.add_service(svc)
.serve_with_incoming(tokio_stream::wrappers::TcpListenerStream::new(listener))
.await
.unwrap();
});

let mut client = test_client::TestClient::connect(format!("http://{}", addr))
.await
.unwrap();

let mut req = Request::new(Input {});
req.metadata_mut()
// 100 ms
.insert("grpc-timeout", "100m".parse().unwrap());

let res = client.unary_call(req).await;
let err = res.unwrap_err();
assert!(err.message().contains("Timeout expired"));
assert_eq!(err.code(), Code::Cancelled);
addr
}

0 comments on commit be20e9d

Please sign in to comment.