Skip to content

Commit b9f4d33

Browse files
committed
tests: add unit tests for client idle timeout
1 parent 5418b73 commit b9f4d33

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/lib.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,15 @@ impl Adapter<HttpConnector, Body> {
420420

421421
Ok(app_response)
422422
}
423+
424+
/// Return whether the client has been idle for longer than the [`Self::client_idle_timeout_ms`].
425+
fn client_timeout_has_expired(&self) -> bool {
426+
self.last_invoke
427+
.elapsed()
428+
.map(|d| d.as_millis() > self.client_idle_timeout_ms.into())
429+
// if the last_invoke is in the future, it's ok to re-use the client
430+
.unwrap_or(false)
431+
}
423432
}
424433

425434
/// Implement a `Tower.Service` that sends the requests
@@ -434,14 +443,7 @@ impl Service<Request> for Adapter<HttpConnector, Body> {
434443
}
435444

436445
fn call(&mut self, event: Request) -> Self::Future {
437-
// validate client timeout
438-
if self
439-
.last_invoke
440-
.elapsed()
441-
.map(|d| d.as_millis() > self.client_idle_timeout_ms.into())
442-
// if the last_invoke is in the future, it's ok to re-use the client
443-
.unwrap_or(false)
444-
{
446+
if self.client_timeout_has_expired() {
445447
// client timeout, create a new client with a new connection pool.
446448
// this is to prevent the pool from using a to-be-disconnected connection after restoring from Lambda SnapStart
447449
tracing::debug!("Client timeout, creating a new client");
@@ -569,4 +571,16 @@ mod tests {
569571
// Assert app server's healthcheck endpoint got called
570572
healthcheck.assert();
571573
}
574+
575+
#[test]
576+
fn test_client_idle_timeout() {
577+
let mut adapter = Adapter::new(&AdapterOptions::default());
578+
assert!(!adapter.client_timeout_has_expired());
579+
580+
adapter.last_invoke = SystemTime::now() - Duration::from_millis(5000);
581+
assert!(adapter.client_timeout_has_expired());
582+
583+
adapter.last_invoke = SystemTime::now() + Duration::from_millis(5000);
584+
assert!(!adapter.client_timeout_has_expired());
585+
}
572586
}

0 commit comments

Comments
 (0)