mirror of
https://github.com/mjl-/mox.git
synced 2024-12-27 00:43:48 +03:00
5b20cba50a
we don't want external software to include internal details like mlog. slog.Logger is/will be the standard. we still have mlog for its helper functions, and its handler that logs in concise logfmt used by mox. packages that are not meant for reuse still pass around mlog.Log for convenience. we use golang.org/x/exp/slog because we also support the previous Go toolchain version. with the next Go release, we'll switch to the builtin slog.
60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
package moxio
|
|
|
|
import (
|
|
"bufio"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/mjl-/mox/mlog"
|
|
)
|
|
|
|
func TestBufpool(t *testing.T) {
|
|
bp := NewBufpool(1, 8)
|
|
a := bp.get()
|
|
b := bp.get()
|
|
for i := 0; i < len(a); i++ {
|
|
a[i] = 1
|
|
}
|
|
log := mlog.New("moxio", nil)
|
|
bp.put(log, a, len(a)) // Will be stored.
|
|
bp.put(log, b, 0) // Will be discarded.
|
|
na := bp.get()
|
|
if fmt.Sprintf("%p", a) != fmt.Sprintf("%p", na) {
|
|
t.Fatalf("received unexpected new buf %p != %p", a, na)
|
|
}
|
|
for _, c := range na {
|
|
if c != 0 {
|
|
t.Fatalf("reused buf not cleared")
|
|
}
|
|
}
|
|
|
|
if _, err := bp.Readline(log, bufio.NewReader(strings.NewReader("this is too long"))); !errors.Is(err, ErrLineTooLong) {
|
|
t.Fatalf("expected ErrLineTooLong, got error %v", err)
|
|
}
|
|
if _, err := bp.Readline(log, bufio.NewReader(strings.NewReader("short"))); !errors.Is(err, io.ErrUnexpectedEOF) {
|
|
t.Fatalf("expected ErrLineTooLong, got error %v", err)
|
|
}
|
|
|
|
er := errReader{fmt.Errorf("bad")}
|
|
if _, err := bp.Readline(log, bufio.NewReader(er)); err == nil || !errors.Is(err, er.err) {
|
|
t.Fatalf("got unexpected error %s", err)
|
|
}
|
|
|
|
if line, err := bp.Readline(log, bufio.NewReader(strings.NewReader("ok\r\n"))); line != "ok" {
|
|
t.Fatalf(`got %q, err %v, expected line "ok"`, line, err)
|
|
}
|
|
if line, err := bp.Readline(log, bufio.NewReader(strings.NewReader("ok\n"))); line != "ok" {
|
|
t.Fatalf(`got %q, err %v, expected line "ok"`, line, err)
|
|
}
|
|
}
|
|
|
|
type errReader struct {
|
|
err error
|
|
}
|
|
|
|
func (r errReader) Read(buf []byte) (int, error) {
|
|
return 0, r.err
|
|
}
|