From 139788936928ea53bccea11564c27017f83b25ff Mon Sep 17 00:00:00 2001 From: Mollusk Date: Tue, 2 Jun 2026 05:49:40 -0400 Subject: [PATCH] test(jitter): seq_before wraparound + buffer overflow/resync MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add unit tests for the jitter buffer's untested pure logic: seq_before's wrapping u32 comparison — basic ordering, the u32::MAX→0 forward wrap, and the exact < (1<<31) half-range tipping point (0x7FFF_FFFF before, 0x8000_0000 not) — and insert's MAX_BUFFERED_FRAMES overflow path (caps depth, drops the oldest frame, resyncs the playout head next_seq to the new front). Tests-only; no production change. Implemented by Gemini (junior implementer), reviewed and verified by senior (cargo build + clippy --all-targets + cargo test all green). Co-Authored-By: Claude Opus 4.8 --- src/core/jitter.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/core/jitter.rs b/src/core/jitter.rs index 72ac0f1..2e3672e 100644 --- a/src/core/jitter.rs +++ b/src/core/jitter.rs @@ -189,4 +189,38 @@ mod tests { jb.insert(5, frame(&mut enc, 600)); assert_eq!(jb.packets.len(), 1); // only seq 7 remains buffered } + + #[test] + fn test_seq_before_ordering() { + // Basic ordering + assert!(seq_before(0, 1)); + assert!(!seq_before(1, 0)); + assert!(!seq_before(5, 5)); + assert!(seq_before(100, 101)); + assert!(!seq_before(101, 100)); + + // Wraparound + assert!(seq_before(u32::MAX, 0)); + assert!(!seq_before(0, u32::MAX)); + + // Half-range boundary + assert!(seq_before(0, 0x7FFF_FFFF)); + assert!(!seq_before(0, 0x8000_0000)); + } + + #[test] + fn test_jitter_buffer_overflow_resync() { + let mut jb = JitterBuffer::new().unwrap(); + assert!(jb.next_seq.is_none()); + + let count = MAX_BUFFERED_FRAMES + 1; + for seq in 0..count { + jb.insert(seq as u32, vec![0u8]); + } + + assert_eq!(jb.packets.len(), MAX_BUFFERED_FRAMES); + assert!(!jb.packets.contains_key(&0)); + assert_eq!(jb.next_seq, Some(1)); + } } +