-
Notifications
You must be signed in to change notification settings - Fork 0
/
sent_packet_test.go
324 lines (261 loc) · 8.76 KB
/
sent_packet_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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package utp_go
import (
"fmt"
"math"
"testing"
"time"
"github.com/stretchr/testify/require"
)
const TEST_DELAY = time.Duration(time.Millisecond * 100)
func TestNextSeqNum(t *testing.T) {
}
func TestOnTransmitInitial(t *testing.T) {
ctrl := newDefaultController(fromConnConfig(NewConnectionConfig()))
initSeqNum := uint16(math.MaxUint16)
sentPackets := newSentPacketsWithoutLogger(initSeqNum, ctrl)
seqNum := sentPackets.NextSeqNum()
data := []byte{0}
length := uint32(len(data))
now := time.Now()
sentPackets.OnTransmit(seqNum, st_data, data, length, now)
if len(sentPackets.packets) != 1 {
t.Errorf("expected 1 packet, got %d", len(sentPackets.packets))
}
packetInst := sentPackets.packets[0]
if packetInst.seqNum != seqNum {
t.Errorf("expected seq num %d, got %d", seqNum, packetInst.seqNum)
}
if !packetInst.transmission.Equal(now) {
t.Errorf("expected transmission time %v, got %v", now, packetInst.transmission)
}
if len(packetInst.acks) != 0 {
t.Errorf("expected empty acks, got %d", len(packetInst.acks))
}
if len(packetInst.retransmissions) != 0 {
t.Errorf("expected empty retransmissions, got %d", len(packetInst.retransmissions))
}
}
func TestOnTransmitRetransmit(t *testing.T) {
initSeqNum := uint16(math.MaxUint16)
ctrl := newDefaultController(fromConnConfig(NewConnectionConfig()))
sentPackets := newSentPacketsWithoutLogger(initSeqNum, ctrl)
seqNum := sentPackets.NextSeqNum()
data := []byte{0}
length := uint32(len(data))
first := time.Now()
second := time.Now()
sentPackets.OnTransmit(seqNum, st_data, data, length, first)
sentPackets.OnTransmit(seqNum, st_data, data, length, second)
if len(sentPackets.packets) != 1 {
t.Errorf("expected 1 packet, got %d", len(sentPackets.packets))
}
packetInst := sentPackets.packets[0]
if packetInst.seqNum != seqNum {
t.Errorf("expected seq num %d, got %d", seqNum, packetInst.seqNum)
}
if !packetInst.transmission.Equal(first) {
t.Errorf("expected transmission time %v, got %v", first, packetInst.transmission)
}
if len(packetInst.acks) != 0 {
t.Errorf("expected empty acks, got %d", len(packetInst.acks))
}
if len(packetInst.retransmissions) != 1 {
t.Errorf("expected 1 retransmission, got %d", len(packetInst.retransmissions))
}
if !packetInst.retransmissions[0].Equal(second) {
t.Errorf("expected retransmission time %v, got %v", second, packetInst.retransmissions[0])
}
}
func TestOnTransmitOutOfOrder(t *testing.T) {
initSeqNum := uint16(math.MaxUint16)
ctrl := newDefaultController(fromConnConfig(NewConnectionConfig()))
sentPackets := newSentPacketsWithoutLogger(initSeqNum, ctrl)
outOfOrderSeqNum := initSeqNum + 2 // wrapping addition for uint16
data := []byte{0}
length := uint32(len(data))
now := time.Now()
// This should panic due to out of order transmit
defer func() {
if r := recover(); r == nil {
t.Error("expected panic for out of order transmit")
}
}()
sentPackets.OnTransmit(outOfOrderSeqNum, st_data, data, length, now)
}
func TestOnSelectiveAck(t *testing.T) {
const (
COUNT = 10
SACK_LEN = COUNT - 2
DELAY = time.Millisecond * 100 // Assuming this value
)
ctrl := newDefaultController(fromConnConfig(NewConnectionConfig()))
initSeqNum := uint16(math.MaxUint16)
sentPackets := newSentPacketsWithoutLogger(initSeqNum, ctrl)
data := []byte{0}
length := uint32(len(data))
// Send COUNT packets
for i := 0; i < COUNT; i++ {
now := time.Now()
seqNum := sentPackets.NextSeqNum()
sentPackets.OnTransmit(seqNum, st_data, data, length, now)
}
// Create selective ACK
acked := make([]bool, SACK_LEN)
for i := range acked {
if i%2 == 0 {
acked[i] = true
}
}
selectiveAck := NewSelectiveAck(acked)
// Process ACK
now := time.Now()
_, _, err := sentPackets.onAck(initSeqNum+1, selectiveAck, DELAY, now)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Verify ACKs
if len(sentPackets.packets[0].acks) != 1 {
t.Errorf("expected 1 ack for first packet, got %d", len(sentPackets.packets[0].acks))
}
if len(sentPackets.packets[1].acks) != 0 {
t.Errorf("expected no acks for second packet, got %d", len(sentPackets.packets[1].acks))
}
for i := 2; i < COUNT; i++ {
isEmpty := i%2 != 0
require.Equal(t, isEmpty, len(sentPackets.packets[i].acks) == 0, fmt.Sprintf("packet %d: expected acks empty=%v, got %v", i, isEmpty, len(sentPackets.packets[i].acks)))
}
}
func TestDetectLostPackets(t *testing.T) {
const (
COUNT = 10
START = COUNT - LossThreshold
DELAY = time.Millisecond * 100
)
ctrl := newDefaultController(fromConnConfig(NewConnectionConfig()))
initSeqNum := uint16(math.MaxUint16)
sentPackets := newSentPacketsWithoutLogger(initSeqNum, ctrl)
data := []byte{0}
length := uint32(len(data))
// Send packets and selectively acknowledge them
for i := 0; i < COUNT; i++ {
now := time.Now()
seqNum := sentPackets.NextSeqNum()
sentPackets.OnTransmit(seqNum, st_data, data, length, now)
if i >= START {
err := sentPackets.Ack(seqNum, DELAY, now)
require.NoError(t, err)
}
}
// Detect lost packets
firstUnacked, err := sentPackets.FirstUnackedSeqNum()
require.NoError(t, err)
lost := sentPackets.DetectLostPackets(firstUnacked)
// Verify lost packets
for i := 0; i < START; i++ {
packetInst := sentPackets.packets[i]
var found bool
for _, lostSeq := range lost {
if found = packetInst.seqNum == lostSeq; found {
break
}
}
require.True(t, found, fmt.Sprintf("packet %d (seq=%d) should be marked as lost", i, packetInst.seqNum))
}
}
func TestAck(t *testing.T) {
const DELAY = time.Millisecond * 100
ctrl := newDefaultController(fromConnConfig(NewConnectionConfig()))
initSeqNum := uint16(math.MaxUint16)
sentPackets := newSentPacketsWithoutLogger(initSeqNum, ctrl)
seqNum := sentPackets.NextSeqNum()
data := []byte{0}
length := uint32(len(data))
now := time.Now()
sentPackets.OnTransmit(seqNum, st_data, data, length, now)
// Artificially insert packet into lost packets
sentPackets.lostPackets.ReplaceOrInsert(seqNum)
// Acknowledge the packet
now = time.Now()
err := sentPackets.Ack(seqNum, DELAY, now)
require.NoError(t, err)
index := sentPackets.SeqNumIndex(seqNum)
packetInst := sentPackets.packets[index]
if len(packetInst.acks) != 1 {
t.Errorf("expected 1 ack, got %d", len(packetInst.acks))
}
if !packetInst.acks[0].Equal(now) {
t.Errorf("expected ack time %v, got %v", now, packetInst.acks[0])
}
sentPackets.lostPackets.Ascend(func(lostSeq uint16) bool {
if lostSeq == packetInst.seqNum {
t.Error("packet should not be marked as lost")
return false
}
return true
})
}
func TestAckPriorUnacked(t *testing.T) {
const (
COUNT = 10
ACK_NUM = uint16(3)
DELAY = time.Millisecond * 100
)
ctrl := newDefaultController(fromConnConfig(NewConnectionConfig()))
initSeqNum := uint16(math.MaxUint16)
sentPackets := newSentPacketsWithoutLogger(initSeqNum, ctrl)
data := []byte{0}
length := uint32(len(data))
// Send COUNT packets
for i := 0; i < COUNT; i++ {
now := time.Now()
seqNum := sentPackets.NextSeqNum()
sentPackets.OnTransmit(seqNum, st_data, data, length, now)
}
// Verify test preconditions
if int(ACK_NUM) >= COUNT {
t.Fatal("ACK_NUM must be less than COUNT")
}
if COUNT-int(ACK_NUM) <= 2 {
t.Fatal("COUNT minus ACK_NUM must be greater than 2")
}
// Acknowledge prior innerMap packets
now := time.Now()
firstUnacked, err := sentPackets.FirstUnackedSeqNum()
require.NoError(t, err)
err = sentPackets.AckPriorUnacked(ACK_NUM, firstUnacked, DELAY, now)
require.NoError(t, err)
// Verify acknowledgments
for i := 0; i < int(ACK_NUM); i++ {
if len(sentPackets.packets[i].acks) != 1 {
t.Errorf("packet %d: expected 1 ack, got %d", i, len(sentPackets.packets[i].acks))
}
}
}
func TestAckUnsent(t *testing.T) {
const DELAY = time.Millisecond * 100
ctrl := newDefaultController(fromConnConfig(NewConnectionConfig()))
initSeqNum := uint16(math.MaxUint16)
sentPackets := newSentPacketsWithoutLogger(initSeqNum, ctrl)
unsentAckNum := initSeqNum + 2 // wrapping addition for uint16
now := time.Now()
// This should panic when trying to acknowledge an unsent packet
defer func() {
if r := recover(); r == nil {
t.Error("expected panic when acknowledging unsent packet")
}
}()
err := sentPackets.Ack(unsentAckNum, DELAY, now)
require.Error(t, err, "expected error when acknowledging unsent packet")
}
func TestSeqNumIndex(t *testing.T) {
ctrl := newDefaultController(fromConnConfig(NewConnectionConfig()))
initSeqNum := uint16(math.MaxUint16)
sentPackets := newSentPacketsWithoutLogger(initSeqNum, ctrl)
if sentPackets.SeqNumIndex(initSeqNum) != math.MaxUint16 {
t.Errorf("expected index %d, got %d", math.MaxUint16, sentPackets.SeqNumIndex(initSeqNum))
}
zero := initSeqNum + 1 // wrapping addition for uint16
if sentPackets.SeqNumIndex(zero) != 0 {
t.Errorf("expected index 0, got %d", sentPackets.SeqNumIndex(zero))
}
}