This repository has been archived by the owner on May 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bufferpool_test.go
197 lines (163 loc) · 3.35 KB
/
bufferpool_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
package bufferpool
import (
"bytes"
"strings"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_makePartitions_1(t *testing.T) {
assert := assert.New(t)
pool, back := makePartitions(10, 10)
assert.Equal(100, len(back))
assert.Equal(10, len(pool))
for _, v := range pool {
assert.Equal(10, len(v))
}
copy(back, []byte(strings.Repeat("-", 200)))
assert.Equal(100, len(back))
for _, v := range back {
assert.Equal("-"[0], v)
}
for _, vp := range pool {
for _, v := range vp {
assert.Equal("-"[0], v)
}
}
for kp := range pool {
pool[kp] = append(pool[kp][:0], []byte(strings.Repeat("*", 10))...)
}
for _, v := range back {
assert.Equal("*"[0], v)
}
for kp := range pool {
pool[kp] = append(pool[kp][:0], []byte(strings.Repeat("-", 11))...)
}
for _, v := range back {
assert.Equal("*"[0], v)
}
}
func Test_makePartitions_2(t *testing.T) {
assert := assert.New(t)
pool, back := makePartitions(10, 10)
assert.Equal(100, len(back))
assert.Equal(10, len(pool))
for _, v := range pool {
assert.Equal(10, len(v))
}
copy(back, []byte(strings.Repeat("-", 200)))
assert.Equal(100, len(back))
for _, v := range back {
assert.Equal("-"[0], v)
}
for _, vp := range pool {
for _, v := range vp {
assert.Equal("-"[0], v)
}
}
for kp := range pool {
copy(pool[kp], []byte(strings.Repeat(string(kp), 200)))
}
for _, v := range pool {
assert.Equal(10, len(v))
}
for k, v := range pool {
check := []byte(strings.Repeat(string(k), 10))
assert.Equal(0, bytes.Compare(v, check))
}
for k, v := range back {
assert.Equal([]byte(string(k / 10))[0], v)
}
for _, vp := range pool {
copy(vp, []byte(strings.Repeat("-", 10)))
}
for _, v := range back {
assert.Equal("-"[0], v)
}
}
func Test_Smoke_BufferPool(t *testing.T) {
assert := assert.New(t)
pool := New(10, 10)
b := pool.Take()
assert.NotNil(b)
assert.Equal(9, pool.Len())
assert.True(pool.Put(b))
assert.Equal(10, pool.Len())
pool.Expand(10)
assert.Equal(20, pool.Len())
var (
next = true
pile [][]byte
)
for next {
b := pool.Take()
if b == nil {
next = false
continue
}
pile = append(pile, b)
}
big := make([]byte, 100)
assert.False(pool.Put(big))
for _, v := range pile {
pool.Put(v)
}
big = make([]byte, 10)
assert.False(pool.Put(big))
pool.Take()
big = make([]byte, 100)
assert.False(pool.Put(big))
}
func Test_BufferPool_Concurrent(t *testing.T) {
assert := assert.New(t)
// run with go test -race
pool := New(10, 1000)
start := make(chan struct{})
var done sync.WaitGroup
var step1 sync.WaitGroup
for i := 0; i < 1000; i++ {
done.Add(1)
step1.Add(1)
go func() {
defer done.Done()
buffer := pool.Take()
step1.Done()
defer pool.Put(buffer)
<-start
for k := range buffer {
buffer[k] = byte(k & 0xFF)
}
}()
}
step1.Wait()
assert.Equal(0, pool.Len())
close(start)
done.Wait()
}
func ExampleBufferPool() {
pool := New(10, 1000)
func() {
buffer := pool.Take()
defer pool.Put(buffer)
}()
}
func ExampleBufferPool_concurrent() {
// run with go test -race
pool := New(10, 1000)
start := make(chan struct{})
var done sync.WaitGroup
for i := 0; i < 1000; i++ {
done.Add(1)
go func() {
defer done.Done()
<-start
buffer := pool.Take()
defer pool.Put(buffer)
for k := range buffer {
buffer[k] = byte(k & 0xFF)
}
}()
}
close(start)
done.Wait()
}