From daab095637f4cae3bc14d5b8673f81ecfc5cd305 Mon Sep 17 00:00:00 2001 From: piquel Date: Sat, 21 Nov 2026 19:34:20 +0100 Subject: [PATCH] fix(auth): the rate-limit deadline test gives the request after the lock its usual deadline The test bounded both requests by the 1-second deadline it set for the blocked one, so on a busy machine the request after the lock was released could take longer and get a 504. That request now goes through the fixture's own deadline; the blocked one still gets one second. Closes #658 --- .../src/security/tests/rate_limits.rs | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/apps/auth-service/src/security/tests/rate_limits.rs b/apps/auth-service/src/security/tests/rate_limits.rs index bab8f9e90..f84605329 100644 --- a/apps/auth-service/src/security/tests/rate_limits.rs +++ b/apps/auth-service/src/security/tests/rate_limits.rs @@ -17,15 +17,17 @@ async fn rate_limit_deadline_includes_a_blocked_database_query() { .allow_request(bucket.as_bytes(), 120, std::time::Duration::from_mins(1)) .await .expect("hold bucket lock"); - // The deadline also bounds the request after the lock is released, so it - // leaves room for queries while other database tests run. - fixture.policy.request_timeout = std::time::Duration::from_secs(1); - fixture.router = Router::new() - .route("/oauth/token", any(|| async { StatusCode::ACCEPTED })) - .layer(middleware::from_fn_with_state( - fixture.policy.clone(), - HttpSecurity::protect, - )); + let token_route = |policy: HttpSecurity| { + Router::new() + .route("/oauth/token", any(|| async { StatusCode::ACCEPTED })) + .layer(middleware::from_fn_with_state( + policy, + HttpSecurity::protect, + )) + }; + let mut short = fixture.policy.clone(); + short.request_timeout = std::time::Duration::from_secs(1); + fixture.router = token_route(short); let response = tokio::time::timeout( std::time::Duration::from_secs(5), fixture.request(Method::POST, "/oauth/token", None, false), @@ -34,6 +36,9 @@ async fn rate_limit_deadline_includes_a_blocked_database_query() { .expect("whole request is bounded"); assert_eq!(response.status(), StatusCode::GATEWAY_TIMEOUT); transaction.rollback().await.expect("release lock"); + // Once the lock is free, the fixture's own deadline applies: a request on a + // busy machine may take longer than the second the blocked one had. + fixture.router = token_route(fixture.policy.clone()); assert_eq!( fixture .request(Method::POST, "/oauth/token", None, false)