10 func TestBufferPool(t *testing.T) {
14 var _ = Suite(&BufferPoolSuite{})
16 type BufferPoolSuite struct{}
18 // Initialize a default-sized buffer pool for the benefit of test
19 // suites that don't run main().
21 bufs = newBufferPool(maxBuffers, BLOCKSIZE)
24 func (s *BufferPoolSuite) TestBufferPoolBufSize(c *C) {
25 bufs := newBufferPool(2, 10)
30 c.Check(len(b3), Equals, 3)
33 func (s *BufferPoolSuite) TestBufferPoolUnderLimit(c *C) {
34 bufs := newBufferPool(3, 10)
37 testBufferPoolRace(c, bufs, b1, "Get")
40 func (s *BufferPoolSuite) TestBufferPoolAtLimit(c *C) {
41 bufs := newBufferPool(2, 10)
44 testBufferPoolRace(c, bufs, b1, "Put")
47 func testBufferPoolRace(c *C, bufs *bufferPool, unused []byte, expectWin string) {
48 race := make(chan string)
51 time.Sleep(time.Millisecond)
55 time.Sleep(10 * time.Millisecond)
59 c.Check(<-race, Equals, expectWin)
60 c.Check(<-race, Not(Equals), expectWin)
64 func (s *BufferPoolSuite) TestBufferPoolReuse(c *C) {
65 bufs := newBufferPool(2, 10)
68 // The buffer pool is allowed to throw away unused buffers
69 // (e.g., during sync.Pool's garbage collection hook, in the
70 // the current implementation). However, if unused buffers are
71 // getting thrown away and reallocated more than {arbitrary
72 // frequency threshold} during a busy loop, it's not acting
73 // much like a buffer pool.
76 for i := 0; i < allocs; i++ {
79 copy(last, []byte("last"))
80 copy(next, []byte("next"))
86 c.Check(reuses > allocs*95/100, Equals, true)