1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
11 "git.arvados.org/arvados.git/sdk/go/ctxlog"
15 var _ = Suite(&BufferPoolSuite{})
17 type BufferPoolSuite struct{}
19 // Initialize a default-sized buffer pool for the benefit of test
20 // suites that don't run main().
22 bufs = newBufferPool(ctxlog.FromContext(context.Background()), 12, BlockSize)
25 // Restore sane default after bufferpool's own tests
26 func (s *BufferPoolSuite) TearDownTest(c *C) {
27 bufs = newBufferPool(ctxlog.FromContext(context.Background()), 12, BlockSize)
30 func (s *BufferPoolSuite) TestBufferPoolBufSize(c *C) {
31 bufs := newBufferPool(ctxlog.TestLogger(c), 2, 10)
36 c.Check(len(b3), Equals, 3)
39 func (s *BufferPoolSuite) TestBufferPoolUnderLimit(c *C) {
40 bufs := newBufferPool(ctxlog.TestLogger(c), 3, 10)
43 testBufferPoolRace(c, bufs, b1, "Get")
46 func (s *BufferPoolSuite) TestBufferPoolAtLimit(c *C) {
47 bufs := newBufferPool(ctxlog.TestLogger(c), 2, 10)
50 testBufferPoolRace(c, bufs, b1, "Put")
53 func testBufferPoolRace(c *C, bufs *bufferPool, unused []byte, expectWin string) {
54 race := make(chan string)
57 time.Sleep(time.Millisecond)
61 time.Sleep(10 * time.Millisecond)
65 c.Check(<-race, Equals, expectWin)
66 c.Check(<-race, Not(Equals), expectWin)
70 func (s *BufferPoolSuite) TestBufferPoolReuse(c *C) {
71 bufs := newBufferPool(ctxlog.TestLogger(c), 2, 10)
74 // The buffer pool is allowed to throw away unused buffers
75 // (e.g., during sync.Pool's garbage collection hook, in the
76 // the current implementation). However, if unused buffers are
77 // getting thrown away and reallocated more than {arbitrary
78 // frequency threshold} during a busy loop, it's not acting
79 // much like a buffer pool.
82 for i := 0; i < allocs; i++ {
85 copy(last, []byte("last"))
86 copy(next, []byte("next"))
92 c.Check(reuses > allocs*95/100, Equals, true)