Files
magpie/internal/settings/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

51 lines
1.5 KiB
Go

package settings
import (
"bytes"
"os"
"testing"
"time"
"github.com/yetone/magpie/internal/filememo"
)
// #1665: a catalog sync holds what was read for its round. A change to the
// settings made while it holds them is made from settings.json as it is on
// the disk: what the user or another magpie saved inside the held second
// stays.
func TestHeldSettingsAreNotWhatAWriteIsMadeFrom(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
if err := Save(Settings{Theme: "light"}); err != nil {
t.Fatal(err)
}
old := time.Now().Add(-time.Hour)
if err := os.Chtimes(Path(), old, old); err != nil {
t.Fatal(err)
}
release := filememo.Hold()
defer release()
if Load().Theme != "light" {
t.Fatal("theme not saved")
}
// another magpie's save, past the time a stamp can't tell apart
b, err := os.ReadFile(Path())
if err != nil || !bytes.Contains(b, []byte(`"light"`)) {
t.Fatalf("%v: %s", err, b)
}
if err := os.WriteFile(Path(), bytes.Replace(b, []byte(`"light"`), []byte(`"dark"`), 1), 0o600); err != nil {
t.Fatal(err)
}
if err := os.Chtimes(Path(), old.Add(time.Minute), old.Add(time.Minute)); err != nil {
t.Fatal(err)
}
if Load().Theme != "light" {
t.Fatal("not held: the test no longer holds what it is about")
}
if err := SetGatewayConversations(true); err != nil {
t.Fatal(err)
}
if got := Load(); got.Theme != "dark" || !got.GatewayConversations {
t.Fatalf("theme %q, recording %v: the other save was written over", got.Theme, got.GatewayConversations)
}
}