Skip to main content

qualia_core_db/net/peer/runtime/
generations.rs

1//! Non-wrapping slot generations. Exhaustion retires the slot; it never wraps
2//! into a previously valid handle.
3
4use crate::net::qdnf::errors::QdnfError;
5use crate::net::qdnf::types::Generation;
6
7/// Advance a stored slot generation. `u64::MAX` fails closed as stale.
8#[inline]
9pub const fn bump_generation(current: Generation) -> Result<Generation, QdnfError> {
10    current.next()
11}
12
13#[cfg(test)]
14mod tests {
15    use super::*;
16    use crate::net::peer::runtime::leases::{LeaseTable, LEASE_SLOTS};
17
18    #[test]
19    fn slot_at_max_next_fails_stale_generation() {
20        assert_eq!(
21            bump_generation(Generation(u64::MAX)),
22            Err(QdnfError::StaleGeneration)
23        );
24        let mut table = LeaseTable::new();
25        table.debug_set_generation(0, Generation(u64::MAX));
26        assert_eq!(
27            table.try_acquire_slot(0, 8, true),
28            Err(QdnfError::StaleGeneration)
29        );
30    }
31
32    #[test]
33    fn acquire_skips_retired_slot_and_never_wraps() {
34        let mut table = LeaseTable::new();
35        table.debug_set_generation(0, Generation(u64::MAX));
36        let lease = table.acquire(8, true).unwrap();
37        assert_eq!(lease.handle.slot, 1);
38        assert_ne!(lease.handle.generation, Generation(0));
39        assert_ne!(lease.handle.generation, Generation(u64::MAX));
40    }
41
42    #[test]
43    fn all_slots_exhausted_is_stale_generation() {
44        let mut table = LeaseTable::new();
45        for i in 0..LEASE_SLOTS {
46            table.debug_set_generation(i, Generation(u64::MAX));
47        }
48        assert_eq!(table.acquire(8, true), Err(QdnfError::StaleGeneration));
49    }
50
51    #[test]
52    fn released_handle_is_not_live_at_old_generation() {
53        let mut table = LeaseTable::new();
54        let first = table.acquire(8, true).unwrap();
55        table.release(first.handle).unwrap();
56        let second = table.acquire(8, true).unwrap();
57        assert_eq!(second.handle.slot, first.handle.slot);
58        assert_ne!(second.handle.generation, first.handle.generation);
59        assert_eq!(table.release(first.handle), Err(QdnfError::StaleGeneration));
60        table.release(second.handle).unwrap();
61    }
62}