10 func TestBufferPool(t *testing.T) {
13 var _ = Suite(&BufferPoolSuite{})
14 type BufferPoolSuite struct {}
16 // Initialize a default-sized buffer pool for the benefit of test
17 // suites that don't run main().
19 bufs = newBufferPool(maxBuffers, BLOCKSIZE)
22 func (s *BufferPoolSuite) TestBufferPoolBufSize(c *C) {
23 bufs := newBufferPool(2, 10)
28 c.Check(len(b3), Equals, 3)
31 func (s *BufferPoolSuite) TestBufferPoolUnderLimit(c *C) {
32 bufs := newBufferPool(3, 10)
35 testBufferPoolRace(c, bufs, b1, "Get")
38 func (s *BufferPoolSuite) TestBufferPoolAtLimit(c *C) {
39 bufs := newBufferPool(2, 10)
42 testBufferPoolRace(c, bufs, b1, "Put")
45 func testBufferPoolRace(c *C, bufs *bufferPool, unused []byte, expectWin string) {
46 race := make(chan string)
49 time.Sleep(time.Millisecond)
53 time.Sleep(10*time.Millisecond)
57 c.Check(<-race, Equals, expectWin)
58 c.Check(<-race, Not(Equals), expectWin)
62 func (s *BufferPoolSuite) TestBufferPoolReuse(c *C) {
63 bufs := newBufferPool(2, 10)
66 // The buffer pool is allowed to throw away unused buffers
67 // (e.g., during sync.Pool's garbage collection hook, in the
68 // the current implementation). However, if unused buffers are
69 // getting thrown away and reallocated more than {arbitrary
70 // frequency threshold} during a busy loop, it's not acting
71 // much like a buffer pool.
74 for i := 0; i < allocs; i++ {
77 copy(last, []byte("last"))
78 copy(next, []byte("next"))
84 c.Check(reuses > allocs * 95/100, Equals, true)