-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpool_test.go
47 lines (35 loc) · 1.28 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
package bytepool
import (
. "gopkg.in/check.v1"
"reflect"
"testing"
)
type TestSuite struct{}
var _ = Suite(&TestSuite{})
func Test(t *testing.T) { TestingT(t) }
func (s *TestSuite) TestPoolEachItemIsOfASpecifiedSize(c *C) {
p := New(1, 9)
item := p.Checkout()
defer item.Close()
c.Assert(cap(item.bytes), Equals, 9, Commentf("expecting array to have a capacity of %d, got %d", 9, cap(item.bytes)))
}
func (s *TestSuite) TestPoolDynamicallyCreatesAnItemWhenPoolIsEmpty(c *C) {
p := New(1, 2)
item1 := p.Checkout()
item2 := p.Checkout()
c.Assert(cap(item2.bytes), Equals, 2, Commentf("Dynamically created item was not properly initialized"))
c.Assert(item2.pool, IsNil, Commentf("The dynamically created item should have a nil pool"))
item1.Close()
item2.Close()
c.Assert(p.Len(), Equals, 1, Commentf("Expecting a pool length of 1, got %d", p.Len()))
c.Assert(p.Misses(), Equals, 1, Commentf("Expecting a miss count of 1, got %d", p.Misses()))
}
func (s *TestSuite) TestPoolReleasesAnItemBackIntoThePool(c *C) {
p := New(1, 20)
item1 := p.Checkout()
pointer := reflect.ValueOf(item1).Pointer()
item1.Close()
item2 := p.Checkout()
defer item2.Close()
c.Assert(reflect.ValueOf(item2).Pointer(), Equals, pointer, Commentf("Pool returned an unexpected item"))
}