-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcongestion_test.go
300 lines (231 loc) · 10.4 KB
/
congestion_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
package utp_go
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestOnTransmit(t *testing.T) {
ctrl := newDefaultController(defaultCtrlConfig())
initialTimeout := ctrl.Timeout()
// Register the initial transmission of a packet with sequence number 1
seqNum := uint16(1)
packetOneSizeBytes := uint32(32)
err := ctrl.OnTransmit(seqNum, Initial, packetOneSizeBytes)
require.NoError(t, err, "transmission registration failed")
transmissionRecord, exists := ctrl.transmissions[seqNum]
require.True(t, exists, "transmission not recorded")
require.Equal(t, packetOneSizeBytes, transmissionRecord.SizeBytes,
"expected size %d, got %d", packetOneSizeBytes, transmissionRecord.SizeBytes)
require.Equal(t, uint32(1), transmissionRecord.NumTransmissions,
"expected 1 transmission, got %d", transmissionRecord.NumTransmissions)
require.Equal(t, packetOneSizeBytes, ctrl.windowSizeBytes,
"expected window size %d, got %d", packetOneSizeBytes, ctrl.windowSizeBytes)
// Register the initial transmission of a packet with sequence number 2
seqNum = 2
packetTwoSizeBytes := uint32(128)
err = ctrl.OnTransmit(seqNum, Initial, packetTwoSizeBytes)
require.NoError(t, err, "transmission registration failed")
transmissionRecord, exists = ctrl.transmissions[seqNum]
require.True(t, exists, "transmission not recorded")
require.Equal(t, packetTwoSizeBytes, transmissionRecord.SizeBytes,
"expected size %d, got %d", packetOneSizeBytes, transmissionRecord.SizeBytes)
require.Equal(t, uint32(1), transmissionRecord.NumTransmissions,
"expected 1 transmission, got %d", transmissionRecord.NumTransmissions)
require.Equal(t, packetOneSizeBytes+packetTwoSizeBytes, ctrl.windowSizeBytes,
"expected window size %d, got %d", packetOneSizeBytes+packetTwoSizeBytes, ctrl.windowSizeBytes)
// Register the retransmission of the packet with sequence number 2
err = ctrl.OnTransmit(seqNum, Retransmission, 0)
require.NoError(t, err, "transmission registration failed")
transmissionRecord, exists = ctrl.transmissions[seqNum]
require.True(t, exists, "transmission not recorded")
require.Equal(t, packetTwoSizeBytes, transmissionRecord.SizeBytes,
"expected size %d, got %d", packetOneSizeBytes, transmissionRecord.SizeBytes)
require.Equal(t, uint32(2), transmissionRecord.NumTransmissions,
"expected 1 transmission, got %d", transmissionRecord.NumTransmissions)
require.Equal(t, packetOneSizeBytes+packetTwoSizeBytes, ctrl.windowSizeBytes,
"expected window size %d, got %d", packetOneSizeBytes+packetTwoSizeBytes, ctrl.windowSizeBytes)
if ctrl.Timeout() != initialTimeout {
t.Fatalf("expected timeout %v, got %v", initialTimeout, ctrl.Timeout())
}
}
func TestOnTransmitDuplicateTransmission(t *testing.T) {
ctrl := newDefaultController(defaultCtrlConfig())
// Register the initial transmission of a packet with sequence number 1
seqNum := uint16(1)
bytes := uint32(32)
err := ctrl.OnTransmit(seqNum, Initial, bytes)
require.NoError(t, err, "transmission registration failed")
require.Equal(t, bytes, ctrl.windowSizeBytes,
"expected window size %d, got %d", bytes, ctrl.windowSizeBytes)
// Register the initial transmission of the SAME packet
err = ctrl.OnTransmit(seqNum, Initial, bytes)
require.ErrorIs(t, err, ErrDuplicateTransmission, "expected ErrDuplicateTransmission, got %v", err)
require.Equal(t, bytes, ctrl.windowSizeBytes,
"expected window size %d, got %d", bytes, ctrl.windowSizeBytes)
}
func TestOnTransmitUnknownSeqNum(t *testing.T) {
ctrl := newDefaultController(defaultCtrlConfig())
// Register the retransmission of the packet with sequence number 1
seqNum := uint16(1)
err := ctrl.OnTransmit(seqNum, Retransmission, 0)
require.ErrorIs(t, err, ErrUnknownSeqNum, "expected ErrUnknownSeqNum, got %v", err)
require.Equal(t, uint32(0), ctrl.windowSizeBytes, "expected window size 0, got %d", ctrl.windowSizeBytes)
}
func TestOnTransmitInsufficientWindowSize(t *testing.T) {
ctrl := newDefaultController(defaultCtrlConfig())
// Register the transmission of a packet with sequence number 1
seqNum := uint16(1)
bytes := ctrl.maxWindowSizeBytes + 1
err := ctrl.OnTransmit(seqNum, Initial, bytes)
require.ErrorIs(t, err, ErrInsufficientWindowSize,
"expected ErrInsufficientWindowSize, got %v", err)
require.Equal(t, uint32(0), ctrl.windowSizeBytes,
"expected window size 0, got %d", ctrl.windowSizeBytes)
}
func TestOnAck(t *testing.T) {
ctrl := newDefaultController(defaultCtrlConfig())
// Register the initial transmission
seqNum := uint16(1)
bytes := uint32(32)
err := ctrl.OnTransmit(seqNum, Initial, bytes)
require.NoError(t, err, "transmission registration failed")
// Register the acknowledgement
ackDelay := 150 * time.Millisecond
ackRTT := 300 * time.Millisecond
ackReceivedAt := time.Now()
ack := Ack{
Delay: ackDelay,
RTT: ackRTT,
ReceivedAt: ackReceivedAt,
}
err = ctrl.OnAck(seqNum, ack)
require.NoError(t, err, "ack registration failed")
// Check base delay
baseDelay := ctrl.delayAcc.BaseDelay()
require.Equal(t, ackDelay, baseDelay, "expected base delay %v, got %v", ackDelay, baseDelay)
// Check window size
require.Equal(t, uint32(0), ctrl.windowSizeBytes, "expected window size 0, got %d", ctrl.windowSizeBytes)
// Check timeout
require.True(t, ctrl.minTimeout >= ctrl.Timeout(), "expected timeout %v, got %v", ctrl.minTimeout, ctrl.Timeout())
}
func TestOnAckUnknownSeqNum(t *testing.T) {
ctrl := newDefaultController(defaultCtrlConfig())
// Register the acknowledgement for packet with sequence number 1
seqNum := uint16(1)
ack := Ack{
Delay: 150 * time.Millisecond,
RTT: 300 * time.Millisecond,
ReceivedAt: time.Now(),
}
err := ctrl.OnAck(seqNum, ack)
require.ErrorIs(t, err, ErrUnknownSeqNum, "expected ErrUnknownSeqNum, got %v", err)
}
func TestOnLostPacketRetransmitting(t *testing.T) {
ctrl := newDefaultController(defaultCtrlConfig())
initialMaxWindowSizeBytes := ctrl.minWindowSizeBytes * 10
ctrl.maxWindowSizeBytes = initialMaxWindowSizeBytes
// Register initial transmission
seqNum := uint16(1)
bytes := uint32(32)
err := ctrl.OnTransmit(seqNum, Initial, bytes)
require.NoError(t, err, "transmission registration failed")
require.Equal(t, bytes, ctrl.windowSizeBytes,
"expected window size %d, got %d", bytes, ctrl.windowSizeBytes)
// Register packet loss with retransmission
err = ctrl.OnLostPacket(seqNum, true)
require.NoError(t, err, "lost packet registration failed")
require.Equal(t, bytes, ctrl.windowSizeBytes,
"expected window size %d, got %d", bytes, ctrl.windowSizeBytes)
require.True(t, ctrl.maxWindowSizeBytes >= ctrl.minWindowSizeBytes,
"max window size less than min window size")
expectedMaxWindow := initialMaxWindowSizeBytes / 2
require.Equal(t, expectedMaxWindow, ctrl.maxWindowSizeBytes,
"expected max window size %d, got %d", expectedMaxWindow, ctrl.maxWindowSizeBytes)
}
func TestOnLostPacketUnknownSeqNum(t *testing.T) {
ctrl := newDefaultController(defaultCtrlConfig())
// Set initial max window size
initialMaxWindowSizeBytes := ctrl.minWindowSizeBytes * 10
ctrl.maxWindowSizeBytes = initialMaxWindowSizeBytes
// Try to register loss for unknown sequence number
seqNum := uint16(1)
err := ctrl.OnLostPacket(seqNum, false)
// Check error
require.ErrorIs(t, err, ErrUnknownSeqNum, "lost packet registration failed")
// Verify window sizes unchanged
require.Equal(t, uint32(0), ctrl.windowSizeBytes, "expected window size 0, got %d", ctrl.windowSizeBytes)
require.Equal(t, initialMaxWindowSizeBytes, ctrl.maxWindowSizeBytes,
"expected max window size %d, got %d", initialMaxWindowSizeBytes, ctrl.maxWindowSizeBytes)
}
func TestOnTimeout(t *testing.T) {
ctrl := newDefaultController(defaultCtrlConfig())
// Set initial max window size
initialMaxWindowSizeBytes := ctrl.minWindowSizeBytes * 10
ctrl.maxWindowSizeBytes = initialMaxWindowSizeBytes
// Store initial timeout
initialTimeout := ctrl.Timeout()
// Register timeout
ctrl.OnTimeout()
// Verify max window size was reset to minimum
require.Equal(t, ctrl.minWindowSizeBytes, ctrl.maxWindowSizeBytes,
"expected max window size %d, got %d", ctrl.minWindowSizeBytes, ctrl.maxWindowSizeBytes)
// Verify timeout was doubled
expectedTimeout := initialTimeout * 2
require.Equal(t, expectedTimeout, ctrl.Timeout(),
"expected timeout %v, got %v", expectedTimeout, ctrl.Timeout())
}
func TestOnTimeoutNotExceedMax(t *testing.T) {
config := defaultCtrlConfig()
config.InitialTimeout = 2 * time.Second
config.MaxTimeout = 3 * time.Second
ctrl := newDefaultController(config)
// Register timeout
ctrl.OnTimeout()
// Verify timeout is capped at max
require.Equal(t, config.MaxTimeout, ctrl.Timeout(),
"expected timeout %v, got %v", config.MaxTimeout, ctrl.Timeout())
}
func TestPush(t *testing.T) {
window := 100 * time.Millisecond
acc := newDelayAccumulator(window)
delay := 50 * time.Millisecond
delayReceivedAt := time.Now()
acc.Push(delay, delayReceivedAt)
require.Equal(t, 1, acc.delays.Len(), "delay not pushed onto accumulator")
item := (*acc.delays)[0]
require.Equal(t, delay, item.Value,
"expected delay %v, got %v", delay, item.Value)
expectedDeadline := delayReceivedAt.Add(window)
require.Equal(t, expectedDeadline, item.Deadline,
"expected delay %v, got %v", expectedDeadline, item.Deadline)
}
func TestBaseDelay(t *testing.T) {
window := 100 * time.Millisecond
acc := newDelayAccumulator(window)
// Push delays in descending order
delaySmall := 50 * time.Millisecond
delaySmallReceivedAt := time.Now()
acc.Push(delaySmall, delaySmallReceivedAt)
delaySmaller := 25 * time.Millisecond
delaySmallerReceivedAt := time.Now()
acc.Push(delaySmaller, delaySmallerReceivedAt)
delaySmallest := 5 * time.Millisecond
delaySmallestReceivedAt := time.Now()
acc.Push(delaySmallest, delaySmallestReceivedAt)
delayExpired := 1 * time.Millisecond
delayExpiredReceivedAt := time.Now().Add(-window)
acc.Push(delayExpired, delayExpiredReceivedAt)
// Check that all delays are present
require.Equal(t, 4, acc.delays.Len(), "expected 4 delays, got %d", acc.delays.Len())
// Get base delay
baseDelay := acc.BaseDelay()
require.Equal(t, delaySmallest, baseDelay, "expected base delay %v, got %v", delaySmallest, baseDelay)
// Check that expired delay was removed
require.Equal(t, 3, acc.delays.Len(), "expected 3 delays, got %d", acc.delays.Len())
}
func TestBaseDelayEmpty(t *testing.T) {
window := time.Millisecond * 100
acc := newDelayAccumulator(window)
baseDelay := acc.BaseDelay()
require.Equal(t, time.Duration(0), baseDelay, "expected base delay 0, got %v", baseDelay)
}