11 "github.com/mjl-/mox/mlog"
14func TestBufpool(t *testing.T) {
15 bp := NewBufpool(1, 8)
18 for i := 0; i < len(a); i++ {
21 log := mlog.New("moxio", nil)
22 bp.put(log, a, len(a)) // Will be stored.
23 bp.put(log, b, 0) // Will be discarded.
25 if fmt.Sprintf("%p", a) != fmt.Sprintf("%p", na) {
26 t.Fatalf("received unexpected new buf %p != %p", a, na)
28 for _, c := range na {
30 t.Fatalf("reused buf not cleared")
34 if _, err := bp.Readline(log, bufio.NewReader(strings.NewReader("this is too long"))); !errors.Is(err, ErrLineTooLong) {
35 t.Fatalf("expected ErrLineTooLong, got error %v", err)
37 if _, err := bp.Readline(log, bufio.NewReader(strings.NewReader("short"))); !errors.Is(err, io.ErrUnexpectedEOF) {
38 t.Fatalf("expected ErrLineTooLong, got error %v", err)
41 er := errReader{fmt.Errorf("bad")}
42 if _, err := bp.Readline(log, bufio.NewReader(er)); err == nil || !errors.Is(err, er.err) {
43 t.Fatalf("got unexpected error %s", err)
46 if line, err := bp.Readline(log, bufio.NewReader(strings.NewReader("ok\r\n"))); line != "ok" {
47 t.Fatalf(`got %q, err %v, expected line "ok"`, line, err)
49 if line, err := bp.Readline(log, bufio.NewReader(strings.NewReader("ok\n"))); line != "ok" {
50 t.Fatalf(`got %q, err %v, expected line "ok"`, line, err)
54type errReader struct {
58func (r errReader) Read(buf []byte) (int, error) {