1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
12 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(theConfig.MaxBuffers, BlockSize)
22 // Restore sane default after bufferpool's own tests
23 func (s *BufferPoolSuite) TearDownTest(c *C) {
24 bufs = newBufferPool(theConfig.MaxBuffers, BlockSize)
27 func (s *BufferPoolSuite) TestBufferPoolBufSize(c *C) {
28 bufs := newBufferPool(2, 10)
33 c.Check(len(b3), Equals, 3)
36 func (s *BufferPoolSuite) TestBufferPoolUnderLimit(c *C) {
37 bufs := newBufferPool(3, 10)
40 testBufferPoolRace(c, bufs, b1, "Get")
43 func (s *BufferPoolSuite) TestBufferPoolAtLimit(c *C) {
44 bufs := newBufferPool(2, 10)
47 testBufferPoolRace(c, bufs, b1, "Put")
50 func testBufferPoolRace(c *C, bufs *bufferPool, unused []byte, expectWin string) {
51 race := make(chan string)
54 time.Sleep(time.Millisecond)
58 time.Sleep(10 * time.Millisecond)
62 c.Check(<-race, Equals, expectWin)
63 c.Check(<-race, Not(Equals), expectWin)
67 func (s *BufferPoolSuite) TestBufferPoolReuse(c *C) {
68 bufs := newBufferPool(2, 10)
71 // The buffer pool is allowed to throw away unused buffers
72 // (e.g., during sync.Pool's garbage collection hook, in the
73 // the current implementation). However, if unused buffers are
74 // getting thrown away and reallocated more than {arbitrary
75 // frequency threshold} during a busy loop, it's not acting
76 // much like a buffer pool.
79 for i := 0; i < allocs; i++ {
82 copy(last, []byte("last"))
83 copy(next, []byte("next"))
89 c.Check(reuses > allocs*95/100, Equals, true)