| 1 |
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package sctp |
| 5 |
|
| 6 |
import ( |
| 7 |
"math" |
| 8 |
"sync/atomic" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
) |
| 14 |
|
| 15 |
func TestRTOManager(t *testing.T) { |
| 16 |
t.Run("initial values", func(t *testing.T) { |
| 17 |
m := newRTOManager(0) |
| 18 |
assert.Equal(t, rtoInitial, m.rto, "should be rtoInitial") |
| 19 |
assert.Equal(t, rtoInitial, m.getRTO(), "should be rtoInitial") |
| 20 |
assert.Equal(t, float64(0), m.srtt, "should be 0") |
| 21 |
assert.Equal(t, float64(0), m.rttvar, "should be 0") |
| 22 |
}) |
| 23 |
|
| 24 |
t.Run("RTO calculation (small RTT)", func(t *testing.T) { |
| 25 |
var rto float64 |
| 26 |
m := newRTOManager(0) |
| 27 |
exp := []int32{ |
| 28 |
1800, |
| 29 |
1500, |
| 30 |
1275, |
| 31 |
1106, |
| 32 |
1000, // capped at RTO.Min |
| 33 |
} |
| 34 |
|
| 35 |
for i := range 5 { |
| 36 |
m.setNewRTT(600) |
| 37 |
rto = m.getRTO() |
| 38 |
assert.Equal(t, exp[i], int32(math.Floor(rto)), "should be equal") |
| 39 |
} |
| 40 |
}) |
| 41 |
|
| 42 |
t.Run("RTO calculation (large RTT)", func(t *testing.T) { |
| 43 |
var rto float64 |
| 44 |
m := newRTOManager(0) |
| 45 |
exp := []int32{ |
| 46 |
60000, // capped at RTO.Max |
| 47 |
60000, // capped at RTO.Max |
| 48 |
60000, // capped at RTO.Max |
| 49 |
55312, |
| 50 |
48984, |
| 51 |
} |
| 52 |
|
| 53 |
for i := range 5 { |
| 54 |
m.setNewRTT(30000) |
| 55 |
rto = m.getRTO() |
| 56 |
assert.Equal(t, exp[i], int32(math.Floor(rto)), "should be equal") |
| 57 |
} |
| 58 |
}) |
| 59 |
|
| 60 |
t.Run("calculateNextTimeout", func(t *testing.T) { |
| 61 |
var rto float64 |
| 62 |
rto = calculateNextTimeout(1.0, 0, defaultRTOMax) |
| 63 |
assert.Equal(t, float64(1), rto, "should match") |
| 64 |
rto = calculateNextTimeout(1.0, 1, defaultRTOMax) |
| 65 |
assert.Equal(t, float64(2), rto, "should match") |
| 66 |
rto = calculateNextTimeout(1.0, 2, defaultRTOMax) |
| 67 |
assert.Equal(t, float64(4), rto, "should match") |
| 68 |
rto = calculateNextTimeout(1.0, 30, defaultRTOMax) |
| 69 |
assert.Equal(t, float64(60000), rto, "should match") |
| 70 |
rto = calculateNextTimeout(1.0, 63, defaultRTOMax) |
| 71 |
assert.Equal(t, float64(60000), rto, "should match") |
| 72 |
rto = calculateNextTimeout(1.0, 64, defaultRTOMax) |
| 73 |
assert.Equal(t, float64(60000), rto, "should match") |
| 74 |
}) |
| 75 |
t.Run("calculateNextTimeout w/ RTOMax", func(t *testing.T) { |
| 76 |
var rto float64 |
| 77 |
rto = calculateNextTimeout(1.0, 0, 2.0) |
| 78 |
assert.Equal(t, 1.0, rto, "should match") |
| 79 |
rto = calculateNextTimeout(1.5, 1, 2.0) |
| 80 |
assert.Equal(t, 2.0, rto, "should match") |
| 81 |
rto = calculateNextTimeout(1.0, 10, 2.0) |
| 82 |
assert.Equal(t, 2.0, rto, "should match") |
| 83 |
rto = calculateNextTimeout(1.0, 31, 1000.0) |
| 84 |
assert.Equal(t, 1000.0, rto, "should match") |
| 85 |
}) |
| 86 |
|
| 87 |
t.Run("reset", func(t *testing.T) { |
| 88 |
m := newRTOManager(0) |
| 89 |
for range 10 { |
| 90 |
m.setNewRTT(200) |
| 91 |
} |
| 92 |
|
| 93 |
m.reset() |
| 94 |
assert.Equal(t, rtoInitial, m.getRTO(), "should be rtoInitial") |
| 95 |
assert.Equal(t, float64(0), m.srtt, "should be 0") |
| 96 |
assert.Equal(t, float64(0), m.rttvar, "should be 0") |
| 97 |
}) |
| 98 |
} |
| 99 |
|
| 100 |
type ( |
| 101 |
onRTO func(id int, n uint) |
| 102 |
onRtxFailure func(id int) |
| 103 |
) |
| 104 |
|
| 105 |
type testTimerObserver struct { |
| 106 |
onRTO onRTO |
| 107 |
onRtxFailure onRtxFailure |
| 108 |
} |
| 109 |
|
| 110 |
func (o *testTimerObserver) onRetransmissionTimeout(id int, n uint) { |
| 111 |
o.onRTO(id, n) |
| 112 |
} |
| 113 |
|
| 114 |
func (o *testTimerObserver) onRetransmissionFailure(id int) { |
| 115 |
o.onRtxFailure(id) |
| 116 |
} |
| 117 |
|
| 118 |
func TestRtxTimer(t *testing.T) { //nolint:maintidx |
| 119 |
t.Run("callback interval", func(t *testing.T) { |
| 120 |
timerID := 0 |
| 121 |
var nCbs int32 |
| 122 |
rt := newRTXTimer(timerID, &testTimerObserver{ |
| 123 |
onRTO: func(id int, _ uint) { |
| 124 |
atomic.AddInt32(&nCbs, 1) |
| 125 |
// 30 : 1 (30) |
| 126 |
// 60 : 2 (90) |
| 127 |
// 120: 3 (210) |
| 128 |
// 240: 4 (550) <== expected in 650 msec |
| 129 |
assert.Equalf(t, timerID, id, "unexpted timer ID: %d", id) |
| 130 |
}, |
| 131 |
onRtxFailure: func(_ int) {}, |
| 132 |
}, pathMaxRetrans, 0) |
| 133 |
|
| 134 |
assert.False(t, rt.isRunning(), "should not be running") |
| 135 |
|
| 136 |
// since := time.Now() |
| 137 |
ok := rt.start(30) |
| 138 |
assert.True(t, ok, "should be true") |
| 139 |
assert.True(t, rt.isRunning(), "should be running") |
| 140 |
|
| 141 |
time.Sleep(650 * time.Millisecond) |
| 142 |
rt.stop() |
| 143 |
assert.False(t, rt.isRunning(), "should not be running") |
| 144 |
|
| 145 |
assert.Equal(t, int32(4), atomic.LoadInt32(&nCbs), "should be called 4 times") |
| 146 |
}) |
| 147 |
|
| 148 |
t.Run("last start wins", func(t *testing.T) { |
| 149 |
timerID := 3 |
| 150 |
var nCbs int32 |
| 151 |
|
| 152 |
rt := newRTXTimer(timerID, &testTimerObserver{ |
| 153 |
onRTO: func(id int, _ uint) { |
| 154 |
atomic.AddInt32(&nCbs, 1) |
| 155 |
assert.Equalf(t, timerID, id, "unexpted timer ID: %d", id) |
| 156 |
}, |
| 157 |
onRtxFailure: func(_ int) {}, |
| 158 |
}, pathMaxRetrans, 0) |
| 159 |
|
| 160 |
interval := float64(30.0) |
| 161 |
ok := rt.start(interval) |
| 162 |
assert.True(t, ok, "should be accepted") |
| 163 |
ok = rt.start(interval * 99) // should ignored |
| 164 |
assert.False(t, ok, "should be ignored") |
| 165 |
ok = rt.start(interval * 99) // should ignored |
| 166 |
assert.False(t, ok, "should be ignored") |
| 167 |
|
| 168 |
time.Sleep(time.Duration(interval*1.5) * time.Millisecond) |
| 169 |
rt.stop() |
| 170 |
|
| 171 |
assert.False(t, rt.isRunning(), "should not be running") |
| 172 |
assert.Equal(t, int32(1), atomic.LoadInt32(&nCbs), "must be called once") |
| 173 |
}) |
| 174 |
|
| 175 |
t.Run("stop right afeter start", func(t *testing.T) { |
| 176 |
timerID := 3 |
| 177 |
var nCbs int32 |
| 178 |
|
| 179 |
rt := newRTXTimer(timerID, &testTimerObserver{ |
| 180 |
onRTO: func(id int, _ uint) { |
| 181 |
atomic.AddInt32(&nCbs, 1) |
| 182 |
assert.Equalf(t, timerID, id, "unexpted timer ID: %d", id) |
| 183 |
}, |
| 184 |
onRtxFailure: func(_ int) {}, |
| 185 |
}, pathMaxRetrans, 0) |
| 186 |
|
| 187 |
interval := float64(30.0) |
| 188 |
ok := rt.start(interval) |
| 189 |
assert.True(t, ok, "should be accepted") |
| 190 |
rt.stop() |
| 191 |
|
| 192 |
time.Sleep(time.Duration(interval*1.5) * time.Millisecond) |
| 193 |
rt.stop() |
| 194 |
|
| 195 |
assert.False(t, rt.isRunning(), "should not be running") |
| 196 |
assert.Equal(t, int32(0), atomic.LoadInt32(&nCbs), "no callback should be made") |
| 197 |
}) |
| 198 |
|
| 199 |
t.Run("start, stop then start", func(t *testing.T) { |
| 200 |
timerID := 1 |
| 201 |
var nCbs int32 |
| 202 |
rt := newRTXTimer(timerID, &testTimerObserver{ |
| 203 |
onRTO: func(id int, _ uint) { |
| 204 |
atomic.AddInt32(&nCbs, 1) |
| 205 |
assert.Equalf(t, timerID, id, "unexpted timer ID: %d", id) |
| 206 |
}, |
| 207 |
onRtxFailure: func(_ int) {}, |
| 208 |
}, pathMaxRetrans, 0) |
| 209 |
|
| 210 |
interval := float64(30.0) |
| 211 |
ok := rt.start(interval) |
| 212 |
assert.True(t, ok, "should be accepted") |
| 213 |
rt.stop() |
| 214 |
assert.False(t, rt.isRunning(), "should NOT be running") |
| 215 |
ok = rt.start(interval) |
| 216 |
assert.True(t, ok, "should be accepted") |
| 217 |
assert.True(t, rt.isRunning(), "should be running") |
| 218 |
|
| 219 |
time.Sleep(time.Duration(interval*1.5) * time.Millisecond) |
| 220 |
rt.stop() |
| 221 |
assert.False(t, rt.isRunning(), "should NOT be running") |
| 222 |
assert.Equal(t, int32(1), atomic.LoadInt32(&nCbs), "must be called once") |
| 223 |
}) |
| 224 |
|
| 225 |
t.Run("start and stop in a tight loop", func(t *testing.T) { |
| 226 |
timerID := 2 |
| 227 |
var nCbs int32 |
| 228 |
rt := newRTXTimer(timerID, &testTimerObserver{ |
| 229 |
onRTO: func(id int, _ uint) { |
| 230 |
atomic.AddInt32(&nCbs, 1) |
| 231 |
t.Log("onRTO() called") |
| 232 |
assert.Equalf(t, timerID, id, "unexpted timer ID: %d", id) |
| 233 |
}, |
| 234 |
onRtxFailure: func(_ int) {}, |
| 235 |
}, pathMaxRetrans, 0) |
| 236 |
|
| 237 |
for range 1000 { |
| 238 |
ok := rt.start(30) |
| 239 |
assert.True(t, ok, "should be accepted") |
| 240 |
assert.True(t, rt.isRunning(), "should be running") |
| 241 |
rt.stop() |
| 242 |
assert.False(t, rt.isRunning(), "should NOT be running") |
| 243 |
} |
| 244 |
|
| 245 |
assert.Equal(t, int32(0), atomic.LoadInt32(&nCbs), "no callback should be made") |
| 246 |
}) |
| 247 |
|
| 248 |
t.Run("timer should stop after rtx failure", func(t *testing.T) { |
| 249 |
timerID := 4 |
| 250 |
var nCbs int32 |
| 251 |
doneCh := make(chan bool) |
| 252 |
|
| 253 |
since := time.Now() |
| 254 |
var elapsed float64 // in seconds |
| 255 |
rt := newRTXTimer(timerID, &testTimerObserver{ |
| 256 |
onRTO: func(id int, nRtos uint) { |
| 257 |
assert.Equal(t, timerID, id, "unexpted timer ID: %d", id) |
| 258 |
t.Logf("onRTO: n=%d elapsed=%.03f\n", nRtos, time.Since(since).Seconds()) |
| 259 |
atomic.AddInt32(&nCbs, 1) |
| 260 |
}, |
| 261 |
onRtxFailure: func(id int) { |
| 262 |
assert.Equal(t, timerID, id, "unexpted timer ID: %d", id) |
| 263 |
elapsed = time.Since(since).Seconds() |
| 264 |
t.Logf("onRtxFailure: elapsed=%.03f\n", elapsed) |
| 265 |
doneCh <- true |
| 266 |
}, |
| 267 |
}, pathMaxRetrans, 0) |
| 268 |
|
| 269 |
// RTO(msec) Total(msec) |
| 270 |
// 10 10 1st RTO |
| 271 |
// 20 30 2nd RTO |
| 272 |
// 40 70 3rd RTO |
| 273 |
// 80 150 4th RTO |
| 274 |
// 160 310 5th RTO (== Path.Max.Retrans) |
| 275 |
// 320 630 Failure |
| 276 |
|
| 277 |
interval := float64(10.0) |
| 278 |
ok := rt.start(interval) |
| 279 |
assert.True(t, ok, "should be accepted") |
| 280 |
assert.True(t, rt.isRunning(), "should be running") |
| 281 |
|
| 282 |
<-doneCh |
| 283 |
|
| 284 |
assert.False(t, rt.isRunning(), "should not be running") |
| 285 |
assert.Equal(t, int32(5), atomic.LoadInt32(&nCbs), "should be called 5 times") |
| 286 |
assert.True(t, elapsed > 0.600, "must have taken more than 600 msec") |
| 287 |
assert.True(t, elapsed < 0.700, "must fail in less than 700 msec") |
| 288 |
}) |
| 289 |
|
| 290 |
t.Run("timer should not stop if maxRetrans is 0", func(t *testing.T) { |
| 291 |
timerID := 4 |
| 292 |
maxRtos := uint(6) |
| 293 |
var nCbs int32 |
| 294 |
doneCh := make(chan bool) |
| 295 |
|
| 296 |
since := time.Now() |
| 297 |
var elapsed float64 // in seconds |
| 298 |
rt := newRTXTimer(timerID, &testTimerObserver{ |
| 299 |
onRTO: func(id int, nRtos uint) { |
| 300 |
assert.Equal(t, timerID, id, "unexpted timer ID: %d", id) |
| 301 |
elapsed = time.Since(since).Seconds() |
| 302 |
t.Logf("onRTO: n=%d elapsed=%.03f\n", nRtos, elapsed) |
| 303 |
atomic.AddInt32(&nCbs, 1) |
| 304 |
if nRtos == maxRtos { |
| 305 |
doneCh <- true |
| 306 |
} |
| 307 |
}, |
| 308 |
onRtxFailure: func(_ int) { |
| 309 |
assert.Fail(t, "timer should not fail") |
| 310 |
}, |
| 311 |
}, 0, 0) |
| 312 |
|
| 313 |
// RTO(msec) Total(msec) |
| 314 |
// 10 10 1st RTO |
| 315 |
// 20 30 2nd RTO |
| 316 |
// 40 70 3rd RTO |
| 317 |
// 80 150 4th RTO |
| 318 |
// 160 310 5th RTO |
| 319 |
// 320 630 6th RTO => exit test (timer should still be running) |
| 320 |
|
| 321 |
interval := float64(10.0) |
| 322 |
ok := rt.start(interval) |
| 323 |
assert.True(t, ok, "should be accepted") |
| 324 |
assert.True(t, rt.isRunning(), "should be running") |
| 325 |
|
| 326 |
<-doneCh |
| 327 |
|
| 328 |
assert.True(t, rt.isRunning(), "should still be running") |
| 329 |
assert.Equal(t, int32(6), atomic.LoadInt32(&nCbs), "should be called 6 times") |
| 330 |
assert.True(t, elapsed > 0.600, "must have taken more than 600 msec") |
| 331 |
assert.True(t, elapsed < 0.700, "must fail in less than 700 msec") |
| 332 |
|
| 333 |
rt.stop() |
| 334 |
}) |
| 335 |
|
| 336 |
t.Run("stop timer that is not running is noop", func(t *testing.T) { |
| 337 |
timerID := 5 |
| 338 |
doneCh := make(chan bool) |
| 339 |
|
| 340 |
rt := newRTXTimer(timerID, &testTimerObserver{ |
| 341 |
onRTO: func(id int, _ uint) { |
| 342 |
assert.Equal(t, timerID, id, "unexpted timer ID: %d", id) |
| 343 |
doneCh <- true |
| 344 |
}, |
| 345 |
onRtxFailure: func(_ int) {}, |
| 346 |
}, pathMaxRetrans, 0) |
| 347 |
|
| 348 |
for range 10 { |
| 349 |
rt.stop() |
| 350 |
} |
| 351 |
|
| 352 |
ok := rt.start(20) |
| 353 |
assert.True(t, ok, "should be accepted") |
| 354 |
assert.True(t, rt.isRunning(), "must be running") |
| 355 |
|
| 356 |
<-doneCh |
| 357 |
rt.stop() |
| 358 |
assert.False(t, rt.isRunning(), "must be false") |
| 359 |
}) |
| 360 |
|
| 361 |
t.Run("closed timer won't start", func(t *testing.T) { |
| 362 |
var rtoCount int |
| 363 |
timerID := 6 |
| 364 |
rt := newRTXTimer(timerID, &testTimerObserver{ |
| 365 |
onRTO: func(_ int, _ uint) { |
| 366 |
rtoCount++ |
| 367 |
}, |
| 368 |
onRtxFailure: func(_ int) {}, |
| 369 |
}, pathMaxRetrans, 0) |
| 370 |
|
| 371 |
ok := rt.start(20) |
| 372 |
assert.True(t, ok, "should be accepted") |
| 373 |
assert.True(t, rt.isRunning(), "must be running") |
| 374 |
|
| 375 |
rt.close() |
| 376 |
assert.False(t, rt.isRunning(), "must be false") |
| 377 |
|
| 378 |
ok = rt.start(20) |
| 379 |
assert.False(t, ok, "should not start") |
| 380 |
assert.False(t, rt.isRunning(), "must not be running") |
| 381 |
|
| 382 |
time.Sleep(100 * time.Millisecond) |
| 383 |
assert.Equal(t, 0, rtoCount, "RTO should not occur") |
| 384 |
}) |
| 385 |
} |