From b9f4d33e361c3af19932bd0520919de99b77ea3c Mon Sep 17 00:00:00 2001 From: DiscreteTom Date: Fri, 3 Jan 2025 03:06:15 +0000 Subject: [PATCH] tests: add unit tests for client idle timeout --- src/lib.rs | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 75ec1d5..9e3799a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -420,6 +420,15 @@ impl Adapter { Ok(app_response) } + + /// Return whether the client has been idle for longer than the [`Self::client_idle_timeout_ms`]. + fn client_timeout_has_expired(&self) -> bool { + self.last_invoke + .elapsed() + .map(|d| d.as_millis() > self.client_idle_timeout_ms.into()) + // if the last_invoke is in the future, it's ok to re-use the client + .unwrap_or(false) + } } /// Implement a `Tower.Service` that sends the requests @@ -434,14 +443,7 @@ impl Service for Adapter { } fn call(&mut self, event: Request) -> Self::Future { - // validate client timeout - if self - .last_invoke - .elapsed() - .map(|d| d.as_millis() > self.client_idle_timeout_ms.into()) - // if the last_invoke is in the future, it's ok to re-use the client - .unwrap_or(false) - { + if self.client_timeout_has_expired() { // client timeout, create a new client with a new connection pool. // this is to prevent the pool from using a to-be-disconnected connection after restoring from Lambda SnapStart tracing::debug!("Client timeout, creating a new client"); @@ -569,4 +571,16 @@ mod tests { // Assert app server's healthcheck endpoint got called healthcheck.assert(); } + + #[test] + fn test_client_idle_timeout() { + let mut adapter = Adapter::new(&AdapterOptions::default()); + assert!(!adapter.client_timeout_has_expired()); + + adapter.last_invoke = SystemTime::now() - Duration::from_millis(5000); + assert!(adapter.client_timeout_has_expired()); + + adapter.last_invoke = SystemTime::now() + Duration::from_millis(5000); + assert!(!adapter.client_timeout_has_expired()); + } }