Files
magpie/internal/dirent/each_test.go
T
yetone 328dc97e28 dirent: Each lists a folder with nothing made an entry; Codex's idle check uses it
b5adaa8bd read Codex's sessions folders with dirent.Stats, and measured
on a real-account HOME it allocated about as much as the walk it
replaced (dirent.entry and nameOf 4.1 MB of 12.7 MB in 4 idle minutes):
Stats copies every name and builds a listing, for a check that keeps
none of it.

dirent.Each gives a callback each entry's name, in getattrlistbulk's
buffer, and its Stat, and stops when told; stats is now built on the
same reader. codexSessionsQuietListed makes a path for each folder and
nothing for a file.

Run:
- mote: go test -count=1 ./internal/dirent ./internal/sessions ok;
  ./internal/provider ok (141s); GOOS=windows and GOOS=linux -tags nogui
  go vet ./internal/dirent ./internal/provider ok; gofmt clean
- xiaomi-gpu (Windows): -run 'TestEach|TestCodexSessionsQuiet' ok
Broken by hand: Each copying each name (string(name)), and
TestEachIsLstatWithNothingMade is red with 302 allocations for 301
entries.
2026-11-11 08:33:52 +08:00

60 lines
1.7 KiB
Go

package dirent
import (
"os"
"path/filepath"
"runtime"
"testing"
)
// Each gives every entry of a folder as os.Lstat has it, stops when told,
// and makes nothing for an entry: Codex's idle check reads every session
// folder with it on each look.
func TestEachIsLstatWithNothingMade(t *testing.T) {
dir := t.TempDir()
for i := range 300 {
p := filepath.Join(dir, "rollout-"+string(rune('a'+i%26))+string(rune('a'+i/26))+".jsonl")
if err := os.WriteFile(p, make([]byte, i), 0o644); err != nil {
t.Fatal(err)
}
}
if err := os.Mkdir(filepath.Join(dir, "sub"), 0o755); err != nil {
t.Fatal(err)
}
seen := 0
ok := Each(dir, func(name string, s Stat) bool {
seen++
fi, err := os.Lstat(filepath.Join(dir, name))
if err != nil {
t.Fatalf("%s: %v", name, err)
}
if s.Size != fi.Size() && !fi.IsDir() || s.Mod != fi.ModTime().UnixNano() || fi.IsDir() != (s.Mode == os.ModeDir) {
t.Fatalf("%s: %+v, lstat %d %v %v", name, s, fi.Size(), fi.ModTime(), fi.Mode())
}
return true
})
if runtime.GOOS != "darwin" {
if ok {
t.Fatal("a listing where the system has none")
}
return
}
if !ok || seen != 301 {
t.Fatalf("ok %v, %d entries, want 301", ok, seen)
}
n := 0
if !Each(dir, func(string, Stat) bool { n++; return n < 3 }) || n != 3 {
t.Fatalf("told to stop at the third, gave %d", n)
}
var mods int64
allocs := testing.AllocsPerRun(20, func() {
Each(dir, func(name string, s Stat) bool { mods += s.Mod + int64(len(name)); return true })
})
if allocs > 4 {
t.Fatalf("%v allocations a listing of 301 entries; want none a entry", allocs)
}
if Each(filepath.Join(dir, "gone"), func(string, Stat) bool { return true }) {
t.Fatal("a folder that isn't there listed")
}
}