-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool_test.go
53 lines (43 loc) · 1022 Bytes
/
pool_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
package just_test
import (
"github.com/kazhuravlev/just"
"github.com/stretchr/testify/assert"
"testing"
)
func TestPool(t *testing.T) {
t.Run("with_reset", func(t *testing.T) {
var isCalled bool
p := just.NewPool(
func() *[]byte { return just.Pointer(make([]byte, 8)) },
func(b *[]byte) { isCalled = true },
)
assert.False(t, isCalled)
bb := p.Get()
assert.Equal(t, 8, len(*bb))
assert.False(t, isCalled)
p.Put(bb)
assert.True(t, isCalled)
})
t.Run("without_reset", func(t *testing.T) {
p := just.NewPool(
func() *[]byte { return just.Pointer(make([]byte, 8)) },
nil,
)
bb := p.Get()
assert.Equal(t, 8, len(*bb))
p.Put(bb)
})
}
func BenchmarkPoolReset(b *testing.B) {
// BenchmarkPoolReset-8 130386861 8.977 ns/op 0 B/op 0 allocs/op
p := just.NewPool(
func() *[]byte { return just.Pointer(make([]byte, 0)) },
func(b *[]byte) { *b = (*b)[:0] },
)
b.ResetTimer()
for i := 0; i < b.N; i++ {
bb := p.Get()
_ = bb
p.Put(bb)
}
}