lib.rs (6406B)
1 #![forbid(unsafe_code)] 2 3 // Execute the production module, without substituting a native runtime or 4 // claiming that this isolated host check qualifies the full Nostr transport. 5 #[path = "../../../src/host_time.rs"] 6 mod host_time; 7 8 use core::{cell::Cell, future::pending, time::Duration}; 9 use futures::FutureExt; 10 use std::rc::Rc; 11 use wasm_bindgen::prelude::*; 12 13 struct OnDrop(Rc<Cell<bool>>); 14 15 impl Drop for OnDrop { 16 fn drop(&mut self) { 17 self.0.set(true); 18 } 19 } 20 21 fn require(value: bool, name: &str) -> Result<(), JsValue> { 22 if value { 23 Ok(()) 24 } else { 25 Err(JsValue::from_str(name)) 26 } 27 } 28 29 #[wasm_bindgen] 30 pub async fn verify_delayed_timer_owner() -> Result<(), JsValue> { 31 use host_time::{Instant, timeout_at}; 32 let deadline = Instant::now() + Duration::from_millis(5); 33 require( 34 timeout_at(deadline, async { 35 // Deliberately occupy this poll until the original deadline is past. 36 // The separate timer owner must schedule zero remaining time, rather 37 // than granting another relative budget when it eventually runs. 38 while Instant::now() < deadline + Duration::from_millis(15) {} 39 pending::<()>().await; 40 }) 41 .await 42 .is_err(), 43 "delayed timer owner expiry", 44 ) 45 } 46 47 #[wasm_bindgen] 48 pub async fn verify_long_timer_owner() -> Result<(), JsValue> { 49 long_timer(u64::from(i32::MAX as u32) + 100).await 50 } 51 52 #[wasm_bindgen] 53 pub async fn verify_extreme_timer_owner() -> Result<(), JsValue> { 54 long_timer(u64::MAX).await 55 } 56 57 async fn long_timer(millis: u64) -> Result<(), JsValue> { 58 let dropped = Rc::new(Cell::new(false)); 59 let guard = OnDrop(Rc::clone(&dropped)); 60 let mut operation = Box::pin(host_time::timeout( 61 Duration::from_millis(millis), 62 async move { 63 let _guard = guard; 64 pending::<()>().await; 65 }, 66 )); 67 require( 68 operation.as_mut().now_or_never().is_none(), 69 "long timer starts pending", 70 )?; 71 async_utility::time::sleep(Duration::ZERO).await; 72 require( 73 operation.as_mut().now_or_never().is_none(), 74 "long timer cannot expire immediately", 75 )?; 76 drop(operation); 77 require(dropped.get(), "long observer cancellation drops work")?; 78 async_utility::time::sleep(Duration::ZERO).await; 79 Ok(()) 80 } 81 82 #[wasm_bindgen] 83 pub async fn verify_fractional_deadlines() -> Result<(), JsValue> { 84 for _ in 0..8 { 85 let deadline = host_time::Instant::now() + Duration::from_micros(1_500); 86 require( 87 host_time::timeout_at(deadline, pending::<()>()) 88 .await 89 .is_err(), 90 "fractional expiry", 91 )?; 92 require( 93 host_time::Instant::now() >= deadline, 94 "timer cannot expire early", 95 )?; 96 } 97 Ok(()) 98 } 99 100 #[wasm_bindgen] 101 pub async fn verify_host_time() -> Result<js_sys::Array, JsValue> { 102 use host_time::{Instant, timeout, timeout_at, unix_time_ms}; 103 let passed = js_sys::Array::new(); 104 let start = Instant::now(); 105 let now = unix_time_ms(); 106 require( 107 now.abs_diff(js_sys::Date::now() as u64) < 1_000, 108 "browser wall clock", 109 )?; 110 passed.push(&"browser wall clock".into()); 111 112 let local = Rc::new(Cell::new(7)); 113 let expired = Instant::now(); 114 require( 115 timeout_at(expired, async { local.get() }).await.ok() == Some(7), 116 "expired ready local future", 117 )?; 118 passed.push(&"expired ready local future".into()); 119 120 struct DeferredReady(Rc<Cell<bool>>); 121 impl core::future::IntoFuture for DeferredReady { 122 type Output = u32; 123 type IntoFuture = futures::future::Ready<u32>; 124 fn into_future(self) -> Self::IntoFuture { 125 self.0.set(true); 126 futures::future::ready(7) 127 } 128 } 129 let converted = Rc::new(Cell::new(false)); 130 let builder = timeout_at(expired, DeferredReady(Rc::clone(&converted))); 131 require(!converted.get(), "unpolled IntoFuture builder is inert")?; 132 require( 133 builder.await.ok() == Some(7) && converted.get(), 134 "IntoFuture builder timeout", 135 )?; 136 passed.push(&"local IntoFuture builder".into()); 137 require( 138 timeout_at(expired, pending::<()>()).await.is_err(), 139 "expired pending future", 140 )?; 141 passed.push(&"expired pending future".into()); 142 143 let dropped = Rc::new(Cell::new(false)); 144 let guard = OnDrop(Rc::clone(&dropped)); 145 require( 146 timeout(Duration::from_millis(10), async move { 147 let _guard = guard; 148 pending::<()>().await; 149 }) 150 .await 151 .is_err() 152 && dropped.get(), 153 "expiry drops local work", 154 )?; 155 passed.push(&"expiry drops local work".into()); 156 157 let deadline = Instant::now() + Duration::from_millis(10); 158 async_utility::time::sleep(Duration::from_millis(20)).await; 159 // A ready custom future checks its original absolute deadline during its 160 // first poll. This does not rely on tight wall-time scheduling thresholds. 161 require( 162 timeout_at(deadline, async { Instant::now() >= deadline }) 163 .await 164 .ok() 165 == Some(true), 166 "one absolute deadline", 167 )?; 168 require( 169 timeout_at(deadline, pending::<()>()).await.is_err(), 170 "elapsed queued work", 171 )?; 172 passed.push(&"one absolute deadline".into()); 173 174 for _ in 0..16 { 175 let dropped = Rc::new(Cell::new(false)); 176 let guard = OnDrop(Rc::clone(&dropped)); 177 require( 178 timeout(Duration::from_secs(1), async move { 179 let _guard = guard; 180 pending::<()>().await; 181 }) 182 .now_or_never() 183 .is_none() 184 && dropped.get(), 185 "observer cancellation drops work", 186 )?; 187 // Give the separate timer owner an executor turn to dispose its timer. 188 async_utility::time::sleep(Duration::ZERO).await; 189 } 190 passed.push(&"repeated observer cancellation".into()); 191 require( 192 timeout(Duration::from_secs(1), async { 9 }).await.ok() == Some(9), 193 "ready after timer cancellation", 194 )?; 195 passed.push(&"ready after timer cancellation".into()); 196 require( 197 Instant::now() >= start && unix_time_ms() >= now, 198 "host clocks advance", 199 )?; 200 passed.push(&"host clocks advance".into()); 201 Ok(passed) 202 }