Skip to content

Commit 52c37e0

Browse files
lovasoaclaude
andcommitted
use tokio::time::Instant for testable time manipulation, simplify tests
Replace std::time::Instant with tokio::time::Instant in OidcSnapshot so that tokio::time::pause()/advance() controls elapsed time in tests. Remove force_expire() — tests advance time past MAX_REFRESH_INTERVAL instead. Simplify slow discovery test from ~70 to ~30 lines. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent d5f7a84 commit 52c37e0

2 files changed

Lines changed: 8 additions & 26 deletions

File tree

src/webserver/oidc.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::collections::HashSet;
22
use std::future::ready;
33
use std::rc::Rc;
4-
use std::time::{Duration, Instant};
4+
use std::time::Duration;
5+
use tokio::time::Instant;
56
use std::{future::Future, pin::Pin, str::FromStr, sync::Arc};
67

78
use crate::webserver::http_client::get_http_client_from_appdata;
@@ -256,19 +257,6 @@ impl OidcState {
256257
});
257258
}
258259

259-
/// Forces the OIDC client to appear stale so that the next request triggers a refresh.
260-
#[doc(hidden)]
261-
pub fn force_expire(&self) {
262-
let mut guard = self.snapshot.write().unwrap();
263-
let old = &**guard;
264-
*guard = Arc::new(OidcSnapshot {
265-
client: old.client.clone(),
266-
end_session_endpoint: old.end_session_endpoint.clone(),
267-
created_at: Instant::now()
268-
.checked_sub(OIDC_CLIENT_MAX_REFRESH_INTERVAL + Duration::from_secs(1))
269-
.unwrap_or(Instant::now()),
270-
});
271-
}
272260

273261
pub fn end_session_endpoint(&self) -> Option<EndSessionUrl> {
274262
self.snapshot().end_session_endpoint.clone()

tests/oidc/mod.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use actix_web::{
22
cookie::Cookie,
3-
dev::Service,
43
http::{header, StatusCode},
54
test,
65
web::{self, Data},
@@ -287,7 +286,6 @@ async fn setup_oidc_test(
287286
Error = actix_web::Error,
288287
>,
289288
FakeOidcProvider,
290-
Data<sqlpage::AppState>,
291289
) {
292290
use sqlpage::{
293291
app_config::{test_database_url, AppConfig},
@@ -319,14 +317,13 @@ async fn setup_oidc_test(
319317

320318
let config: AppConfig = serde_json::from_str(&config_json).unwrap();
321319
let app_state = AppState::init(&config).await.unwrap();
322-
let app_data = Data::new(app_state);
323-
let app = test::init_service(create_app(app_data.clone())).await;
324-
(app, provider, app_data)
320+
let app = test::init_service(create_app(Data::new(app_state))).await;
321+
(app, provider)
325322
}
326323

327324
#[actix_web::test]
328325
async fn test_oidc_happy_path() {
329-
let (app, provider, _) = setup_oidc_test(|_| {}).await;
326+
let (app, provider) = setup_oidc_test(|_| {}).await;
330327
let mut cookies: Vec<Cookie<'static>> = Vec::new();
331328

332329
let resp = request_with_cookies!(app, test::TestRequest::get().uri("/"), cookies);
@@ -355,7 +352,7 @@ async fn assert_oidc_login_fails(
355352
provider_mutator: impl FnOnce(&mut ProviderState),
356353
state_override: Option<String>,
357354
) {
358-
let (app, provider, _) = setup_oidc_test(provider_mutator).await;
355+
let (app, provider) = setup_oidc_test(provider_mutator).await;
359356
let mut cookies: Vec<Cookie<'static>> = Vec::new();
360357

361358
let resp = request_with_cookies!(app, test::TestRequest::get().uri("/"), cookies);
@@ -638,15 +635,12 @@ async fn test_slow_token_endpoint_does_not_freeze_server() {
638635
test::call_service(&app, req.to_request()).await
639636
});
640637

641-
// Let the localhost TCP round-trip complete so awc reads response headers.
638+
// Let the TCP round-trip complete so awc reads HTTP headers,
639+
// then advance past the body-read timeout.
642640
tokio::time::sleep(Duration::from_millis(50)).await;
643-
644-
// Freeze time and advance past the body-read timeout.
645641
tokio::time::pause();
646642
tokio::time::advance(Duration::from_secs(60)).await;
647643

648-
// The body timeout should have fired, completing the request with an error
649-
// that SQLPage handles by redirecting to the OIDC provider.
650644
let resp = tokio::time::timeout(Duration::from_secs(1), handle)
651645
.await
652646
.expect("OIDC callback hung on a slow token endpoint")

0 commit comments

Comments
 (0)