Skip to content

Add loom tests for MetaWord AtomicU64 CAS correctness #9

Description

@ddsha441981

Summary

PulseMap's MetaWord uses AtomicU64 with compare_exchange_weak in a CAS loop (on_access method) to update eviction priorities lock-free. The AccessBuffer also uses multiple atomics (AtomicU32, AtomicUsize) for its lock-free ring buffer. While these appear correct by inspection, concurrent atomics are notoriously difficult to reason about. loom can systematically explore all possible thread interleavings to formally verify correctness.

What To Do

1. Add loom as a dev-dependency

[dev-dependencies]
loom = "0.7"

2. Create tests/loom_meta.rs

Test the MetaWord::on_access CAS loop under concurrent access:

  • Spawn 2-3 loom threads that simultaneously call on_access() on different slots of the same MetaWord
  • After all threads complete, verify:
    • All slot states are still valid (no corrupted bits)
    • Frequency counters reflect the total number of accesses (no lost updates)
    • Recency values are within valid range (0-7)

3. Create tests/loom_access_buffer.rs

Test the AccessBuffer push/drain under concurrent access:

  • Spawn 2 producer threads calling push() and 1 consumer calling drain()
  • Verify no events are duplicated (each event drained at most once)
  • Verify the lossy property: dropped events are acceptable, corrupted events are not

Key Considerations

  • loom replaces std::sync::atomic with its own instrumented version. You may need to add #[cfg(loom)] conditional compilation to swap atomic imports in meta.rs and access_buffer.rs.
  • Keep the state space small: use 2-3 threads and minimal iterations. Loom explores ALL interleavings, so large state spaces will take too long.
  • Reference: loom documentation

Acceptance Criteria

  • cargo test --test loom_meta passes (loom explores all interleavings without panic)
  • cargo test --test loom_access_buffer passes
  • No modifications to the core engine logic — only add #[cfg(loom)] import swaps if needed
  • Tests complete in under 5 minutes on a standard machine

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions