host_time.rs (3360B)
1 //! Host clocks and deadlines shared by the Nostr session adapters. 2 3 use core::time::Duration; 4 5 #[cfg(not(target_arch = "wasm32"))] 6 pub(crate) use tokio::time::{Instant, timeout, timeout_at}; 7 #[cfg(target_arch = "wasm32")] 8 #[path = "host_time_browser.rs"] 9 mod browser; 10 #[cfg(target_arch = "wasm32")] 11 pub(crate) use browser::{Instant, timeout, timeout_at}; 12 13 #[cfg_attr(coverage_nightly, coverage(off))] 14 pub(crate) fn unix_time_ms() -> u64 { 15 #[cfg(not(target_arch = "wasm32"))] 16 use std::time::{SystemTime, UNIX_EPOCH}; 17 #[cfg(target_arch = "wasm32")] 18 use web_time::{SystemTime, UNIX_EPOCH}; 19 20 wall_millis(SystemTime::now().duration_since(UNIX_EPOCH).ok()) 21 } 22 23 fn wall_millis(since_epoch: Option<Duration>) -> u64 { 24 since_epoch 25 .map(|duration| u64::try_from(duration.as_millis()).unwrap_or(u64::MAX)) 26 .unwrap_or_default() 27 } 28 29 #[cfg(all(test, not(target_arch = "wasm32")))] 30 mod tests { 31 use super::*; 32 use core::{cell::Cell, future::pending}; 33 use futures::FutureExt; 34 use std::rc::Rc; 35 36 struct OnDrop(Rc<Cell<bool>>); 37 38 impl Drop for OnDrop { 39 fn drop(&mut self) { 40 self.0.set(true); 41 } 42 } 43 44 #[test] 45 fn wall_clock_conversion_preserves_unavailable_and_saturated_values() { 46 assert_eq!(wall_millis(None), 0); 47 assert_eq!(wall_millis(Some(Duration::ZERO)), 0); 48 assert_eq!(wall_millis(Some(Duration::from_micros(1999))), 1); 49 assert_eq!(wall_millis(Some(Duration::MAX)), u64::MAX); 50 } 51 52 #[tokio::test(start_paused = true)] 53 async fn ready_local_future_wins_even_at_an_expired_deadline() { 54 let local = Rc::new(Cell::new(7)); 55 let deadline = Instant::now() - Duration::from_secs(1); 56 assert_eq!( 57 timeout_at(deadline, async { local.get() }).await.unwrap(), 58 7 59 ); 60 } 61 62 #[tokio::test(start_paused = true)] 63 async fn expiry_drops_pending_local_work() { 64 let dropped = Rc::new(Cell::new(false)); 65 let guard = OnDrop(Rc::clone(&dropped)); 66 assert!( 67 timeout(Duration::from_millis(10), async move { 68 let _guard = guard; 69 pending::<()>().await; 70 }) 71 .await 72 .is_err() 73 ); 74 assert!(dropped.get()); 75 } 76 77 #[tokio::test(start_paused = true)] 78 async fn cancellation_drops_work_and_a_fresh_timeout_still_runs() { 79 let dropped = Rc::new(Cell::new(false)); 80 let guard = OnDrop(Rc::clone(&dropped)); 81 assert!( 82 timeout(Duration::from_secs(1), async move { 83 let _guard = guard; 84 pending::<()>().await; 85 }) 86 .now_or_never() 87 .is_none() 88 ); 89 assert!(dropped.get()); 90 assert_eq!( 91 timeout(Duration::from_secs(1), async { 9 }).await.unwrap(), 92 9 93 ); 94 } 95 96 #[tokio::test(start_paused = true)] 97 async fn queued_work_consumes_one_original_deadline() { 98 let started = Instant::now(); 99 let deadline = started + Duration::from_millis(20); 100 tokio::time::sleep(Duration::from_millis(15)).await; 101 assert!(timeout_at(deadline, pending::<()>()).await.is_err()); 102 assert_eq!(Instant::now() - started, Duration::from_millis(20)); 103 assert!(timeout_at(deadline, pending::<()>()).await.is_err()); 104 } 105 }