This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
receive_payload_queue_test.go
169 lines (150 loc) · 5.07 KB
/
receive_payload_queue_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package sctp
import (
"math"
"testing"
"github.com/stretchr/testify/assert"
)
func TestReceivePayloadQueue(t *testing.T) {
maxOffset := uint32(512)
q := newReceivePayloadQueue(maxOffset)
initTSN := uint32(math.MaxUint32 - 10)
q.init(initTSN - 2)
assert.Equal(t, initTSN-2, q.getcumulativeTSN())
assert.Zero(t, q.size())
_, ok := q.getLastTSNReceived()
assert.False(t, ok)
assert.Empty(t, q.getGapAckBlocks())
// force pop empy queue to advance cumulative TSN
assert.False(t, q.pop(true))
assert.Equal(t, initTSN-1, q.getcumulativeTSN())
assert.Zero(t, q.size())
assert.Empty(t, q.getGapAckBlocks())
nextTSN := initTSN + maxOffset - 1
assert.True(t, q.push(nextTSN))
assert.Equal(t, 1, q.size())
lastTSN, ok := q.getLastTSNReceived()
assert.True(t, lastTSN == nextTSN && ok, "lastTSN:%d, ok:%t", lastTSN, ok)
assert.True(t, q.hasChunk(nextTSN))
assert.True(t, q.push(initTSN))
assert.False(t, q.canPush(initTSN-1))
assert.False(t, q.canPush(initTSN+maxOffset))
assert.False(t, q.push(initTSN+maxOffset))
assert.True(t, q.canPush(nextTSN-1))
assert.Equal(t, 2, q.size())
gaps := q.getGapAckBlocks()
assert.EqualValues(t, []gapAckBlock{
{start: uint16(1), end: uint16(1)},
{start: uint16(maxOffset), end: uint16(maxOffset)},
}, gaps)
assert.True(t, q.pop(false))
assert.Equal(t, 1, q.size())
assert.Equal(t, initTSN, q.cumulativeTSN)
assert.False(t, q.pop(false))
assert.Equal(t, initTSN, q.cumulativeTSN)
size := q.size()
// push tsn with two gap
// tsnRange [[start,end]...]
tsnRange := [][]uint32{
{initTSN + 5, initTSN + 6},
{initTSN + 9, initTSN + 140},
}
range0, range1 := tsnRange[0], tsnRange[1]
for tsn := range0[0]; sna32LTE(tsn, range0[1]); tsn++ {
assert.True(t, q.push(tsn))
assert.False(t, q.pop(false))
assert.True(t, q.hasChunk(tsn))
}
size += int(range0[1] - range0[0] + 1)
for tsn := range1[0]; sna32LTE(tsn, range1[1]); tsn++ {
assert.True(t, q.push(tsn))
assert.False(t, q.pop(false))
assert.True(t, q.hasChunk(tsn))
}
size += int(range1[1] - range1[0] + 1)
assert.Equal(t, size, q.size())
gaps = q.getGapAckBlocks()
assert.EqualValues(t, []gapAckBlock{
{start: uint16(range0[0] - initTSN), end: uint16(range0[1] - initTSN)},
{start: uint16(range1[0] - initTSN), end: uint16(range1[1] - initTSN)},
{start: uint16(nextTSN - initTSN), end: uint16(nextTSN - initTSN)},
}, gaps)
// push duplicate tsns
assert.False(t, q.push(initTSN-2))
assert.False(t, q.push(range0[0]))
assert.False(t, q.push(range0[0]))
assert.False(t, q.push(nextTSN))
assert.False(t, q.push(initTSN+maxOffset+1))
duplicates := q.popDuplicates()
assert.EqualValues(t, []uint32{initTSN - 2, range0[0], range0[0], nextTSN}, duplicates)
// force pop to advance cumulativeTSN to fill the gap [initTSN, initTSN+4]
for tsn := initTSN + 1; sna32LT(tsn, range0[0]); tsn++ {
assert.False(t, q.pop(true))
assert.Equal(t, size, q.size())
assert.Equal(t, tsn, q.cumulativeTSN)
}
for tsn := range0[0]; sna32LTE(tsn, range0[1]); tsn++ {
assert.True(t, q.pop(false))
assert.Equal(t, tsn, q.getcumulativeTSN())
}
assert.False(t, q.pop(false))
cumulativeTSN := q.getcumulativeTSN()
assert.Equal(t, range0[1], cumulativeTSN)
gaps = q.getGapAckBlocks()
assert.EqualValues(t, []gapAckBlock{
{start: uint16(range1[0] - range0[1]), end: uint16(range1[1] - range0[1])},
{start: uint16(nextTSN - range0[1]), end: uint16(nextTSN - range0[1])},
}, gaps)
// fill the gap with received tsn
for tsn := range0[1] + 1; sna32LT(tsn, range1[0]); tsn++ {
assert.True(t, q.push(tsn), tsn)
}
for tsn := range0[1] + 1; sna32LTE(tsn, range1[1]); tsn++ {
assert.True(t, q.pop(false))
assert.Equal(t, tsn, q.getcumulativeTSN())
}
assert.False(t, q.pop(false))
assert.Equal(t, range1[1], q.getcumulativeTSN())
gaps = q.getGapAckBlocks()
assert.EqualValues(t, []gapAckBlock{
{start: uint16(nextTSN - range1[1]), end: uint16(nextTSN - range1[1])},
}, gaps)
// gap block cross end tsn
endTSN := maxOffset - 1
for tsn := nextTSN + 1; sna32LTE(tsn, endTSN); tsn++ {
assert.True(t, q.push(tsn))
}
gaps = q.getGapAckBlocks()
assert.EqualValues(t, []gapAckBlock{
{start: uint16(nextTSN - range1[1]), end: uint16(endTSN - range1[1])},
}, gaps)
assert.NotEmpty(t, q.getGapAckBlocksString())
}
func TestBitfunc(t *testing.T) {
idx, ok := getFirstNonZeroBit(0xf, 0, 20)
assert.True(t, ok)
assert.Equal(t, 0, idx)
_, ok = getFirstNonZeroBit(0xf<<20, 0, 20)
assert.False(t, ok)
idx, ok = getFirstNonZeroBit(0xf<<20, 5, 25)
assert.True(t, ok)
assert.Equal(t, 20, idx)
_, ok = getFirstNonZeroBit(0xf<<20, 30, 40)
assert.False(t, ok)
_, ok = getFirstNonZeroBit(0, 0, 64)
assert.False(t, ok)
idx, ok = getFirstZeroBit(0xf, 0, 20)
assert.True(t, ok)
assert.Equal(t, 4, idx)
idx, ok = getFirstZeroBit(0xf<<20, 0, 20)
assert.True(t, ok)
assert.Equal(t, 0, idx)
_, ok = getFirstZeroBit(0xf<<20, 20, 24)
assert.False(t, ok)
idx, ok = getFirstZeroBit(0xf<<20, 30, 40)
assert.True(t, ok)
assert.Equal(t, 30, idx)
_, ok = getFirstZeroBit(math.MaxUint64, 0, 64)
assert.False(t, ok)
}