Skip to content

Commit

Permalink
Fetching timers test by fetch or swap
Browse files Browse the repository at this point in the history
  • Loading branch information
Hasan6979 committed Dec 17, 2024
1 parent 4ba9f5e commit 09834fe
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions neptun/src/noise/timers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ mod tests {
}

#[test]
fn test_mark_timers_during_update() {
fn test_fetching_timers_by_swap_vs_load() {
let my_secret_key = x25519_dalek::StaticSecret::random_from_rng(OsRng);
let my_idx = OsRng.next_u32();

Expand All @@ -483,11 +483,13 @@ mod tests {
// Mark timers to update
my_tun.mark_timer_to_update(super::TimerName::TimeLastDataPacketSent);

// Fetch by swap
let timer_mask = my_tun
.timers
.timers_to_update_mask
.swap(0, std::sync::atomic::Ordering::Relaxed);

// Timer marked after fetch
my_tun.mark_timer_to_update(super::TimerName::TimeLastDataPacketReceived);

my_tun.tick_marked_timers(timer_mask);
Expand All @@ -496,13 +498,37 @@ mod tests {
assert!(!my_tun.timers[TimerName::TimeLastDataPacketSent].is_zero());
assert!(my_tun.timers[TimerName::TimeLastDataPacketReceived].is_zero());

my_tun.update_timers(&mut [0]);

// Previously marked TimeLastDataPacketReceivedOnly updated in next cycle
assert!(!my_tun.timers[TimerName::TimeLastDataPacketReceived].is_zero());

// Reset the timers
my_tun.timers[TimerName::TimeLastDataPacketSent] = SafeDuration::from_millis(0);

my_tun.update_timers(&mut [0]);
// Fetch timers by load
let timer_mask = my_tun
.timers
.timers_to_update_mask
.load(std::sync::atomic::Ordering::Relaxed);

// Now the timers should not update
// Timer marked after fetch
my_tun.mark_timer_to_update(super::TimerName::TimeLastDataPacketSent);

my_tun.tick_marked_timers(timer_mask);

my_tun
.timers
.timers_to_update_mask
.store(0, std::sync::atomic::Ordering::Relaxed);

// Only TimeLastDataPacketReceived udpated
assert!(my_tun.timers[TimerName::TimeLastDataPacketSent].is_zero());
assert!(!my_tun.timers[TimerName::TimeLastDataPacketReceived].is_zero());

my_tun.update_timers(&mut [0]);

// TimeLastDataPacketSent timer still not updated
assert!(my_tun.timers[TimerName::TimeLastDataPacketSent].is_zero());
}
}

0 comments on commit 09834fe

Please sign in to comment.