Files
got/bt/bt_test.go
Hojun-Cho bb49578892 bt: filter anacrolix logs to errors so warnings don't corrupt the readout
webseed/peer/tracker warnings (the 403/429 chatter) go through slog, the
rest through the legacy analog logger; both default to Warning, which
interleaves with and corrupts the live progress block. Filter both to
Error and above so only genuine errors reach stderr. quietSlogger is a
small testable helper; anacrolix/log becomes a direct dependency.
2026-06-20 21:11:57 +09:00

76 lines
1.9 KiB
Go

package bt
import (
"bytes"
"context"
"log/slog"
"reflect"
"strings"
"testing"
)
// TestQuietSloggerDropsWarnings: the library's warning chatter (the webseed
// 403/429 noise) is filtered out so it can't corrupt the live display, while a
// genuine error still gets through.
func TestQuietSloggerDropsWarnings(t *testing.T) {
var buf bytes.Buffer
lg := quietSlogger(&buf)
lg.Warn("webseed request error", "err", "429 Too Many Requests")
if buf.Len() != 0 {
t.Errorf("warning leaked into output: %q", buf.String())
}
// anacrolix's webseed code logs via Log(ctx, level, ...); confirm that path is
// filtered too, not just the Warn helper.
lg.Log(context.Background(), slog.LevelWarn, "another warning")
if buf.Len() != 0 {
t.Errorf("Log-at-Warn leaked: %q", buf.String())
}
lg.Error("real failure")
if !strings.Contains(buf.String(), "real failure") {
t.Errorf("error was suppressed, want it kept: %q", buf.String())
}
}
func TestParsePorts(t *testing.T) {
tests := []struct {
spec string
want []int
}{
// port spec examples.
{"6881,6885", []int{6881, 6885}},
{"6881-6999", rangeInts(6881, 6999)},
{"7000-7001,8000", []int{7000, 7001, 8000}},
{"6881-6889,6999", append(rangeInts(6881, 6889), 6999)},
// single bare port.
{"6881", []int{6881}},
// whitespace and empty fields are tolerated.
{" 6881 , 6885 ", []int{6881, 6885}},
{"6881,,6885", []int{6881, 6885}},
// empty / all-bad specs yield nil.
{"", nil},
{",", nil},
{"abc", nil},
// out-of-range ports are dropped.
{"0", nil},
{"70000", nil},
// a range that overruns 65535 keeps only the valid part.
{"65534-65540", []int{65534, 65535}},
}
for _, tt := range tests {
got := ParsePorts(tt.spec)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ParsePorts(%q) = %v, want %v", tt.spec, got, tt.want)
}
}
}
func rangeInts(a, b int) []int {
out := make([]int, 0, b-a+1)
for i := a; i <= b; i++ {
out = append(out, i)
}
return out
}