1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
package meilisearch
import (
"bytes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sync"
"testing"
)
func TestPooledBuffer_Read(t *testing.T) {
pool := &sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
buf := pool.Get().(*bytes.Buffer)
buf.WriteString("hello world")
pb := &pooledBuffer{
Buffer: buf,
pool: pool,
}
readBuf := make([]byte, 5)
n, err := pb.Read(readBuf)
require.NoError(t, err)
assert.Equal(t, 5, n)
assert.Equal(t, "hello", string(readBuf[:n]))
}
func TestPooledBuffer_Close(t *testing.T) {
pool := &sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
buf := pool.Get().(*bytes.Buffer)
buf.WriteString("data to reset")
pb := &pooledBuffer{
Buffer: buf,
pool: pool,
}
err := pb.Close()
require.NoError(t, err)
got := pool.Get().(*bytes.Buffer)
got.WriteString("new data")
assert.Equal(t, "new data", got.String(), "buffer should be reusable and empty after close")
}
|