| 1 |
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package sctp |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
) |
| 11 |
|
| 12 |
const ( |
| 13 |
noFragment = iota |
| 14 |
fragBegin |
| 15 |
fragMiddle |
| 16 |
fragEnd |
| 17 |
) |
| 18 |
|
| 19 |
func makeDataChunk(tsn uint32, unordered bool, frag int) *chunkPayloadData { |
| 20 |
var begin, end bool |
| 21 |
switch frag { |
| 22 |
case noFragment: |
| 23 |
begin = true |
| 24 |
end = true |
| 25 |
case fragBegin: |
| 26 |
begin = true |
| 27 |
case fragEnd: |
| 28 |
end = true |
| 29 |
} |
| 30 |
|
| 31 |
return &chunkPayloadData{ |
| 32 |
tsn: tsn, |
| 33 |
unordered: unordered, |
| 34 |
beginningFragment: begin, |
| 35 |
endingFragment: end, |
| 36 |
userData: make([]byte, 10), // always 10 bytes |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
func makeStreamDataChunk(tsn uint32, streamID uint16) *chunkPayloadData { |
| 41 |
chunk := makeDataChunk(tsn, false, 0) |
| 42 |
chunk.streamIdentifier = streamID |
| 43 |
|
| 44 |
return chunk |
| 45 |
} |
| 46 |
|
| 47 |
type lifoStreamScheduler struct { |
| 48 |
chunks []StreamSchedulerChunk |
| 49 |
resetCount int |
| 50 |
} |
| 51 |
|
| 52 |
func (s *lifoStreamScheduler) Reset() { |
| 53 |
s.chunks = nil |
| 54 |
s.resetCount++ |
| 55 |
} |
| 56 |
|
| 57 |
func (s *lifoStreamScheduler) Push(chunk StreamSchedulerChunk) { |
| 58 |
s.chunks = append(s.chunks, chunk) |
| 59 |
} |
| 60 |
|
| 61 |
func (s *lifoStreamScheduler) Peek() StreamSchedulerChunk { |
| 62 |
if len(s.chunks) == 0 { |
| 63 |
return nil |
| 64 |
} |
| 65 |
|
| 66 |
return s.chunks[len(s.chunks)-1] |
| 67 |
} |
| 68 |
|
| 69 |
func (s *lifoStreamScheduler) Pop(chunk StreamSchedulerChunk) error { |
| 70 |
if len(s.chunks) == 0 { |
| 71 |
return ErrUnexpectedQState |
| 72 |
} |
| 73 |
last := len(s.chunks) - 1 |
| 74 |
if s.chunks[last] != chunk { |
| 75 |
return ErrUnexpectedChunkPoppedStream |
| 76 |
} |
| 77 |
s.chunks[last] = nil |
| 78 |
s.chunks = s.chunks[:last] |
| 79 |
|
| 80 |
return nil |
| 81 |
} |
| 82 |
|
| 83 |
func TestPendingBaseQueue(t *testing.T) { |
| 84 |
t.Run("push and pop", func(t *testing.T) { |
| 85 |
pq := newPendingBaseQueue() |
| 86 |
pq.push(makeDataChunk(0, false, noFragment)) |
| 87 |
pq.push(makeDataChunk(1, false, noFragment)) |
| 88 |
pq.push(makeDataChunk(2, false, noFragment)) |
| 89 |
|
| 90 |
for i := range uint32(3) { |
| 91 |
c := pq.get(int(i)) |
| 92 |
assert.NotNil(t, c, "should not be nil") |
| 93 |
assert.Equal(t, i, c.tsn, "TSN should match") |
| 94 |
} |
| 95 |
|
| 96 |
for i := range uint32(3) { |
| 97 |
c := pq.pop() |
| 98 |
assert.NotNil(t, c, "should not be nil") |
| 99 |
assert.Equal(t, i, c.tsn, "TSN should match") |
| 100 |
} |
| 101 |
|
| 102 |
pq.push(makeDataChunk(3, false, noFragment)) |
| 103 |
pq.push(makeDataChunk(4, false, noFragment)) |
| 104 |
|
| 105 |
for i := uint32(3); i < 5; i++ { |
| 106 |
c := pq.pop() |
| 107 |
assert.NotNil(t, c, "should not be nil") |
| 108 |
assert.Equal(t, i, c.tsn, "TSN should match") |
| 109 |
} |
| 110 |
}) |
| 111 |
|
| 112 |
t.Run("out of bounds", func(t *testing.T) { |
| 113 |
pq := newPendingBaseQueue() |
| 114 |
assert.Nil(t, pq.pop(), "should be nil") |
| 115 |
assert.Nil(t, pq.get(0), "should be nil") |
| 116 |
|
| 117 |
pq.push(makeDataChunk(0, false, noFragment)) |
| 118 |
assert.Nil(t, pq.get(-1), "should be nil") |
| 119 |
assert.Nil(t, pq.get(1), "should be nil") |
| 120 |
}) |
| 121 |
} |
| 122 |
|
| 123 |
func TestPendingQueue(t *testing.T) { |
| 124 |
// NOTE: TSN is not used in pendingQueue in the actual usage. |
| 125 |
// Following tests use TSN field as a chunk ID. |
| 126 |
|
| 127 |
t.Run("push and pop", func(t *testing.T) { |
| 128 |
pq := newPendingQueue(nil) |
| 129 |
pq.push(makeDataChunk(0, false, noFragment)) |
| 130 |
assert.Equal(t, 10, pq.getNumBytes(), "total bytes mismatch") |
| 131 |
pq.push(makeDataChunk(1, false, noFragment)) |
| 132 |
assert.Equal(t, 20, pq.getNumBytes(), "total bytes mismatch") |
| 133 |
pq.push(makeDataChunk(2, false, noFragment)) |
| 134 |
assert.Equal(t, 30, pq.getNumBytes(), "total bytes mismatch") |
| 135 |
|
| 136 |
for i := range uint32(3) { |
| 137 |
c := pq.peek() |
| 138 |
err := pq.pop(c) |
| 139 |
assert.Nil(t, err, "should not error") |
| 140 |
assert.Equal(t, i, c.tsn, "TSN should match") |
| 141 |
} |
| 142 |
|
| 143 |
assert.Equal(t, 0, pq.getNumBytes(), "total bytes mismatch") |
| 144 |
|
| 145 |
pq.push(makeDataChunk(3, false, noFragment)) |
| 146 |
assert.Equal(t, 10, pq.getNumBytes(), "total bytes mismatch") |
| 147 |
pq.push(makeDataChunk(4, false, noFragment)) |
| 148 |
assert.Equal(t, 20, pq.getNumBytes(), "total bytes mismatch") |
| 149 |
|
| 150 |
for i := uint32(3); i < 5; i++ { |
| 151 |
c := pq.peek() |
| 152 |
err := pq.pop(c) |
| 153 |
assert.Nil(t, err, "should not error") |
| 154 |
assert.Equal(t, i, c.tsn, "TSN should match") |
| 155 |
} |
| 156 |
|
| 157 |
assert.Equal(t, 0, pq.getNumBytes(), "total bytes mismatch") |
| 158 |
}) |
| 159 |
|
| 160 |
t.Run("unordered wins", func(t *testing.T) { |
| 161 |
pq := newPendingQueue(nil) |
| 162 |
pq.push(makeDataChunk(0, false, noFragment)) |
| 163 |
assert.Equal(t, 10, pq.getNumBytes(), "total bytes mismatch") |
| 164 |
pq.push(makeDataChunk(1, true, noFragment)) |
| 165 |
assert.Equal(t, 20, pq.getNumBytes(), "total bytes mismatch") |
| 166 |
pq.push(makeDataChunk(2, false, noFragment)) |
| 167 |
assert.Equal(t, 30, pq.getNumBytes(), "total bytes mismatch") |
| 168 |
pq.push(makeDataChunk(3, true, noFragment)) |
| 169 |
assert.Equal(t, 40, pq.getNumBytes(), "total bytes mismatch") |
| 170 |
|
| 171 |
chunkPayload := pq.peek() |
| 172 |
err := pq.pop(chunkPayload) |
| 173 |
assert.NoError(t, err, "should not error") |
| 174 |
assert.Equal(t, uint32(1), chunkPayload.tsn, "TSN should match") |
| 175 |
|
| 176 |
chunkPayload = pq.peek() |
| 177 |
err = pq.pop(chunkPayload) |
| 178 |
assert.NoError(t, err, "should not error") |
| 179 |
assert.Equal(t, uint32(3), chunkPayload.tsn, "TSN should match") |
| 180 |
|
| 181 |
chunkPayload = pq.peek() |
| 182 |
err = pq.pop(chunkPayload) |
| 183 |
assert.NoError(t, err, "should not error") |
| 184 |
assert.Equal(t, uint32(0), chunkPayload.tsn, "TSN should match") |
| 185 |
|
| 186 |
chunkPayload = pq.peek() |
| 187 |
err = pq.pop(chunkPayload) |
| 188 |
assert.NoError(t, err, "should not error") |
| 189 |
assert.Equal(t, uint32(2), chunkPayload.tsn, "TSN should match") |
| 190 |
|
| 191 |
assert.Equal(t, 0, pq.getNumBytes(), "total bytes mismatch") |
| 192 |
}) |
| 193 |
|
| 194 |
t.Run("fragments", func(t *testing.T) { |
| 195 |
pq := newPendingQueue(nil) |
| 196 |
pq.push(makeDataChunk(0, false, fragBegin)) |
| 197 |
pq.push(makeDataChunk(1, false, fragMiddle)) |
| 198 |
pq.push(makeDataChunk(2, false, fragEnd)) |
| 199 |
pq.push(makeDataChunk(3, true, fragBegin)) |
| 200 |
pq.push(makeDataChunk(4, true, fragMiddle)) |
| 201 |
pq.push(makeDataChunk(5, true, fragEnd)) |
| 202 |
|
| 203 |
expects := []uint32{3, 4, 5, 0, 1, 2} |
| 204 |
|
| 205 |
for _, exp := range expects { |
| 206 |
c := pq.peek() |
| 207 |
err := pq.pop(c) |
| 208 |
assert.NoError(t, err, "should not error") |
| 209 |
assert.Equal(t, exp, c.tsn, "TSN should match") |
| 210 |
} |
| 211 |
}) |
| 212 |
|
| 213 |
// Once decided ordered or unordered, the decision should persist until |
| 214 |
// it pops a chunk with endingFragment flags set to true. |
| 215 |
t.Run("selection persistence", func(t *testing.T) { |
| 216 |
pq := newPendingQueue(nil) |
| 217 |
pq.push(makeDataChunk(0, false, fragBegin)) |
| 218 |
|
| 219 |
chunkPayload := pq.peek() |
| 220 |
err := pq.pop(chunkPayload) |
| 221 |
assert.NoError(t, err, "should not error") |
| 222 |
assert.Equal(t, uint32(0), chunkPayload.tsn, "TSN should match") |
| 223 |
|
| 224 |
pq.push(makeDataChunk(1, true, noFragment)) |
| 225 |
pq.push(makeDataChunk(2, false, fragMiddle)) |
| 226 |
pq.push(makeDataChunk(3, false, fragEnd)) |
| 227 |
|
| 228 |
expects := []uint32{2, 3, 1} |
| 229 |
|
| 230 |
for _, exp := range expects { |
| 231 |
chunkPayload = pq.peek() |
| 232 |
err = pq.pop(chunkPayload) |
| 233 |
assert.NoError(t, err, "should not error") |
| 234 |
assert.Equal(t, exp, chunkPayload.tsn, "TSN should match") |
| 235 |
} |
| 236 |
}) |
| 237 |
|
| 238 |
t.Run("set interleaving rejects non-empty queue", func(t *testing.T) { |
| 239 |
pq := newPendingQueue(nil) |
| 240 |
pq.push(makeDataChunk(1, false, noFragment)) |
| 241 |
|
| 242 |
err := pq.setInterleaving(true) |
| 243 |
assert.ErrorIs(t, err, ErrPendingQueueModeChangeNonEmpty) |
| 244 |
assert.False(t, pq.interleaving) |
| 245 |
|
| 246 |
chunkPayload := pq.peek() |
| 247 |
assert.NotNil(t, chunkPayload) |
| 248 |
assert.Equal(t, uint32(1), chunkPayload.tsn) |
| 249 |
}) |
| 250 |
|
| 251 |
t.Run("set interleaving swaps policy when empty", func(t *testing.T) { |
| 252 |
pq := newPendingQueue(func() InterleavingStreamScheduler { |
| 253 |
return newWeightedFairQueueingPendingQueuePolicy(nil) |
| 254 |
}) |
| 255 |
|
| 256 |
err := pq.setInterleaving(true) |
| 257 |
assert.NoError(t, err) |
| 258 |
assert.True(t, pq.interleaving) |
| 259 |
|
| 260 |
policy, ok := pq.policy.(*interleavingStreamSchedulerPolicy) |
| 261 |
assert.True(t, ok) |
| 262 |
_, ok = policy.scheduler.(*weightedFairQueueingPendingQueuePolicy) |
| 263 |
assert.True(t, ok) |
| 264 |
|
| 265 |
err = pq.setInterleaving(true) |
| 266 |
assert.NoError(t, err) |
| 267 |
|
| 268 |
err = pq.setInterleaving(false) |
| 269 |
assert.NoError(t, err) |
| 270 |
assert.False(t, pq.interleaving) |
| 271 |
|
| 272 |
_, ok = pq.policy.(*messagePendingQueuePolicy) |
| 273 |
assert.True(t, ok) |
| 274 |
}) |
| 275 |
|
| 276 |
t.Run("set interleaving uses configured round robin scheduler", func(t *testing.T) { |
| 277 |
pq := newPendingQueue(func() InterleavingStreamScheduler { |
| 278 |
return newRoundRobinPendingQueuePolicy() |
| 279 |
}) |
| 280 |
|
| 281 |
err := pq.setInterleaving(true) |
| 282 |
assert.NoError(t, err) |
| 283 |
|
| 284 |
policy, ok := pq.policy.(*interleavingStreamSchedulerPolicy) |
| 285 |
assert.True(t, ok) |
| 286 |
_, ok = policy.scheduler.(*roundRobinPendingQueuePolicy) |
| 287 |
assert.True(t, ok) |
| 288 |
}) |
| 289 |
|
| 290 |
t.Run("set interleaving uses custom scheduler", func(t *testing.T) { |
| 291 |
scheduler := &lifoStreamScheduler{} |
| 292 |
pq := newPendingQueue(func() InterleavingStreamScheduler { |
| 293 |
return scheduler |
| 294 |
}) |
| 295 |
|
| 296 |
err := pq.setInterleaving(true) |
| 297 |
assert.NoError(t, err) |
| 298 |
assert.Equal(t, 1, scheduler.resetCount) |
| 299 |
|
| 300 |
pq.push(makeStreamDataChunk(1, 1)) |
| 301 |
pq.push(makeStreamDataChunk(2, 2)) |
| 302 |
|
| 303 |
chunkPayload := pq.peek() |
| 304 |
assert.NotNil(t, chunkPayload) |
| 305 |
assert.Equal(t, uint32(2), chunkPayload.tsn) |
| 306 |
assert.NoError(t, pq.pop(chunkPayload)) |
| 307 |
|
| 308 |
chunkPayload = pq.peek() |
| 309 |
assert.NotNil(t, chunkPayload) |
| 310 |
assert.Equal(t, uint32(1), chunkPayload.tsn) |
| 311 |
assert.NoError(t, pq.pop(chunkPayload)) |
| 312 |
|
| 313 |
err = pq.setInterleaving(false) |
| 314 |
assert.NoError(t, err) |
| 315 |
assert.Equal(t, 2, scheduler.resetCount) |
| 316 |
}) |
| 317 |
} |
| 318 |
|
| 319 |
func TestRoundRobinPendingQueuePolicy(t *testing.T) { |
| 320 |
t.Run("round robin across streams", func(t *testing.T) { |
| 321 |
pq := newRoundRobinPendingQueuePolicy() |
| 322 |
pq.Push(makeStreamDataChunk(1, 1)) |
| 323 |
pq.Push(makeStreamDataChunk(2, 2)) |
| 324 |
pq.Push(makeStreamDataChunk(3, 1)) |
| 325 |
|
| 326 |
expects := []uint32{1, 2, 3} |
| 327 |
for _, exp := range expects { |
| 328 |
chunkPayload := pq.Peek() |
| 329 |
assert.NotNil(t, chunkPayload) |
| 330 |
assert.Equal(t, exp, chunkPayload.chunkPayloadData().tsn) |
| 331 |
|
| 332 |
err := pq.Pop(chunkPayload) |
| 333 |
assert.NoError(t, err) |
| 334 |
} |
| 335 |
|
| 336 |
assert.Nil(t, pq.Peek()) |
| 337 |
}) |
| 338 |
|
| 339 |
t.Run("peek keeps the selected stream until pop", func(t *testing.T) { |
| 340 |
pq := newRoundRobinPendingQueuePolicy() |
| 341 |
pq.Push(makeStreamDataChunk(10, 1)) |
| 342 |
pq.Push(makeStreamDataChunk(20, 2)) |
| 343 |
|
| 344 |
first := pq.Peek() |
| 345 |
second := pq.Peek() |
| 346 |
|
| 347 |
assert.Same(t, first, second) |
| 348 |
assert.Equal(t, uint16(1), pq.selectedStream) |
| 349 |
|
| 350 |
err := pq.Pop(first) |
| 351 |
assert.NoError(t, err) |
| 352 |
|
| 353 |
next := pq.Peek() |
| 354 |
assert.NotNil(t, next) |
| 355 |
assert.Equal(t, uint32(20), next.chunkPayloadData().tsn) |
| 356 |
}) |
| 357 |
|
| 358 |
t.Run("pop errors", func(t *testing.T) { |
| 359 |
t.Run("requires selection", func(t *testing.T) { |
| 360 |
pq := newRoundRobinPendingQueuePolicy() |
| 361 |
err := pq.Pop(makeStreamDataChunk(30, 1)) |
| 362 |
assert.ErrorIs(t, err, ErrUnexpectedQState) |
| 363 |
}) |
| 364 |
|
| 365 |
t.Run("validates popped chunk", func(t *testing.T) { |
| 366 |
pq := newRoundRobinPendingQueuePolicy() |
| 367 |
first := makeStreamDataChunk(40, 1) |
| 368 |
second := makeStreamDataChunk(41, 1) |
| 369 |
pq.Push(first) |
| 370 |
pq.Push(second) |
| 371 |
|
| 372 |
assert.Same(t, first, pq.Peek()) |
| 373 |
|
| 374 |
err := pq.Pop(second) |
| 375 |
assert.ErrorIs(t, err, ErrUnexpectedChunkPoppedStream) |
| 376 |
}) |
| 377 |
}) |
| 378 |
} |
| 379 |
|
| 380 |
func TestWeightedFairQueueingPendingQueuePolicy(t *testing.T) { |
| 381 |
t.Run("selects lowest virtual finish time", func(t *testing.T) { |
| 382 |
pq := newWeightedFairQueueingPendingQueuePolicy(nil) |
| 383 |
large1 := makeStreamDataChunk(1, 1) |
| 384 |
large1.userData = make([]byte, 100) |
| 385 |
large2 := makeStreamDataChunk(2, 1) |
| 386 |
large2.userData = make([]byte, 100) |
| 387 |
small := makeStreamDataChunk(3, 2) |
| 388 |
small.userData = make([]byte, 10) |
| 389 |
|
| 390 |
pq.Push(large1) |
| 391 |
pq.Push(large2) |
| 392 |
pq.Push(small) |
| 393 |
|
| 394 |
expects := []uint32{3, 1, 2} |
| 395 |
for _, exp := range expects { |
| 396 |
chunkPayload := pq.Peek() |
| 397 |
assert.NotNil(t, chunkPayload) |
| 398 |
assert.Equal(t, exp, chunkPayload.chunkPayloadData().tsn) |
| 399 |
|
| 400 |
err := pq.Pop(chunkPayload) |
| 401 |
assert.NoError(t, err) |
| 402 |
} |
| 403 |
|
| 404 |
assert.Nil(t, pq.Peek()) |
| 405 |
}) |
| 406 |
|
| 407 |
t.Run("applies stream weights", func(t *testing.T) { |
| 408 |
pq := newWeightedFairQueueingPendingQueuePolicy(map[uint16]uint16{1: 2}) |
| 409 |
weighted := makeStreamDataChunk(1, 1) |
| 410 |
weighted.userData = make([]byte, 100) |
| 411 |
unweighted := makeStreamDataChunk(2, 2) |
| 412 |
unweighted.userData = make([]byte, 60) |
| 413 |
|
| 414 |
pq.Push(weighted) |
| 415 |
pq.Push(unweighted) |
| 416 |
|
| 417 |
chunkPayload := pq.Peek() |
| 418 |
assert.NotNil(t, chunkPayload) |
| 419 |
assert.Equal(t, uint32(1), chunkPayload.chunkPayloadData().tsn) |
| 420 |
assert.NoError(t, pq.Pop(chunkPayload)) |
| 421 |
|
| 422 |
chunkPayload = pq.Peek() |
| 423 |
assert.NotNil(t, chunkPayload) |
| 424 |
assert.Equal(t, uint32(2), chunkPayload.chunkPayloadData().tsn) |
| 425 |
assert.NoError(t, pq.Pop(chunkPayload)) |
| 426 |
}) |
| 427 |
|
| 428 |
t.Run("pop errors", func(t *testing.T) { |
| 429 |
t.Run("requires selection", func(t *testing.T) { |
| 430 |
pq := newWeightedFairQueueingPendingQueuePolicy(nil) |
| 431 |
err := pq.Pop(makeStreamDataChunk(30, 1)) |
| 432 |
assert.ErrorIs(t, err, ErrUnexpectedQState) |
| 433 |
}) |
| 434 |
|
| 435 |
t.Run("validates popped chunk", func(t *testing.T) { |
| 436 |
pq := newWeightedFairQueueingPendingQueuePolicy(nil) |
| 437 |
first := makeStreamDataChunk(40, 1) |
| 438 |
second := makeStreamDataChunk(41, 1) |
| 439 |
pq.Push(first) |
| 440 |
pq.Push(second) |
| 441 |
|
| 442 |
assert.Same(t, first, pq.Peek()) |
| 443 |
|
| 444 |
err := pq.Pop(second) |
| 445 |
assert.ErrorIs(t, err, ErrUnexpectedChunkPoppedStream) |
| 446 |
}) |
| 447 |
}) |
| 448 |
} |
| 449 |
|
| 450 |
func TestPendingQueue_PopErrors(t *testing.T) { |
| 451 |
t.Run("ErrUnexpectedQState when not selected and not beginningFragment", func(t *testing.T) { |
| 452 |
pq := newPendingQueue(nil) |
| 453 |
|
| 454 |
mid := makeDataChunk(100, false, fragMiddle) |
| 455 |
err := pq.pop(mid) |
| 456 |
assert.ErrorIs(t, err, ErrUnexpectedQState) |
| 457 |
}) |
| 458 |
|
| 459 |
t.Run("ErrUnexpectedChunkPoppedUnordered (not selected path)", func(t *testing.T) { |
| 460 |
pq := newPendingQueue(nil) |
| 461 |
|
| 462 |
u1 := makeDataChunk(1, true, noFragment) |
| 463 |
u2 := makeDataChunk(2, true, noFragment) |
| 464 |
pq.push(u1) |
| 465 |
pq.push(u2) |
| 466 |
|
| 467 |
err := pq.pop(u2) |
| 468 |
assert.ErrorIs(t, err, ErrUnexpectedChunkPoppedUnordered) |
| 469 |
}) |
| 470 |
|
| 471 |
t.Run("ErrUnexpectedChunkPoppedOrdered (not selected path)", func(t *testing.T) { |
| 472 |
pq := newPendingQueue(nil) |
| 473 |
|
| 474 |
o1 := makeDataChunk(10, false, noFragment) |
| 475 |
o2 := makeDataChunk(11, false, noFragment) |
| 476 |
pq.push(o1) |
| 477 |
pq.push(o2) |
| 478 |
|
| 479 |
err := pq.pop(o2) |
| 480 |
assert.ErrorIs(t, err, ErrUnexpectedChunkPoppedOrdered) |
| 481 |
}) |
| 482 |
|
| 483 |
t.Run("ErrUnexpectedChunkPoppedUnordered (selected unordered path)", func(t *testing.T) { |
| 484 |
pq := newPendingQueue(nil) |
| 485 |
|
| 486 |
uBegin := makeDataChunk(21, true, fragBegin) |
| 487 |
uMid := makeDataChunk(22, true, fragMiddle) |
| 488 |
uEnd := makeDataChunk(23, true, fragEnd) |
| 489 |
pq.push(uBegin) |
| 490 |
pq.push(uMid) |
| 491 |
pq.push(uEnd) |
| 492 |
|
| 493 |
err := pq.pop(uBegin) |
| 494 |
assert.NoError(t, err) |
| 495 |
|
| 496 |
err = pq.pop(uEnd) |
| 497 |
assert.ErrorIs(t, err, ErrUnexpectedChunkPoppedUnordered) |
| 498 |
}) |
| 499 |
|
| 500 |
t.Run("ErrUnexpectedChunkPoppedOrdered (selected ordered path)", func(t *testing.T) { |
| 501 |
pq := newPendingQueue(nil) |
| 502 |
|
| 503 |
oBegin := makeDataChunk(31, false, fragBegin) |
| 504 |
oMid := makeDataChunk(32, false, fragMiddle) |
| 505 |
oEnd := makeDataChunk(33, false, fragEnd) |
| 506 |
pq.push(oBegin) |
| 507 |
pq.push(oMid) |
| 508 |
pq.push(oEnd) |
| 509 |
|
| 510 |
err := pq.pop(oBegin) |
| 511 |
assert.NoError(t, err) |
| 512 |
|
| 513 |
err = pq.pop(oEnd) |
| 514 |
assert.ErrorIs(t, err, ErrUnexpectedChunkPoppedOrdered) |
| 515 |
}) |
| 516 |
|
| 517 |
t.Run("nBytes guard clamps to zero when underflows", func(t *testing.T) { |
| 518 |
pq := newPendingQueue(nil) |
| 519 |
|
| 520 |
c := makeDataChunk(40, false, noFragment) |
| 521 |
pq.push(c) |
| 522 |
|
| 523 |
pq.nBytes = 5 |
| 524 |
|
| 525 |
peek := pq.peek() |
| 526 |
err := pq.pop(peek) |
| 527 |
assert.NoError(t, err) |
| 528 |
|
| 529 |
assert.Equal(t, 0, pq.getNumBytes()) |
| 530 |
}) |
| 531 |
} |