forked from viki-org/bytepool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonpool_test.go
More file actions
50 lines (44 loc) · 1.17 KB
/
jsonpool_test.go
File metadata and controls
50 lines (44 loc) · 1.17 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
package bytepool
import (
"reflect"
"testing"
)
func TestJsonPoolEachItemIsOfASpecifiedSize(t *testing.T) {
expected := 9
p := New(1, expected)
item := p.Checkout()
defer item.Close()
if cap(item.bytes) != expected {
t.Errorf("expecting array to have a capacity of %d, got %d", expected, cap(item.bytes))
}
}
func TestJsonPoolDynamicallyCreatesAnItemWhenPoolIsEmpty(t *testing.T) {
p := New(1,2)
item1 := p.Checkout()
item2 := p.Checkout()
if cap(item2.bytes) != 2 {
t.Error("Dynamically created item was not properly initialized")
}
if item2.pool != nil {
t.Error("The dynamically created item should have a nil pool")
}
item1.Close()
item2.Close()
if p.Len() != 1 {
t.Errorf("Expecting a pool lenght of 1, got %d", p.Len())
}
if p.Misses() != 1 {
t.Errorf("Expecting a miss count of 1, got %d", p.Misses())
}
}
func TestJsonPoolReleasesAnItemBackIntoThePool(t *testing.T) {
p := New(1, 20)
item1 := p.Checkout()
pointer := reflect.ValueOf(item1).Pointer()
item1.Close()
item2 := p.Checkout()
defer item2.Close()
if reflect.ValueOf(item2).Pointer() != pointer {
t.Error("Pool returned an unexected item")
}
}