This repository was archived by the owner on Nov 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgompool_test.go
More file actions
99 lines (72 loc) · 1.6 KB
/
gompool_test.go
File metadata and controls
99 lines (72 loc) · 1.6 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package gompool
import (
"testing"
"github.com/hlts2/gompool/treiber"
)
func TestNewGompool(t *testing.T) {
pool := NewGompool(12, func() interface{} {
return new(string)
})
if pool == nil {
t.Errorf("NewGompool is nil")
}
}
func TestGetAndPut(t *testing.T) {
var poolSize uint = 100
pool := NewGompool(poolSize, func() interface{} {
return make([]int, 10)
})
poolNodes := make([]*treiber.Node, 0, int(poolSize))
for i := 0; i < int(poolSize); i++ {
n := pool.Get()
if n == nil {
t.Errorf("Get is nil")
}
poolNodes = append(poolNodes, n)
}
expected := 0
got := pool.Cap()
if expected != got {
t.Errorf("Cap expected: %v, got: %v", expected, got)
}
for _, n := range poolNodes {
pool.Put(n)
}
expected = int(poolSize)
got = pool.Cap()
if expected != got {
t.Errorf("Cap expected: %v, got: %v", expected, got)
}
}
func TestCap(t *testing.T) {
var poolSize uint = 10
pool := NewGompool(poolSize, func() interface{} {
return new(int)
})
got := pool.Cap()
if got != int(poolSize) {
t.Errorf("Cap expected: %v, got: %v", poolSize, got)
}
_ = pool.Get()
got = pool.Cap()
if got != int(poolSize)-1 {
t.Errorf("Cap expected: %v, got: %v", int(poolSize)-1, got)
}
}
func TestDestPool(t *testing.T) {
var poolSize uint = 20
pool := NewGompool(poolSize, func() interface{} {
return make([]byte, 100)
})
expected := int(poolSize)
got := pool.Cap()
if expected != got {
t.Errorf("Cap expected: %v, got: %v", expected, got)
}
pool.DestPool()
expected = 0
got = pool.Cap()
if expected != got {
t.Errorf("Cap expected: %v, got: %v", expected, got)
}
}