From d78108bbec013e9a6abfb6b54ebfbed09f85aaf9 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sat, 29 Jan 2022 05:12:19 +0000 Subject: [PATCH] fix slow request test --- actix-http/src/h1/dispatcher.rs | 10 ++++++++-- actix-http/tests/test_server.rs | 28 ++++++++++++++-------------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/actix-http/src/h1/dispatcher.rs b/actix-http/src/h1/dispatcher.rs index 2581504cd..2e8d39def 100644 --- a/actix-http/src/h1/dispatcher.rs +++ b/actix-http/src/h1/dispatcher.rs @@ -195,6 +195,7 @@ impl TimerState { }; } + #[allow(dead_code)] fn reset(&mut self, deadline: Instant, line: u32) { if !self.is_enabled() { log::warn!("resetting disabled timer from line {}", line); @@ -999,7 +1000,8 @@ where ); debug_assert!( this.state.is_none(), - "dispatcher should not be in keep-alive phase if state is not none", + "dispatcher should not be in keep-alive phase if state is not none: {:?}", + this.state, ); debug_assert!( this.write_buf.is_empty(), @@ -1134,6 +1136,8 @@ where Poll::Ready(Ok(n)) => { log::trace!(" read {} bytes", n); + this.flags.remove(Flags::FINISHED); + if n == 0 { log::trace!(" signalling should_disconnect"); return Ok(true); @@ -1141,10 +1145,12 @@ where read_some = true; } + Poll::Pending => { log::trace!(" read pending"); return Ok(false); } + Poll::Ready(Err(err)) => { log::trace!(" read err: {:?}", &err); @@ -1259,7 +1265,7 @@ where PollResponse::DrainWriteBuf => true, PollResponse::DoNothing => { - if inner.flags.contains(Flags::KEEP_ALIVE) { + if inner.flags.contains(Flags::FINISHED | Flags::KEEP_ALIVE) { if let Some(deadline) = inner.config.keep_alive_timer() { log::trace!("setting keep-alive timer"); inner.as_mut().project().ka_timer.set_and_init( diff --git a/actix-http/tests/test_server.rs b/actix-http/tests/test_server.rs index 56ae74c96..ec4795d8a 100644 --- a/actix-http/tests/test_server.rs +++ b/actix-http/tests/test_server.rs @@ -198,8 +198,6 @@ async fn test_chunked_payload() { #[actix_rt::test] async fn slow_request_close() { - let _ = env_logger::try_init(); - let mut srv = test_server(|| { HttpService::build() .client_timeout(200) @@ -213,18 +211,21 @@ async fn slow_request_close() { let mut stream = net::TcpStream::connect(srv.addr()).unwrap(); - sleep(Duration::from_secs(1)).await; - - let mut data = Vec::new(); - let _ = stream.read(&mut data).unwrap(); + let mut data = String::new(); + let _ = stream.read_to_string(&mut data).unwrap(); assert!( - data.is_empty(), - "connection should close without writing a response" + data.starts_with("HTTP/1.1 408 Request Timeout"), + "response was not 408: {}", + data ); - let end = Instant::now(); + let diff = Instant::now().duration_since(start); - if end.duration_since(start) > Duration::from_secs(1) { + if diff < Duration::from_secs(1) { + // test success + } else if diff < Duration::from_secs(3) { + panic!("request seems to have wrongly timed-out according to keep-alive"); + } else { panic!("request took way too long to time out"); } @@ -232,7 +233,7 @@ async fn slow_request_close() { } #[actix_rt::test] -async fn test_slow_request_408() { +async fn slow_request_408() { let mut srv = test_server(|| { HttpService::build() .client_timeout(200) @@ -245,7 +246,7 @@ async fn test_slow_request_408() { let start = Instant::now(); let mut stream = net::TcpStream::connect(srv.addr()).unwrap(); - let _ = stream.write_all(b"GET /test/tests/test HTTP/1.1\r\n"); + let _ = stream.write_all(b"GET /test HTTP/1.1\r\n"); let mut data = String::new(); let _ = stream.read_to_string(&mut data); assert!( @@ -254,8 +255,7 @@ async fn test_slow_request_408() { data ); - let end = Instant::now(); - let diff = end - start; + let diff = Instant::now().duration_since(start); if diff < Duration::from_secs(1) { // test success