|
| 1 | +use loro_internal::awareness::EphemeralStore; |
| 2 | +use loro_internal::LoroValue; |
| 3 | +use serde::Serialize; |
| 4 | +use std::time::{Duration, SystemTime, UNIX_EPOCH}; |
| 5 | + |
| 6 | +#[derive(Serialize)] |
| 7 | +struct WireState { |
| 8 | + key: String, |
| 9 | + value: Option<LoroValue>, |
| 10 | + timestamp: i64, |
| 11 | +} |
| 12 | + |
| 13 | +fn now_ms() -> i64 { |
| 14 | + SystemTime::now() |
| 15 | + .duration_since(UNIX_EPOCH) |
| 16 | + .unwrap() |
| 17 | + .as_millis() as i64 |
| 18 | +} |
| 19 | + |
| 20 | +#[test] |
| 21 | +fn import_skips_entries_past_timeout() { |
| 22 | + let store = EphemeralStore::new(100); |
| 23 | + let stale_timestamp = now_ms() - 500; |
| 24 | + |
| 25 | + let payload = postcard::to_allocvec(&vec![WireState { |
| 26 | + key: "stale".into(), |
| 27 | + value: Some(LoroValue::from(1)), |
| 28 | + timestamp: stale_timestamp, |
| 29 | + }]) |
| 30 | + .unwrap(); |
| 31 | + |
| 32 | + store.apply(&payload).unwrap(); |
| 33 | + |
| 34 | + assert!(store.get("stale").is_none()); |
| 35 | + assert!(store.get_all_states().is_empty()); |
| 36 | +} |
| 37 | + |
| 38 | +#[test] |
| 39 | +fn import_preserves_remote_timestamp_for_timeout() { |
| 40 | + let timeout_ms = 100; |
| 41 | + let store = EphemeralStore::new(timeout_ms); |
| 42 | + let remote_timestamp = now_ms() - (timeout_ms - 20); |
| 43 | + |
| 44 | + let payload = postcard::to_allocvec(&vec![WireState { |
| 45 | + key: "cursor".into(), |
| 46 | + value: Some(LoroValue::from("v")), |
| 47 | + timestamp: remote_timestamp, |
| 48 | + }]) |
| 49 | + .unwrap(); |
| 50 | + |
| 51 | + store.apply(&payload).unwrap(); |
| 52 | + assert_eq!(store.get("cursor"), Some(LoroValue::from("v"))); |
| 53 | + |
| 54 | + std::thread::sleep(Duration::from_millis(40)); |
| 55 | + store.remove_outdated(); |
| 56 | + |
| 57 | + assert!(store.get("cursor").is_none()); |
| 58 | + assert!(store.get_all_states().is_empty()); |
| 59 | +} |
0 commit comments