Skip to content

Commit 09834fe

Browse files
committed
Fetching timers test by fetch or swap
1 parent 4ba9f5e commit 09834fe

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

neptun/src/noise/timers.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ mod tests {
470470
}
471471

472472
#[test]
473-
fn test_mark_timers_during_update() {
473+
fn test_fetching_timers_by_swap_vs_load() {
474474
let my_secret_key = x25519_dalek::StaticSecret::random_from_rng(OsRng);
475475
let my_idx = OsRng.next_u32();
476476

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

486+
// Fetch by swap
486487
let timer_mask = my_tun
487488
.timers
488489
.timers_to_update_mask
489490
.swap(0, std::sync::atomic::Ordering::Relaxed);
490491

492+
// Timer marked after fetch
491493
my_tun.mark_timer_to_update(super::TimerName::TimeLastDataPacketReceived);
492494

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

501+
my_tun.update_timers(&mut [0]);
502+
503+
// Previously marked TimeLastDataPacketReceivedOnly updated in next cycle
504+
assert!(!my_tun.timers[TimerName::TimeLastDataPacketReceived].is_zero());
505+
499506
// Reset the timers
500507
my_tun.timers[TimerName::TimeLastDataPacketSent] = SafeDuration::from_millis(0);
501508

502-
my_tun.update_timers(&mut [0]);
509+
// Fetch timers by load
510+
let timer_mask = my_tun
511+
.timers
512+
.timers_to_update_mask
513+
.load(std::sync::atomic::Ordering::Relaxed);
503514

504-
// Now the timers should not update
515+
// Timer marked after fetch
516+
my_tun.mark_timer_to_update(super::TimerName::TimeLastDataPacketSent);
517+
518+
my_tun.tick_marked_timers(timer_mask);
519+
520+
my_tun
521+
.timers
522+
.timers_to_update_mask
523+
.store(0, std::sync::atomic::Ordering::Relaxed);
524+
525+
// Only TimeLastDataPacketReceived udpated
505526
assert!(my_tun.timers[TimerName::TimeLastDataPacketSent].is_zero());
506527
assert!(!my_tun.timers[TimerName::TimeLastDataPacketReceived].is_zero());
528+
529+
my_tun.update_timers(&mut [0]);
530+
531+
// TimeLastDataPacketSent timer still not updated
532+
assert!(my_tun.timers[TimerName::TimeLastDataPacketSent].is_zero());
507533
}
508534
}

0 commit comments

Comments
 (0)