use std::time::Duration;
use crate::audit::{AuditEvent, AuditSink};
use crate::session::SessionStore;
pub fn sweep_expired_sessions(store: &SessionStore, audit: &AuditSink, ttl: Duration) -> usize {
let Ok(expired) = store.sweep_idle(ttl) else {
return 0;
};
for id in &expired {
audit.record(AuditEvent::new("session.expired").with_session(id.as_u64()));
}
expired.len()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::session::SessionState;
#[test]
fn the_sweep_reports_what_it_removed() {
let store = SessionStore::new();
let busy = store.create().expect("create");
store.create().expect("create");
store
.update(&busy, |s| {
let _ = s.state.transition_to(SessionState::Active);
})
.expect("busy");
let swept = sweep_expired_sessions(&store, &AuditSink::Disabled, Duration::ZERO);
assert_eq!(swept, 1, "only the session with no command in it may go");
assert!(
store.contains(&busy).expect("lock"),
"a session running a command must survive"
);
}
}