8 var _ = Suite(&BufferPoolSuite{})
10 type BufferPoolSuite struct{}
12 // Initialize a default-sized buffer pool for the benefit of test
13 // suites that don't run main().
15 bufs = newBufferPool(theConfig.MaxBuffers, BlockSize)
18 // Restore sane default after bufferpool's own tests
19 func (s *BufferPoolSuite) TearDownTest(c *C) {
20 bufs = newBufferPool(theConfig.MaxBuffers, BlockSize)
23 func (s *BufferPoolSuite) TestBufferPoolBufSize(c *C) {
24 bufs := newBufferPool(2, 10)
29 c.Check(len(b3), Equals, 3)
32 func (s *BufferPoolSuite) TestBufferPoolUnderLimit(c *C) {
33 bufs := newBufferPool(3, 10)
36 testBufferPoolRace(c, bufs, b1, "Get")
39 func (s *BufferPoolSuite) TestBufferPoolAtLimit(c *C) {
40 bufs := newBufferPool(2, 10)
43 testBufferPoolRace(c, bufs, b1, "Put")
46 func testBufferPoolRace(c *C, bufs *bufferPool, unused []byte, expectWin string) {
47 race := make(chan string)
50 time.Sleep(time.Millisecond)
54 time.Sleep(10 * time.Millisecond)
58 c.Check(<-race, Equals, expectWin)
59 c.Check(<-race, Not(Equals), expectWin)
63 func (s *BufferPoolSuite) TestBufferPoolReuse(c *C) {
64 bufs := newBufferPool(2, 10)
67 // The buffer pool is allowed to throw away unused buffers
68 // (e.g., during sync.Pool's garbage collection hook, in the
69 // the current implementation). However, if unused buffers are
70 // getting thrown away and reallocated more than {arbitrary
71 // frequency threshold} during a busy loop, it's not acting
72 // much like a buffer pool.
75 for i := 0; i < allocs; i++ {
78 copy(last, []byte("last"))
79 copy(next, []byte("next"))
85 c.Check(reuses > allocs*95/100, Equals, true)