forked from xtaci/kcp-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbufferpool_test.go
More file actions
79 lines (60 loc) · 1.59 KB
/
bufferpool_test.go
File metadata and controls
79 lines (60 loc) · 1.59 KB
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
package kcp
import "testing"
func TestBufferPoolGetSize(t *testing.T) {
bp := newBufferPool(mtuLimit)
buf := bp.Get()
// Check length
if len(buf) != mtuLimit {
t.Fatalf("expected len=%d, got %d", mtuLimit, len(buf))
return
}
// Check capacity
if cap(buf) != mtuLimit {
t.Fatalf("expected cap=%d, got %d", mtuLimit, cap(buf))
return
}
}
func TestBufferPoolPutAndReuse(t *testing.T) {
bp := newBufferPool(mtuLimit)
buf := bp.Get()
// Modify buffer to track it
buf[0] = 99
// Put back to pool
bp.Put(buf)
// Get again; it should reuse the same buffer
buf2 := bp.Get()
// Check if it is reused by comparing pointer address
if &buf2[0] != &buf[0] {
t.Fatalf("expected buffer reuse, but got a new one")
return
}
if buf2[0] != 99 {
t.Fatalf("expected reused buffer to keep previous data")
return
}
}
func TestBufferPoolPutWrongSizeIgnored(t *testing.T) {
bp := newBufferPool(mtuLimit)
// Make a buffer with wrong capacity
wrongBuf := make([]byte, 100)
bp.Put(wrongBuf)
// Get should still return a buffer with mtuLimit capacity
buf := bp.Get()
if cap(buf) != mtuLimit {
t.Fatalf("pool accepted wrong-sized buffer; expected cap=%d, got %d", mtuLimit, cap(buf))
return
}
}
func TestBufferPoolPutReturnsError(t *testing.T) {
bp := newBufferPool(mtuLimit)
// 1. Correct size
buf := make([]byte, mtuLimit)
if err := bp.Put(buf); err != nil {
t.Fatalf("expected nil error for correct size, got %v", err)
}
// 2. Incorrect size
wrongBuf := make([]byte, mtuLimit+1)
if err := bp.Put(wrongBuf); err == nil {
t.Fatalf("expected error for wrong size, got nil")
}
}