-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrcv_test.go
440 lines (381 loc) · 11.4 KB
/
rcv_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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package tomtp
import (
"bytes"
"github.com/stretchr/testify/assert"
"testing"
)
func TestReceiveBuffer_BasicOperations(t *testing.T) {
rb := NewReceiveBuffer(1000)
// Test initial state
if rb.Size() != 0 {
t.Errorf("Initial size = %v, want 0", rb.Size())
}
if rb.IsClosed() {
t.Error("New buffer should not be closed")
}
// Test basic insert and remove
segment := &RcvSegment{
offset: 0,
data: []byte("hello"),
}
if status := rb.Insert(segment); status != RcvInsertOk {
t.Errorf("Insert status = %v, want %v", status, RcvInsertOk)
}
if got := rb.Size(); got != 5 {
t.Errorf("After insert size = %v, want 5", got)
}
// Test removal
if got := rb.RemoveOldestInOrder(); got == nil {
t.Error("RemoveOldestInOrder() = nil, want segment")
} else if !bytes.Equal(got.data, []byte("hello")) {
t.Errorf("RemoveOldestInOrder() data = %v, want %v", got.data, []byte("hello"))
}
}
func TestReceiveBuffer_OutOfOrderInsert(t *testing.T) {
rb := NewReceiveBuffer(1000)
// Insert out of order segments
segments := []*RcvSegment{
{offset: 5, data: []byte("world")},
{offset: 0, data: []byte("hello")},
}
// Insert out of order
for _, seg := range segments {
if status := rb.Insert(seg); status != RcvInsertOk {
t.Errorf("Insert status = %v, want %v", status, RcvInsertOk)
}
}
// Should only get the first segment
if got := rb.RemoveOldestInOrder(); got == nil {
t.Error("RemoveOldestInOrder() = nil, want first segment")
} else if !bytes.Equal(got.data, []byte("hello")) {
t.Errorf("RemoveOldestInOrder() data = %v, want %v", got.data, []byte("hello"))
}
// Second segment should now be available
if got := rb.RemoveOldestInOrder(); got == nil {
t.Error("RemoveOldestInOrder() = nil, want second segment")
} else if !bytes.Equal(got.data, []byte("world")) {
t.Errorf("RemoveOldestInOrder() data = %v, want %v", got.data, []byte("world"))
}
}
func TestReceiveBuffer_Duplicates(t *testing.T) {
rb := NewReceiveBuffer(1000)
// Test exact duplicate
seg1 := &RcvSegment{offset: 0, data: []byte("hello")}
if status := rb.Insert(seg1); status != RcvInsertOk {
t.Errorf("First insert status = %v, want %v", status, RcvInsertOk)
}
if status := rb.Insert(seg1); status != RcvInsertDuplicate {
t.Errorf("Duplicate insert status = %v, want %v", status, RcvInsertDuplicate)
}
// Test larger retransmission
seg2 := &RcvSegment{offset: 0, data: []byte("hello_world")}
if status := rb.Insert(seg2); status != RcvInsertOk {
t.Errorf("Larger segment insert status = %v, want %v", status, RcvInsertOk)
}
// Test smaller retransmission
seg3 := &RcvSegment{offset: 0, data: []byte("hi")}
if status := rb.Insert(seg3); status != RcvInsertDuplicate {
t.Errorf("Smaller segment insert status = %v, want %v", status, RcvInsertDuplicate)
}
}
func TestReceiveBuffer_PartialData(t *testing.T) {
rb := NewReceiveBuffer(1000)
// Insert and remove first segment
seg1 := &RcvSegment{offset: 0, data: []byte("hello")}
rb.Insert(seg1)
rb.RemoveOldestInOrder()
// Try to insert segment that overlaps with removed data
seg2 := &RcvSegment{offset: 3, data: []byte("lo_world")}
if status := rb.Insert(seg2); status != RcvInsertOk {
t.Errorf("Partial new data insert status = %v, want %v", status, RcvInsertOk)
}
// Check that only new data was stored
if got := rb.RemoveOldestInOrder(); got == nil {
t.Error("RemoveOldestInOrder() = nil, want partial segment")
} else if !bytes.Equal(got.data, []byte("_world")) {
t.Errorf("Partial segment data = %v, want %v", got.data, []byte("_world"))
}
}
func TestReceiveBuffer_Capacity(t *testing.T) {
rb := NewReceiveBuffer(10) // Small capacity
// Fill buffer
seg1 := &RcvSegment{offset: 0, data: bytes.Repeat([]byte("a"), 8)}
if status := rb.Insert(seg1); status != RcvInsertOk {
t.Errorf("First insert status = %v, want %v", status, RcvInsertOk)
}
// Try to exceed capacity
seg2 := &RcvSegment{offset: 8, data: bytes.Repeat([]byte("b"), 4)}
if status := rb.Insert(seg2); status != RcvInsertBufferFull {
t.Errorf("Exceeding capacity status = %v, want %v", status, RcvInsertBufferFull)
}
// Remove some data and try again
rb.RemoveOldestInOrder()
if status := rb.Insert(seg2); status != RcvInsertOk {
t.Errorf("After removal insert status = %v, want %v", status, RcvInsertOk)
}
}
func TestReceiveBuffer_Close(t *testing.T) {
rb := NewReceiveBuffer(1000)
// Insert before closing
seg1 := &RcvSegment{offset: 0, data: []byte("hello")}
if status := rb.Insert(seg1); status != RcvInsertOk {
t.Errorf("Pre-close insert status = %v, want %v", status, RcvInsertOk)
}
// Close buffer
rb.Close()
if !rb.IsClosed() {
t.Error("IsClosed() = false after Close()")
}
// Try to insert after closing
seg2 := &RcvSegment{offset: 5, data: []byte("world")}
if status := rb.Insert(seg2); status != RcvInsertBufferFull {
t.Errorf("Post-close insert status = %v, want %v", status, RcvInsertBufferFull)
}
// Should still be able to remove
if got := rb.RemoveOldestInOrder(); got == nil {
t.Error("RemoveOldestInOrder() = nil after close")
}
}
func TestReceiveBuffer_NonByteTypes(t *testing.T) {
rb := NewReceiveBuffer(1000)
// Test with string type
seg1 := &RcvSegment{offset: 0, data: []byte("hello")}
if status := rb.Insert(seg1); status != RcvInsertOk {
t.Errorf("String insert status = %v, want %v", status, RcvInsertOk)
}
if got := rb.RemoveOldestInOrder(); got == nil {
assert.Equal(t, got.data, []byte("hello"))
t.Error("Failed to handle string type correctly")
}
// Test with custom type
type CustomType struct{ value int }
rb2 := NewReceiveBuffer(1000)
seg2 := &RcvSegment{offset: 0, data: []byte{42}}
if status := rb2.Insert(seg2); status != RcvInsertOk {
t.Errorf("Custom type insert status = %v, want %v", status, RcvInsertOk)
}
}
func TestReceiveBuffer_OverlappingRanges(t *testing.T) {
rb := NewReceiveBuffer(1000)
// Insert first segment
seg1 := &RcvSegment{
offset: 100,
data: []byte("middle"),
}
rb.Insert(seg1)
// Test cases for overlapping segments
tests := []struct {
name string
segment *RcvSegment
wantSize int
wantStatus RcvInsertStatus
}{
{
name: "before existing range",
segment: &RcvSegment{
offset: 90,
data: []byte("beforemid"),
},
wantSize: 14,
wantStatus: RcvInsertOk,
},
{
name: "after existing range",
segment: &RcvSegment{
offset: 106,
data: []byte("endpart"),
},
wantSize: 21,
wantStatus: RcvInsertOk,
},
{
name: "completely within range",
segment: &RcvSegment{
offset: 101,
data: []byte("mid"),
},
wantSize: 21,
wantStatus: RcvInsertDuplicate,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
status := rb.Insert(tt.segment)
if status != tt.wantStatus {
t.Errorf("Insert status = %v, want %v", status, tt.wantStatus)
}
if got := rb.Size(); got != tt.wantSize {
t.Errorf("Size after insert = %v, want %v", got, tt.wantSize)
}
})
}
}
func TestReceiveBuffer_SequentialRemoval(t *testing.T) {
rb := NewReceiveBuffer(1000)
// Insert segments out of order
segments := []*RcvSegment{
{offset: 10, data: []byte("third")},
{offset: 0, data: []byte("first")},
{offset: 5, data: []byte("second")},
}
for _, seg := range segments {
rb.Insert(seg)
}
// Remove segments in order
expected := [][]byte{
[]byte("first"),
[]byte("second"),
[]byte("third"),
}
for i, want := range expected {
got := rb.RemoveOldestInOrder()
if got == nil {
t.Fatalf("RemoveOldestInOrder() returned nil for segment %d", i)
}
if !bytes.Equal(got.data, want) {
t.Errorf("Segment %d data = %v, want %v", i, got.data, want)
}
}
// Verify buffer is empty
if rb.Size() != 0 {
t.Errorf("Final size = %v, want 0", rb.Size())
}
}
func TestReceiveBuffer_48BitRollover(t *testing.T) {
rb := NewReceiveBuffer(1000)
// Set nextOffset near the 48-bit limit
rb.nextOffset = MaxUint48 - 10
tests := []struct {
name string
insert *RcvSegment
wantStatus RcvInsertStatus
wantRemove bool // whether RemoveOldestInOrder should return a segment
checkOffset bool // whether to check the nextOffset after removal
wantOffset uint64 // expected nextOffset after removal
}{
{
name: "insert at rollover boundary",
insert: &RcvSegment{
offset: MaxUint48 - 10,
data: []byte("rollover"),
},
wantStatus: RcvInsertOk,
wantRemove: true,
checkOffset: true,
wantOffset: MaxUint48 - 2, // -10 + 8 (len("rollover"))
},
{
name: "insert spanning rollover",
insert: &RcvSegment{
offset: MaxUint48 - 2,
data: []byte("span"),
},
wantStatus: RcvInsertOk,
wantRemove: true,
checkOffset: true,
wantOffset: 2, // (MaxUint48 - 2 + 4) % MaxUint48
},
{
name: "insert after rollover",
insert: &RcvSegment{
offset: 2,
data: []byte("after"),
},
wantStatus: RcvInsertOk,
wantRemove: true,
checkOffset: true,
wantOffset: 7, // 2 + 5
},
{
name: "duplicate after rollover",
insert: &RcvSegment{
offset: 2,
data: []byte("duplicate"),
},
wantStatus: RcvInsertDuplicate,
wantRemove: false,
checkOffset: false,
},
{
name: "old data before rollover",
insert: &RcvSegment{
offset: MaxUint48 - 20,
data: []byte("old"),
},
wantStatus: RcvInsertDuplicate,
wantRemove: false,
checkOffset: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Insert segment
status := rb.Insert(tt.insert)
if status != tt.wantStatus {
t.Errorf("Insert() status = %v, want %v", status, tt.wantStatus)
}
// Check removal if expected
if tt.wantRemove {
got := rb.RemoveOldestInOrder()
if got == nil {
t.Fatal("RemoveOldestInOrder() = nil, want segment")
}
if !bytes.Equal(got.data, tt.insert.data) {
t.Errorf("RemoveOldestInOrder() data = %v, want %v", got.data, tt.insert.data)
}
if tt.checkOffset && rb.nextOffset != tt.wantOffset {
t.Errorf("nextOffset after remove = %v, want %v", rb.nextOffset, tt.wantOffset)
}
}
})
}
}
func TestReceiveBuffer_LargeSequenceJump(t *testing.T) {
rb := NewReceiveBuffer(1000)
// Test large sequence number jumps
segments := []*RcvSegment{
{offset: 5, data: []byte("first")},
{offset: MaxUint48 - 10, data: []byte("jump")},
{offset: 2, data: []byte("after_rollover")},
}
// Insert all segments (out of order)
for _, seg := range segments {
status := rb.Insert(seg)
if status != RcvInsertOk {
t.Errorf("Insert offset %v status = %v, want %v", seg.offset, status, RcvInsertOk)
}
}
// Only the first segment should be available for removal
got := rb.RemoveOldestInOrder()
if got == nil || got.offset != 5 {
t.Errorf("RemoveOldestInOrder() offset = %v, want 5", got.offset)
}
}
func TestReceiveBuffer_ExactRollover(t *testing.T) {
rb := NewReceiveBuffer(1000)
rb.nextOffset = MaxUint48 - 1
// Test exact rollover point
seg1 := &RcvSegment{
offset: MaxUint48 - 1,
data: []byte("x"),
}
if status := rb.Insert(seg1); status != RcvInsertOk {
t.Errorf("Insert at MaxUint48-1 status = %v, want %v", status, RcvInsertOk)
}
got := rb.RemoveOldestInOrder()
if got == nil {
t.Fatal("RemoveOldestInOrder() = nil, want segment")
}
// Next offset should be exactly 0
if rb.nextOffset != 0 {
t.Errorf("nextOffset = %v, want 0", rb.nextOffset)
}
// Insert at offset 0 should work
seg2 := &RcvSegment{
offset: 0,
data: []byte("y"),
}
if status := rb.Insert(seg2); status != RcvInsertOk {
t.Errorf("Insert at 0 status = %v, want %v", status, RcvInsertOk)
}
}