@@ -470,7 +470,7 @@ mod tests {
470
470
}
471
471
472
472
#[ test]
473
- fn test_mark_timers_during_update ( ) {
473
+ fn test_fetching_timers_by_swap_vs_load ( ) {
474
474
let my_secret_key = x25519_dalek:: StaticSecret :: random_from_rng ( OsRng ) ;
475
475
let my_idx = OsRng . next_u32 ( ) ;
476
476
@@ -483,11 +483,13 @@ mod tests {
483
483
// Mark timers to update
484
484
my_tun. mark_timer_to_update ( super :: TimerName :: TimeLastDataPacketSent ) ;
485
485
486
+ // Fetch by swap
486
487
let timer_mask = my_tun
487
488
. timers
488
489
. timers_to_update_mask
489
490
. swap ( 0 , std:: sync:: atomic:: Ordering :: Relaxed ) ;
490
491
492
+ // Timer marked after fetch
491
493
my_tun. mark_timer_to_update ( super :: TimerName :: TimeLastDataPacketReceived ) ;
492
494
493
495
my_tun. tick_marked_timers ( timer_mask) ;
@@ -496,13 +498,37 @@ mod tests {
496
498
assert ! ( !my_tun. timers[ TimerName :: TimeLastDataPacketSent ] . is_zero( ) ) ;
497
499
assert ! ( my_tun. timers[ TimerName :: TimeLastDataPacketReceived ] . is_zero( ) ) ;
498
500
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
+
499
506
// Reset the timers
500
507
my_tun. timers [ TimerName :: TimeLastDataPacketSent ] = SafeDuration :: from_millis ( 0 ) ;
501
508
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 ) ;
503
514
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
505
526
assert ! ( my_tun. timers[ TimerName :: TimeLastDataPacketSent ] . is_zero( ) ) ;
506
527
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( ) ) ;
507
533
}
508
534
}
0 commit comments