use std::time::Duration;
#[cfg(feature = "fake-clock")]
use sn_fake_clock::FakeClock as Instant;
use qlrumap::LruMap;
#[cfg(test)]
fn delay(secs: u64) {
#[cfg(not(feature = "sn_fake_clock"))]
std::thread::sleep(Duration::from_secs(secs));
#[cfg(feature = "sn_fake_clock")]
Instant::advance_time(secs * 1000);
}
#[test]
fn single_entry() {
let ttl = Duration::from_secs(1);
let mut map = LruMap::with_ttl(ttl);
map.insert("hello", "world");
delay(2);
let mut drain = map.drain_timedout();
assert_eq!(drain.next(), Some(("hello", "world")));
assert_eq!(drain.next(), None);
}
#[test]
fn collect() {
let ttl = Duration::from_secs(1);
let mut map = LruMap::with_ttl(ttl);
map.insert("hello", "world");
delay(2);
let drain = map.drain_timedout();
let v: Vec<(&str, &str)> = drain.collect();
assert_eq!(v.len(), 1);
assert_eq!(v[0].0, "hello");
assert_eq!(v[0].1, "world");
}
#[test]
fn step_by_step() {
let ttl = Duration::from_secs(3);
let mut map = LruMap::with_ttl(ttl);
map.insert("hello", "world");
delay(1);
map.insert("moo", "cow");
delay(1);
let mut drain = map.drain_timedout();
assert_eq!(drain.next(), None);
drop(drain);
delay(1);
let mut drain = map.drain_timedout();
assert_eq!(drain.next(), Some(("hello", "world")));
assert_eq!(drain.next(), None);
drop(drain);
delay(1);
let mut drain = map.drain_timedout();
assert_eq!(drain.next(), Some(("moo", "cow")));
assert_eq!(drain.next(), None);
drop(drain);
assert!(map.is_empty());
}