Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
Add a test ensuring blocking endpoints don't block each other
Browse files Browse the repository at this point in the history
  • Loading branch information
jtgeibel committed Feb 22, 2020
1 parent 16a21b5 commit f7f53b8
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ fn build_headers(msg: &str) -> HashMap<String, Vec<String>> {

fn make_service<H: Handler>(
handler: H,
max_thread_count: usize,
) -> impl Service<
hyper::Request<hyper::Body>,
Response = hyper::Response<hyper::Body>,
Expand All @@ -93,7 +94,7 @@ fn make_service<H: Handler>(
> {
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<hyper::Body>| {
let remote_addr = ([0, 0, 0, 0], 0).into();
Expand All @@ -102,7 +103,7 @@ fn make_service<H: Handler>(
}

async fn simulate_request<H: Handler>(handler: H) -> hyper::Response<hyper::Body> {
let mut service = make_service(handler);
let mut service = make_service(handler, 1);
service.call(hyper::Request::default()).await.unwrap()
}

Expand Down Expand Up @@ -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();
Expand All @@ -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());

Expand All @@ -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);
}

0 comments on commit f7f53b8

Please sign in to comment.