localauthentication/
timeout.rs1use std::time::Duration;
2
3use crate::ffi;
4
5pub fn set_sync_timeout(timeout: Option<Duration>) {
6 let seconds = timeout.map_or(f64::INFINITY, |timeout| timeout.as_secs_f64());
7 unsafe { ffi::la_set_sync_timeout(seconds) };
8}
9
10#[must_use]
11pub fn sync_timeout() -> Option<Duration> {
12 let seconds = unsafe { ffi::la_get_sync_timeout() };
13 seconds
14 .is_finite()
15 .then(|| Duration::try_from_secs_f64(seconds).unwrap_or(Duration::MAX))
16}
17
18#[cfg(test)]
19mod tests {
20 use core::ffi::c_char;
21 use std::ptr;
22 use std::sync::atomic::{AtomicUsize, Ordering};
23 use std::sync::Mutex;
24 use std::thread;
25 use std::time::{Duration, Instant};
26
27 use super::{set_sync_timeout, sync_timeout};
28 use crate::la_error::{from_status, LAError};
29
30 static SERIAL: Mutex<()> = Mutex::new(());
31 static LATE_SUCCESSES: AtomicUsize = AtomicUsize::new(0);
32
33 unsafe extern "C" {
34 fn la_bridge_probe_await(
35 delay_milliseconds: u32,
36 late_success: Option<unsafe extern "C" fn()>,
37 out_timeout_hook_ran: *mut u8,
38 error_out: *mut *mut c_char,
39 ) -> i32;
40 }
41
42 unsafe extern "C" fn record_late_success() {
43 LATE_SUCCESSES.fetch_add(1, Ordering::SeqCst);
44 }
45
46 fn probe(delay_milliseconds: u32) -> (Result<(), LAError>, bool) {
47 let mut hook_ran = 0_u8;
48 let mut error = ptr::null_mut();
49 let status = unsafe {
50 la_bridge_probe_await(
51 delay_milliseconds,
52 Some(record_late_success),
53 &raw mut hook_ran,
54 &raw mut error,
55 )
56 };
57 let result = if status == crate::ffi::status::OK {
58 Ok(())
59 } else {
60 Err(from_status(status, error))
61 };
62 (result, hook_ran != 0)
63 }
64
65 #[test]
66 fn the_timeout_is_configurable_and_defaults_to_thirty_seconds() {
67 let _serial = SERIAL
68 .lock()
69 .unwrap_or_else(std::sync::PoisonError::into_inner);
70 assert_eq!(sync_timeout(), Some(Duration::from_secs(30)));
71 set_sync_timeout(Some(Duration::from_millis(1500)));
72 assert_eq!(sync_timeout(), Some(Duration::from_millis(1500)));
73 set_sync_timeout(None);
74 assert_eq!(sync_timeout(), None);
75 set_sync_timeout(Some(Duration::from_secs(30)));
76 }
77
78 #[test]
79 fn timed_out_calls_are_cancelled_and_late_results_still_run_their_hook() {
80 let _serial = SERIAL
81 .lock()
82 .unwrap_or_else(std::sync::PoisonError::into_inner);
83 let before = LATE_SUCCESSES.load(Ordering::SeqCst);
84 set_sync_timeout(Some(Duration::from_millis(50)));
85 let started = Instant::now();
86 let (result, hook_ran) = probe(400);
87 set_sync_timeout(Some(Duration::from_secs(30)));
88
89 assert!(matches!(result, Err(LAError::TimedOut(_))), "{result:?}");
90 assert!(hook_ran);
91 assert!(started.elapsed() < Duration::from_millis(350));
92
93 let deadline = Instant::now() + Duration::from_secs(5);
94 while LATE_SUCCESSES.load(Ordering::SeqCst) == before && Instant::now() < deadline {
95 thread::sleep(Duration::from_millis(20));
96 }
97 assert_eq!(LATE_SUCCESSES.load(Ordering::SeqCst), before + 1);
98 }
99
100 #[test]
101 fn calls_that_finish_in_time_skip_both_hooks() {
102 let _serial = SERIAL
103 .lock()
104 .unwrap_or_else(std::sync::PoisonError::into_inner);
105 let before = LATE_SUCCESSES.load(Ordering::SeqCst);
106 let (result, hook_ran) = probe(10);
107 assert!(result.is_ok(), "{result:?}");
108 assert!(!hook_ran);
109 thread::sleep(Duration::from_millis(100));
110 assert_eq!(LATE_SUCCESSES.load(Ordering::SeqCst), before);
111 }
112}