-
Notifications
You must be signed in to change notification settings - Fork 3
/
pool_test.go
50 lines (43 loc) · 1.08 KB
/
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
// Pool is no-op under race detector, so all these tests do not work.
//go:build !race
package astjson
import (
"fmt"
"sync"
"testing"
)
func TestParserPool(t *testing.T) {
var pp ParserPool
for i := 0; i < 10; i++ {
p := pp.Get()
if _, err := p.Parse("null"); err != nil {
t.Fatalf("cannot parse null: %s", err)
}
pp.Put(p)
}
}
func TestParserPoolMaxSize(t *testing.T) {
var numNew, numNewLimit int
ppr := &ParserPool{
sync.Pool{New: func() interface{} { numNew++; return new(Parser) }},
}
pprLimit := &ParserPool{
sync.Pool{New: func() interface{} { numNewLimit++; return new(Parser) }},
}
parse := func(ppr *ParserPool, maxSize int, index int) {
var json = fmt.Sprintf(`{"%d":"test"}`, index)
pr := ppr.Get()
_, _ = pr.Parse(json)
ppr.PutIfSizeLessThan(pr, maxSize)
}
for i := 0; i < 10; i++ {
parse(ppr, 0, i)
parse(pprLimit, 1, i)
}
if numNew != 1 {
t.Fatalf("Expected exactly 1 calls to Pool New with no Max Size (not %d)", numNew)
}
if numNewLimit != 10 {
t.Fatalf("Expected exactly 10 calls to Pool with a Max Size (not %d)", numNewLimit)
}
}