// Copyright (c) Tailscale Inc & contributors // SPDX-License-Identifier: BSD-3-Clause package bufpool import ( "bytes" "crypto/rand" "io" "slices" "testing" "testing/iotest" qt "github.com/frankban/quicktest" "github.com/google/go-cmp/cmp/cmpopts" "golang.org/x/sync/errgroup" "tailscale.com/util/must" ) // Reduce the range of pool levels for testing purposes. func init() { minBits = 8 maxBits = 16 } func Test(t *testing.T) { c := qt.New(t) const maxSize = 1 << 20 p := NewPool() p.SetMaxSize(maxSize) c.Run("NoCapacity", func(c *qt.C) { _, err := p.Get(1 + maxSize) c.Assert(err, qt.CmpEquals(cmpopts.EquateErrors()), cmpopts.AnyError) c.Assert(p.counterUsedBytes.Value(), qt.Equals, int64(0)) c.Assert(p.counterUsedBuffers.Value(), qt.Equals, int64(0)) }) c.Run("BackToBack", func(c *qt.C) { var usedBytes int64 sizes := []int{0, 3, 184, 254, 255, 256, 257, 1381, 1023, 1024, 1025, 30105, 30105, 65534, 65535, 65536, 65537, 184823, 184823} func() { for _, n := range sizes { b := must.Get(p.Get(n)) defer p.Put(b) if n < 1<