From f7f53b89cb13d310b5bef6509865de64fdb89649 Mon Sep 17 00:00:00 2001 From: Justin Geibel Date: Fri, 21 Feb 2020 22:45:45 -0500 Subject: [PATCH] Add a test ensuring blocking endpoints don't block each other --- src/tests.rs | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index 08a79a3..04654c1 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -85,6 +85,7 @@ fn build_headers(msg: &str) -> HashMap> { fn make_service( handler: H, + max_thread_count: usize, ) -> impl Service< hyper::Request, Response = hyper::Response, @@ -93,7 +94,7 @@ fn make_service( > { use hyper::service::service_fn; - let handler = std::sync::Arc::new(BlockingHandler::new(handler, 1)); + let handler = std::sync::Arc::new(BlockingHandler::new(handler, max_thread_count)); service_fn(move |request: hyper::Request| { let remote_addr = ([0, 0, 0, 0], 0).into(); @@ -102,7 +103,7 @@ fn make_service( } async fn simulate_request(handler: H) -> hyper::Response { - let mut service = make_service(handler); + let mut service = make_service(handler, 1); service.call(hyper::Request::default()).await.unwrap() } @@ -141,7 +142,7 @@ async fn recover_from_panic() { #[tokio::test] async fn normalize_path() { - let mut service = make_service(AssertPathNormalized); + let mut service = make_service(AssertPathNormalized, 1); let req = hyper::Request::put("//removed/.././.././normalized") .body(hyper::Body::default()) .unwrap(); @@ -159,7 +160,7 @@ async fn normalize_path() { #[tokio::test] async fn limits_thread_count() { - let mut service = make_service(Sleep); + let mut service = make_service(Sleep, 1); let first = service.call(hyper::Request::default()); let second = service.call(hyper::Request::default()); @@ -173,3 +174,22 @@ async fn limits_thread_count() { assert_eq!(first_completed.status(), 503) } + +#[tokio::test] +async fn sleeping_doesnt_block_another_request() { + let mut service = make_service(Sleep, 2); + + let first = service.call(hyper::Request::default()); + let second = service.call(hyper::Request::default()); + + let start = std::time::Instant::now(); + + // Spawn 2 requests that each sleeps for 100ms + let (first, second) = futures::join!(first, second); + + // Elapsed time should be closer to 100ms than 200ms + assert!(start.elapsed().as_millis() < 150); + + assert_eq!(first.unwrap().status(), 200); + assert_eq!(second.unwrap().status(), 200); +}