bufferpool_test.go 873 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package iox
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestBufferPool(t *testing.T) {
  8. capacity := 1024
  9. pool := NewBufferPool(capacity)
  10. pool.Put(bytes.NewBuffer(make([]byte, 0, 2*capacity)))
  11. assert.True(t, pool.Get().Cap() <= capacity)
  12. }
  13. func TestBufferPool_Put(t *testing.T) {
  14. t.Run("with nil buf", func(t *testing.T) {
  15. pool := NewBufferPool(1024)
  16. pool.Put(nil)
  17. val := pool.Get()
  18. assert.IsType(t, new(bytes.Buffer), val)
  19. })
  20. t.Run("with less-cap buf", func(t *testing.T) {
  21. pool := NewBufferPool(1024)
  22. pool.Put(bytes.NewBuffer(make([]byte, 0, 512)))
  23. val := pool.Get()
  24. assert.IsType(t, new(bytes.Buffer), val)
  25. })
  26. t.Run("with more-cap buf", func(t *testing.T) {
  27. pool := NewBufferPool(1024)
  28. pool.Put(bytes.NewBuffer(make([]byte, 0, 1024<<1)))
  29. val := pool.Get()
  30. assert.IsType(t, new(bytes.Buffer), val)
  31. })
  32. }