Skip to content

Commit

Permalink
tests: add unit tests for client idle timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
DiscreteTom committed Jan 3, 2025
1 parent 5418b73 commit b9f4d33
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,15 @@ impl Adapter<HttpConnector, Body> {

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
Expand All @@ -434,14 +443,7 @@ impl Service<Request> for Adapter<HttpConnector, Body> {
}

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

0 comments on commit b9f4d33

Please sign in to comment.