Skip to content

Commit

Permalink
Move pool tests into their own test and disable for race
Browse files Browse the repository at this point in the history
  • Loading branch information
jfroundjian committed Sep 10, 2024
1 parent b80f64a commit ce0af59
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 41 deletions.
41 changes: 0 additions & 41 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"math"
"strings"
"sync"
"testing"
"time"
)
Expand Down Expand Up @@ -178,46 +177,6 @@ func TestParseRawString(t *testing.T) {
})
}

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)
}
}

func TestValueInvalidTypeConversion(t *testing.T) {
var p Parser

Expand Down
52 changes: 52 additions & 0 deletions pool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//go:build !race

// 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)
}
}

0 comments on commit ce0af59

Please sign in to comment.