Files
magpie/internal/filememo/filememo_windows_test.go
T
yetone d5619d5a92 windows: an account list read while another magpie writes it is waited for, not taken for no accounts (Packing1 on Discord, Windows 11: 早上更新重启后之前登录的Antigravity订阅供应商和账号都没了 … 整行都没了,供应商里面antigravity直接消失了) — on Windows a file being renamed over (every write magpie makes: a temp file renamed onto it) is, for a moment of up to ~2ms, "The system cannot find the file specified" to anyone else looking at it, with MoveFileEx and with POSIX-semantics renames alike. filememo.Read gave that back as an error, readLogins as no accounts, and whatever change came next (any account's read-modify-write) wrote the empty list back: every built-in account gone, its subscription with it. Measured on Windows 11 (10.0.0.132): one process replacing a 200 KB file, another stat-ing and reading it, saw the file "not there" in a few windows each 20 s. Now steady.Stat and steady.ReadThere wait out a moment of a file not there (15 tries a millisecond apart, Windows only), and filememo looks through them, remembering a file found not there so one that really isn't costs the wait once. A magpie started by an update now waits for the one it replaces before its start-up moves write any file (AwaitPredecessor at the top of run, not only in the GUI). TestLoginsKeptWhileAnotherMagpieWrites (Windows: two writers replacing logins.json, readLogins never without the Antigravity account) failed 1 in 3 and 2 in 8 runs on the old code ("read as no accounts 8 times"), passed 11 of 11 on this; filememo's TestReadWhileReplaced the same (1180 misses on the old code); TestStatWaitsOutAReplace everywhere. A real update in a sandbox home on the Windows box, magpie-cli 0.1.880 → 0.1.942 from the release, kept the account and logins.json byte for byte. go vet (darwin, and windows nogui), builds (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-05 10:57:45 +08:00

61 lines
1.5 KiB
Go

package filememo
import (
"bytes"
"os"
"path/filepath"
"sync/atomic"
"testing"
"time"
"github.com/yetone/magpie/internal/steady"
)
// Another magpie replacing the file (a write is a temp file renamed over
// it) has Windows say, for a moment, that the file isn't there at all: read
// so, a file of accounts was no accounts, and the next write left them out
// (Packing1 on Discord, Windows 11: Antigravity's provider and its account
// gone after an update). Read waits that moment out.
func TestReadWhileReplaced(t *testing.T) {
dir := t.TempDir()
p := filepath.Join(dir, "logins.json")
body := bytes.Repeat([]byte("x"), 100_000)
os.WriteFile(p, body, 0o600)
var stop atomic.Bool
done := make(chan struct{})
for range 2 {
go func() {
defer func() { done <- struct{}{} }()
for !stop.Load() {
tmp, err := os.CreateTemp(dir, ".logins.json.*")
if err != nil {
continue
}
tmp.Write(body)
tmp.Close()
if steady.Rename(tmp.Name(), p) != nil {
os.Remove(tmp.Name())
}
}
}()
}
defer func() {
stop.Store(true)
<-done
<-done
}()
parse := func(b []byte) (int, error) { return len(b), nil }
misses := 0
for end := time.Now().Add(5 * time.Second); time.Now().Before(end); {
if n, err := Read("replaced", p, parse); err != nil || n != len(body) {
misses++
if misses < 4 {
t.Logf("read %d bytes, %v", n, err)
}
}
}
if misses > 0 {
t.Fatalf("the file read as not there %d times while replaced", misses)
}
}