mirror of
https://github.com/hanzoai/search-go.git
synced 2026-10-30 18:22:58 +00:00
Modules declare go 1.27.1 and the build images follow. go fix applied the modernizers, and the test and example pointer helpers went once new(expr) could say them; omitzero is not applied, since it changes what the JSON says. Co-authored-by: Hanzo Dev <[email protected]>
56 lines
993 B
Go
56 lines
993 B
Go
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() any {
|
|
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() any {
|
|
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")
|
|
}
|