1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
13 var _ = Suite(&BufferPoolSuite{})
15 type BufferPoolSuite struct{}
17 // Initialize a default-sized buffer pool for the benefit of test
18 // suites that don't run main().
20 bufs = newBufferPool(theConfig.MaxBuffers, BlockSize)
23 // Restore sane default after bufferpool's own tests
24 func (s *BufferPoolSuite) TearDownTest(c *C) {
25 bufs = newBufferPool(theConfig.MaxBuffers, BlockSize)
28 func (s *BufferPoolSuite) TestBufferPoolBufSize(c *C) {
29 bufs := newBufferPool(2, 10)
34 c.Check(len(b3), Equals, 3)
37 func (s *BufferPoolSuite) TestBufferPoolUnderLimit(c *C) {
38 bufs := newBufferPool(3, 10)
41 testBufferPoolRace(c, bufs, b1, "Get")
44 func (s *BufferPoolSuite) TestBufferPoolAtLimit(c *C) {
45 bufs := newBufferPool(2, 10)
48 testBufferPoolRace(c, bufs, b1, "Put")
51 func testBufferPoolRace(c *C, bufs *bufferPool, unused []byte, expectWin string) {
52 race := make(chan string)
55 time.Sleep(time.Millisecond)
59 time.Sleep(10 * time.Millisecond)
63 c.Check(<-race, Equals, expectWin)
64 c.Check(<-race, Not(Equals), expectWin)
68 func (s *BufferPoolSuite) TestBufferPoolReuse(c *C) {
69 bufs := newBufferPool(2, 10)
72 // The buffer pool is allowed to throw away unused buffers
73 // (e.g., during sync.Pool's garbage collection hook, in the
74 // the current implementation). However, if unused buffers are
75 // getting thrown away and reallocated more than {arbitrary
76 // frequency threshold} during a busy loop, it's not acting
77 // much like a buffer pool.
80 for i := 0; i < allocs; i++ {
83 copy(last, []byte("last"))
84 copy(next, []byte("next"))
90 c.Check(reuses > allocs*95/100, Equals, true)