Files
magpie/internal/proc/rss_test.go
T
yetoneandskygunner cec54c1d01 proc: main's two reds, the TUI's serve start through proc.Command (#3567) and an RSS read before the loader ran
Test (ubuntu, macos) on main has been red since 75e78a5a0 on two tests.

TestNoCommandBypassesProc: internal/tui/keep.go's serveInBackground
started `magpie serve` with os/exec. It is proc.Command now, the fix
skygunner gave on #3567 (64bd29fa on their fork). Off Windows
proc.Command is exec.Command, and this path runs on Linux only, so
nothing else changes.

TestTreeRSS (ubuntu only): the test read sleep's VmRSS right after
StartTree and wanted at least 64 KiB. Just after its exec a command
holds a few pages until its loader maps libc: CI read 16384 bytes. The
test now reads again until it holds 64 KiB, for up to 10s. Tree.RSS
itself is unchanged.

Run at this head:
- golang:1.26 docker (Linux, CI's Go): TestTreeRSS -count=50,
  old test red 13 of 50 ("a running sleep holds ..."), new 50 of 50 ok.
- macOS: go test -count=3 ./internal/proc ./internal/tui ok (the old
  keep.go is the guard's red on CI's runs 40199986992 and earlier).
- GOOS=linux and GOOS=windows go vet -tags nogui on both packages,
  gofmt clean.
Not run: the full suite.

Co-authored-by: skygunner <skygunner@users.noreply.github.com>
2026-10-25 23:15:45 +08:00

43 lines
1.2 KiB
Go

package proc
import (
"os/exec"
"runtime"
"testing"
"time"
)
// A running tree's resident memory is read, and none once it was waited
// for: its pid may be another process's by then.
func TestTreeRSS(t *testing.T) {
name, args := "sleep", []string{"30"}
if runtime.GOOS == "windows" {
name, args = "ping", []string{"-n", "30", "127.0.0.1"}
}
if _, err := exec.LookPath(name); err != nil {
t.Skip("no " + name)
}
tree, err := StartTree(exec.Command(name, args...))
if err != nil {
t.Fatal(err)
}
// just after its exec a command holds a few pages, until its loader
// maps what it runs on (16KiB on ubuntu CI), so it is read until it does
rss, ok := tree.RSS()
for end := time.Now().Add(10 * time.Second); ok && rss < 64<<10 && time.Now().Before(end); {
time.Sleep(20 * time.Millisecond)
rss, ok = tree.RSS()
}
if !ok || rss < 64<<10 || rss > 1<<30 {
t.Fatalf("a running %s holds %d bytes (%v)", name, rss, ok)
}
tree.Kill()
_ = tree.Wait()
if _, ok := tree.RSS(); ok {
t.Fatal("a tree waited for was read")
}
if MemTotal() <= 0 && (runtime.GOOS == "darwin" || runtime.GOOS == "linux" || runtime.GOOS == "windows") {
t.Fatal("this computer's memory isn't known")
}
}