Files
magpie/internal/agent/statstamp_test.go
T
yetone da8480326f agent: detection's remaining stats make no os.FileInfo on darwin and linux
After statKind (e3ab7f3bf), os.Stat was still a fifth of what forty
/api/drift asks allocated with real accounts: Agent.Detected's look at
the agent's file, bundleInfo's and folderSubs' size-and-time stamps for
every desktop app, and qoderApp's status files. Each os.Stat makes an
os.FileInfo on the heap. They now go through the system call into a
Stat_t on the stack:
- Detected: statKind (exists, followed through links, as os.Stat).
- qoderApp: statFile, a regular file as Mode().IsRegular() says.
- bundleInfo, folderSubs: statStamp, mtime and size as ModTime/Size
  say (Mtimespec on darwin, Mtim on linux; os.Stat elsewhere).
Windows and other systems keep os.Stat.

Run (at this tree):
- mote: go test -count=1 ./internal/agent ok (518s, load high after a
  reboot); go vet darwin; linux and windows vet clean
- wsl-run (Linux): TestStatStamp|TestStatKind|TestQoder ok
- win-run (Windows): same set; TestQoderSettingsFromAnIDEPluginIsNotTheCLI
  red, and red the same on origin/main 783cd0f84 (#1827: the fixture is
  an extensionless #!/bin/sh on PATH); the rest ok
- TestStatStampAndFileSayWhatStatSays compares with os.Stat for a file,
  a folder, a link, a gone path and a path under a file; with darwin's
  statStamp put back on os.Stat by hand it is red (3 allocations for
  two looks), restored
- Real accounts (sandbox HOME copy), 40 /api/drift calls, paired runs:
  os.Stat 2.0/3.1 MB before, 1.5/0 MB after (left: pencilApp, a glob,
  provider.stampOf); totals 20.2/19.1 MB before, 18.5/17.5 MB after.
2026-11-10 06:46:50 +08:00

44 lines
1.3 KiB
Go

package agent
import (
"os"
"path/filepath"
"runtime"
"testing"
"time"
)
// statStamp and statFile say what os.Stat would, for a file, a folder, a
// link and a gone path, and make no os.FileInfo for it on unix.
func TestStatStampAndFileSayWhatStatSays(t *testing.T) {
dir := t.TempDir()
file := filepath.Join(dir, "Info.plist")
if err := os.WriteFile(file, []byte("<plist/>"), 0o600); err != nil {
t.Fatal(err)
}
at := time.Date(2026, 10, 1, 12, 0, 0, 123456789, time.UTC)
os.Chtimes(file, at, at)
link := filepath.Join(dir, "link")
os.Symlink(file, link)
for _, p := range []string{file, dir, link, filepath.Join(dir, "gone"), filepath.Join(file, "under")} {
mod, size, ok := statStamp(p)
st, err := os.Stat(p)
if ok != (err == nil) {
t.Fatalf("%s: ok %v, os.Stat err %v", p, ok, err)
}
if err == nil && (!mod.Equal(st.ModTime()) || size != st.Size()) {
t.Fatalf("%s: %v %d, os.Stat %v %d", p, mod, size, st.ModTime(), st.Size())
}
if want := err == nil && st.Mode().IsRegular(); statFile(p) != want {
t.Fatalf("%s: statFile %v, want %v", p, !want, want)
}
}
if runtime.GOOS != "darwin" && runtime.GOOS != "linux" {
return
}
// the path's bytes for the system call are all a look allocates
if n := testing.AllocsPerRun(20, func() { statStamp(file); statFile(file) }); n > 2 {
t.Fatalf("%v allocations for two looks", n)
}
}