Files
magpie/internal/edit/held_test.go
T
yetone 5160809d2e agent: a catalog sync's round looks at each file once; a write reads the disk (#1665)
A catalog sync asked about each agent's files many times a round
(GetJSON, GetTOMLTop, settings.load from each agent's Sync): on Windows
an empty sandbox's start stat'ed settings.json 1065 times and
config.toml 569 in 25s, almost all from SyncCatalog. Each round now
holds what was read (filememo.Hold), as the GUI's read-only requests and
the gateway already do.

What a hold serves may be up to a second old, so nothing is written from
it any more:
- edit.Read, which every Set*/Del* helper and the agents' own
  read-modify-write code start from, looks at the disk even under a
  hold (filememo.ReadNow). The getters (GetJSON, GetJSONRaw,
  GetJSONItem, GetEnvFile, GetYAMLTop, the YAML getters, GetTOMLTop's
  line reader) read through the hold.
- settings: a write's load under fileMu (lockFile) reads the disk too.
Before this, a GUI request's hold could already hand a Set* stale
bytes; that is closed as well.

WriteAtomic forgot every held file even when it left the file as it
was, and a sync writes each agent's files each round, almost always
unchanged: it now forgets only after a real write.

Measured on xiaomi-gpu (Windows 11), as in the steady commit before
this one (second start, 27s, stats counted by an uncommitted build):
  before          2307 stats, sync 1318 of them, round 1308ms
  this alone      1787 stats, sync  864,          round 1273ms
  with steady's    988 stats, sync   96,          round   96ms
First start of an empty sandbox: 2448 stats before, 1041 after.

Run:
- TestHeldReadIsNotWhatAWriteIsMadeFrom (json, toml, yaml),
  TestUnchangedWriteKeepsWhatIsHeld, TestHeldSettingsAreNotWhatAWriteIsMadeFrom:
  with edit.Read and the settings write load put back on the held read
  and WriteAtomic's Forget removed by hand, json and yaml lost the other
  app's key, the settings case wrote "light" over "dark", and the
  forget case went red; green with the change, on mote.
- mote, at the pushed head: 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 red only on TestMainWindowCorners
  (#3949, macOS 27, red on main before this).
- xiaomi-gpu: go test of edit, filememo and settings ok.
2026-10-28 13:43:37 +08:00

96 lines
3.0 KiB
Go

package edit
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/filememo"
)
// #1665: a catalog sync holds what was read for its round, so an agent's
// files are looked at once a round; a write is still made from what is on
// the disk, never over what another app wrote inside the held second.
func TestHeldReadIsNotWhatAWriteIsMadeFrom(t *testing.T) {
cases := []struct {
name, file, start, theirs string
get func(path string) (string, bool)
set func(path string) error
want []string
}{
{"json", "settings.json", `{"a": 1}`, `{"a": 1, "theirs": true}`,
func(p string) (string, bool) { return GetJSON(p, "a") },
func(p string) error { return SetJSON(p, KV{"ours", 2}) },
[]string{`"theirs": true`, `"ours"`}},
{"toml", "config.toml", "a = 1\n", "a = 1\ntheirs = true\n",
func(p string) (string, bool) { return GetTOMLTop(p, "a") },
func(p string) error { return SetTOMLTop(p, KV{"ours", 2}) },
[]string{"theirs = true", "ours = 2"}},
{"yaml", "config.yaml", "a: 1\n", "a: 1\ntheirs: true\n",
func(p string) (string, bool) { return GetYAMLTop(p, "a") },
func(p string) error { return SetYAMLTop(p, KV{"ours", 2}) },
[]string{"theirs: true", "ours: 2"}},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), c.file)
old := time.Now().Add(-time.Hour)
writeAged(t, path, c.start, old)
release := filememo.Hold()
defer release()
if v, ok := c.get(path); !ok || v != "1" {
t.Fatalf("get = %q, %v", v, ok)
}
// another app's write, past the time a stamp can't tell apart
writeAged(t, path, c.theirs, old.Add(time.Minute))
if v, ok := c.get(path); !ok || v != "1" {
t.Fatalf("held get = %q, %v", v, ok)
}
if err := c.set(path); err != nil {
t.Fatal(err)
}
b, _ := os.ReadFile(path)
for _, w := range c.want {
if !strings.Contains(string(b), w) {
t.Fatalf("missing %q after the write:\n%s", w, b)
}
}
})
}
}
func writeAged(t *testing.T, path, text string, at time.Time) {
t.Helper()
if err := os.WriteFile(path, []byte(text), 0o644); err != nil {
t.Fatal(err)
}
if err := os.Chtimes(path, at, at); err != nil {
t.Fatal(err)
}
}
// A write that leaves a file as it is keeps what is held of every file:
// a sync writes each agent's files each round, almost always unchanged.
func TestUnchangedWriteKeepsWhatIsHeld(t *testing.T) {
path := filepath.Join(t.TempDir(), "settings.json")
writeAged(t, path, `{"a": 1}`, time.Now().Add(-time.Hour))
release := filememo.Hold()
defer release()
GetJSON(path, "a")
g := filememo.Gen()
if err := SetJSON(path, KV{"a", 1}); err != nil {
t.Fatal(err)
}
if filememo.Gen() != g {
t.Fatal("an unchanged write forgot what was held")
}
if err := SetJSON(path, KV{"a", 2}); err != nil {
t.Fatal(err)
}
if filememo.Gen() == g {
t.Fatal("a write that changed the file kept what was held")
}
}