-
Notifications
You must be signed in to change notification settings - Fork 79
/
pending_queue_test.go
193 lines (160 loc) · 5.31 KB
/
pending_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package sctp
import (
"testing"
"github.com/stretchr/testify/assert"
)
const (
noFragment = iota
fragBegin
fragMiddle
fragEnd
)
func makeDataChunk(tsn uint32, unordered bool, frag int) *chunkPayloadData {
var b, e bool
switch frag {
case noFragment:
b = true
e = true
case fragBegin:
b = true
case fragEnd:
e = true
}
return &chunkPayloadData{
tsn: tsn,
unordered: unordered,
beginningFragment: b,
endingFragment: e,
userData: make([]byte, 10), // always 10 bytes
}
}
func TestPendingBaseQueue(t *testing.T) {
t.Run("push and pop", func(t *testing.T) {
pq := newPendingBaseQueue()
pq.push(makeDataChunk(0, false, noFragment))
pq.push(makeDataChunk(1, false, noFragment))
pq.push(makeDataChunk(2, false, noFragment))
for i := uint32(0); i < 3; i++ {
c := pq.get(int(i))
assert.NotNil(t, c, "should not be nil")
assert.Equal(t, i, c.tsn, "TSN should match")
}
for i := uint32(0); i < 3; i++ {
c := pq.pop()
assert.NotNil(t, c, "should not be nil")
assert.Equal(t, i, c.tsn, "TSN should match")
}
pq.push(makeDataChunk(3, false, noFragment))
pq.push(makeDataChunk(4, false, noFragment))
for i := uint32(3); i < 5; i++ {
c := pq.pop()
assert.NotNil(t, c, "should not be nil")
assert.Equal(t, i, c.tsn, "TSN should match")
}
})
t.Run("out of bounce", func(t *testing.T) {
pq := newPendingBaseQueue()
assert.Nil(t, pq.pop(), "should be nil")
assert.Nil(t, pq.get(0), "should be nil")
pq.push(makeDataChunk(0, false, noFragment))
assert.Nil(t, pq.get(-1), "should be nil")
assert.Nil(t, pq.get(1), "should be nil")
})
}
func TestPendingQueue(t *testing.T) {
// NOTE: TSN is not used in pendingQueue in the actual usage.
// Following tests use TSN field as a chunk ID.
t.Run("push and pop", func(t *testing.T) {
pq := newPendingQueue()
pq.push(makeDataChunk(0, false, noFragment))
assert.Equal(t, 10, pq.getNumBytes(), "total bytes mismatch")
pq.push(makeDataChunk(1, false, noFragment))
assert.Equal(t, 20, pq.getNumBytes(), "total bytes mismatch")
pq.push(makeDataChunk(2, false, noFragment))
assert.Equal(t, 30, pq.getNumBytes(), "total bytes mismatch")
for i := uint32(0); i < 3; i++ {
c := pq.peek()
err := pq.pop(c)
assert.Nil(t, err, "should not error")
assert.Equal(t, i, c.tsn, "TSN should match")
}
assert.Equal(t, 0, pq.getNumBytes(), "total bytes mismatch")
pq.push(makeDataChunk(3, false, noFragment))
assert.Equal(t, 10, pq.getNumBytes(), "total bytes mismatch")
pq.push(makeDataChunk(4, false, noFragment))
assert.Equal(t, 20, pq.getNumBytes(), "total bytes mismatch")
for i := uint32(3); i < 5; i++ {
c := pq.peek()
err := pq.pop(c)
assert.Nil(t, err, "should not error")
assert.Equal(t, i, c.tsn, "TSN should match")
}
assert.Equal(t, 0, pq.getNumBytes(), "total bytes mismatch")
})
t.Run("unordered wins", func(t *testing.T) {
pq := newPendingQueue()
pq.push(makeDataChunk(0, false, noFragment))
assert.Equal(t, 10, pq.getNumBytes(), "total bytes mismatch")
pq.push(makeDataChunk(1, true, noFragment))
assert.Equal(t, 20, pq.getNumBytes(), "total bytes mismatch")
pq.push(makeDataChunk(2, false, noFragment))
assert.Equal(t, 30, pq.getNumBytes(), "total bytes mismatch")
pq.push(makeDataChunk(3, true, noFragment))
assert.Equal(t, 40, pq.getNumBytes(), "total bytes mismatch")
c := pq.peek()
err := pq.pop(c)
assert.NoError(t, err, "should not error")
assert.Equal(t, uint32(1), c.tsn, "TSN should match")
c = pq.peek()
err = pq.pop(c)
assert.NoError(t, err, "should not error")
assert.Equal(t, uint32(3), c.tsn, "TSN should match")
c = pq.peek()
err = pq.pop(c)
assert.NoError(t, err, "should not error")
assert.Equal(t, uint32(0), c.tsn, "TSN should match")
c = pq.peek()
err = pq.pop(c)
assert.NoError(t, err, "should not error")
assert.Equal(t, uint32(2), c.tsn, "TSN should match")
assert.Equal(t, 0, pq.getNumBytes(), "total bytes mismatch")
})
t.Run("fragments", func(t *testing.T) {
pq := newPendingQueue()
pq.push(makeDataChunk(0, false, fragBegin))
pq.push(makeDataChunk(1, false, fragMiddle))
pq.push(makeDataChunk(2, false, fragEnd))
pq.push(makeDataChunk(3, true, fragBegin))
pq.push(makeDataChunk(4, true, fragMiddle))
pq.push(makeDataChunk(5, true, fragEnd))
expects := []uint32{3, 4, 5, 0, 1, 2}
for _, exp := range expects {
c := pq.peek()
err := pq.pop(c)
assert.NoError(t, err, "should not error")
assert.Equal(t, exp, c.tsn, "TSN should match")
}
})
// Once decided ordered or unordered, the decision should persist until
// it pops a chunk with endingFragment flags set to true.
t.Run("selection persistence", func(t *testing.T) {
pq := newPendingQueue()
pq.push(makeDataChunk(0, false, fragBegin))
c := pq.peek()
err := pq.pop(c)
assert.NoError(t, err, "should not error")
assert.Equal(t, uint32(0), c.tsn, "TSN should match")
pq.push(makeDataChunk(1, true, noFragment))
pq.push(makeDataChunk(2, false, fragMiddle))
pq.push(makeDataChunk(3, false, fragEnd))
expects := []uint32{2, 3, 1}
for _, exp := range expects {
c = pq.peek()
err = pq.pop(c)
assert.NoError(t, err, "should not error")
assert.Equal(t, exp, c.tsn, "TSN should match")
}
})
}