Files
magpie/internal/steady/steady_test.go
T
yetone 8173854168 steady: a file whose folder isn't there is said gone without the wait (#1665)
On Windows, Stat and ReadThere wait up to 15 x 1ms on a file not there,
in case it is being renamed over. filememo remembers a file gone after
its first look, but every start pays that first look once per path, and
most agents aren't installed: each agent's part of a catalog sync took
at least ~22ms, 1.3s a round of 116 agents. Windows tells a folder on
the way not there (ERROR_PATH_NOT_FOUND, 3) apart from the file alone
not there (ERROR_FILE_NOT_FOUND, 2); nothing is renamed into a folder
that isn't there, so that one is said gone at once. A file alone not
there is waited on as before.

Measured on xiaomi-gpu (Windows 11), an empty sandbox HOME, magpie web
--gateway for 27s, the second start (files settled), with a build
counting each stat and timing each sync round (instrumentation not
committed):
  before            round 1308ms, 2307 stats
  this + next       round   96ms,  988 stats
Agents not installed now take 0ms each; the slowest left are the two
WSL agents (~25ms, they ask WSL) and reasonix (~23ms).
Probed on the same box: os.Stat of a missing file gives errno 2, of a
file in a missing folder 3 (one and two levels deep).

Run:
- TestStatOfAFileWithNoFolderWaitsNothing (all OSes, Windows faked) and
  TestNoFolderIsPathNotFound (Windows, the real os.Stat): both red with
  the errPathNotFound check taken out by hand ("looked 16 times"), green
  with it, on mote and on xiaomi-gpu.
- xiaomi-gpu: go test ./internal/steady ./internal/edit ./internal/filememo
  ./internal/settings ok.
- mote, at the pushed head with the next commit: gofmt -l internal clean;
  GOOS=linux/windows/darwin go vet of steady, edit, filememo, settings and
  agent clean; go test -count=1 of steady, edit, filememo, settings,
  agent, provider, catalog and library ok. internal/gui is red only on
  TestMainWindowCorners, mote's macOS 27 (#3949, red on main before this).
2026-10-28 13:43:37 +08:00

69 lines
2.0 KiB
Go

package steady
import (
"errors"
"io/fs"
"os"
"runtime"
"testing"
)
// as Windows has it: a file being renamed over is, for a moment, not there
func TestStatWaitsOutAReplace(t *testing.T) {
windows = true
defer func() { windows, stat, readFile = runtime.GOOS == "windows", os.Stat, os.ReadFile }()
missing := 3
stat = func(p string) (fs.FileInfo, error) {
if missing > 0 {
missing--
return nil, fs.ErrNotExist
}
return os.Stat(p)
}
p := t.TempDir()
if _, err := Stat(p); err != nil {
t.Fatalf("a file being replaced taken for gone: %v", err)
}
missing = 3
readFile = func(p string) ([]byte, error) {
if missing > 0 {
missing--
return nil, &fs.PathError{Op: "open", Path: p, Err: fs.ErrNotExist}
}
return []byte("accounts"), nil
}
if b, err := ReadThere(p); err != nil || string(b) != "accounts" {
t.Fatalf("read %q, %v", b, err)
}
// one really gone is said so, after the wait
stat = os.Stat
if _, err := Stat(p + "-none"); !errors.Is(err, fs.ErrNotExist) {
t.Fatalf("a file not there: %v", err)
}
}
// #1665: a file whose folder isn't there is not being renamed into it, and
// is said gone without the wait; most agents aren't installed, and each
// one's first look waited the whole of it
func TestStatOfAFileWithNoFolderWaitsNothing(t *testing.T) {
windows = true
defer func() { windows, stat = runtime.GOOS == "windows", os.Stat }()
looks := 0
stat = func(p string) (fs.FileInfo, error) {
looks++
return nil, &fs.PathError{Op: "stat", Path: p, Err: errors.Join(fs.ErrNotExist, errPathNotFound)}
}
if _, err := Stat("no-folder/settings.json"); !errors.Is(err, fs.ErrNotExist) || looks != 1 {
t.Fatalf("looked %d times: %v", looks, err)
}
// its folder there, the file not, is waited on as before
looks = 0
stat = func(p string) (fs.FileInfo, error) {
looks++
return nil, &fs.PathError{Op: "stat", Path: p, Err: fs.ErrNotExist}
}
if _, err := Stat("settings.json"); !errors.Is(err, fs.ErrNotExist) || looks < 2 {
t.Fatalf("looked %d times: %v", looks, err)
}
}