@@ -22,50 +22,48 @@ func (o *testAckTimerObserver) onAckTimeout() {
2222}
2323
2424func TestAckTimer (t * testing.T ) {
25- t .Run ("start and close" , func (t * testing.T ) {
25+ t .Run ("start and close (second packet rule) " , func (t * testing.T ) {
2626 var nCbs uint32
2727 rt := newAckTimer (& testAckTimerObserver {
2828 onAckTO : func () {
29- t .Log ("ack timed out" )
3029 atomic .AddUint32 (& nCbs , 1 )
3130 },
3231 })
3332
3433 for i := 0 ; i < 2 ; i ++ {
35- // should start ok
34+ // First eligible packet -> arm delayed SACK timer
3635 ok := rt .start ()
37- assert .True (t , ok , "start() should succeed" )
38- assert .True (t , rt .isRunning (), "should be running" )
36+ assert .True (t , ok , "first start() should succeed" )
37+ assert .True (t , rt .isRunning (), "timer should be running after first start " )
3938
40- // subsequent start is a noop
39+ // Second eligible packet -> immediate SACK, timer stops (RFC 9260 section 6.2.1)
4140 ok = rt .start ()
42- assert .False (t , ok , "start() should NOT succeed once closed" )
43- assert .True (t , rt .isRunning (), "should be running" )
41+ assert .True (t , ok , "second start() should be accepted and trigger immediate ACK" )
4442
45- // Sleep more than 2 * 200msec interval to test if it times out only once
46- time .Sleep (ackInterval * 2 + 50 * time .Millisecond )
43+ // Give the async callback time to fire.
44+ time .Sleep (20 * time .Millisecond )
4745
4846 assert .Equalf (t , uint32 (1 ), atomic .LoadUint32 (& nCbs ),
49- "should be called once (actual: %d)" , atomic .LoadUint32 (& nCbs ))
47+ "immediate ACK should have fired exactly once (iter %d)" , i )
48+ assert .False (t , rt .isRunning (), "timer should have stopped after immediate ACK" )
5049
50+ // Reset for next iteration
5151 atomic .StoreUint32 (& nCbs , 0 )
5252 }
5353
54- // should close ok
54+ // Close disables future starts
5555 rt .close ()
56- assert .False (t , rt .isRunning (), "should not be running" )
56+ assert .False (t , rt .isRunning (), "timer should not be running after close " )
5757
58- // once closed, it cannot start
5958 ok := rt .start ()
6059 assert .False (t , ok , "start() should NOT succeed once closed" )
61- assert .False (t , rt .isRunning (), "should not be running" )
60+ assert .False (t , rt .isRunning (), "still not running after refused start on closed timer " )
6261 })
6362
64- t .Run ("start and stop" , func (t * testing.T ) {
63+ t .Run ("start and stop (no timeout) " , func (t * testing.T ) {
6564 var nCbs uint32
6665 rt := newAckTimer (& testAckTimerObserver {
6766 onAckTO : func () {
68- t .Log ("ack timed out" )
6967 atomic .AddUint32 (& nCbs , 1 )
7068 },
7169 })
@@ -74,26 +72,25 @@ func TestAckTimer(t *testing.T) {
7472 // should start ok
7573 ok := rt .start ()
7674 assert .True (t , ok , "start() should succeed" )
77- assert .True (t , rt .isRunning (), "should be running" )
75+ assert .True (t , rt .isRunning (), "timer should be running" )
7876
7977 // stop immedidately
8078 rt .stop ()
81- assert .False (t , rt .isRunning (), "should not be running" )
79+ assert .False (t , rt .isRunning (), "timer should not be running after stop() " )
8280 }
8381
84- // Sleep more than 200msec of interval to test if it never times out
82+ // Wait > interval to ensure no stray timeout happened.
8583 time .Sleep (ackInterval + 50 * time .Millisecond )
86-
8784 assert .Equalf (t , uint32 (0 ), atomic .LoadUint32 (& nCbs ),
88- "should not be timed out (actual: %d)" , atomic .LoadUint32 (& nCbs ))
85+ "no ACK should have fired after stop() (got %d)" , atomic .LoadUint32 (& nCbs ))
8986
9087 // can start again
9188 ok := rt .start ()
9289 assert .True (t , ok , "start() should succeed again" )
93- assert .True (t , rt .isRunning (), "should be running" )
90+ assert .True (t , rt .isRunning (), "timer should be running" )
9491
95- // should close ok
92+ // Close disables and stops
9693 rt .close ()
97- assert .False (t , rt .isRunning (), "should not be running" )
94+ assert .False (t , rt .isRunning (), "timer should not be running after close " )
9895 })
9996}
0 commit comments