tests: TestCursorEditorCopiedOncePerChange keeps its copy past a loaded runner's pause, not a 100ms linger that counts a third copy

The test set cursorSnapLinger to 100ms for the whole run, so a pause of
more than that between two reads (List after the change, then the next
visit) let the shared copy go and the next read made another: "copies
after one change: 3, want 2" in a go test -tags nogui ./... run on a Mac
at load average ~200. The linger is now an hour while copies are
counted and cut to 100ms only for the last check, that nothing is left
in the temp folder. It is set under cursorSnaps' lock, where the timers
read it.

Run: with a 300ms sleep injected before the visit after the change, the
old test fails with "copies after one change: 3, want 2"; the new one
passes with 300ms sleeps injected there and between the first two
visits. go test -race -count=5 of it and TestCursorLeftCopiesSwept;
go test -tags nogui ./internal/sessions (whole package, sandbox HOME);
gofmt -l; go vet darwin, GOOS=linux -tags nogui, GOOS=windows.
This commit is contained in:
yetone
2026-10-15 00:03:24 +08:00
parent 0c0231b80f
commit 2efb71ea0f
+17 -2
View File
@@ -24,9 +24,20 @@ func cursorTempCopies(t *testing.T) []string {
func TestCursorEditorCopiedOncePerChange(t *testing.T) {
cursorSetup(t)
t.Setenv("TMPDIR", t.TempDir())
// The copy lingers past any pause a loaded runner makes between two
// reads while copies are counted (a 100ms linger let a slow runner drop
// it between List and the next visit, and count a third copy); it is
// cut short only to see the last one removed. The timers read the
// linger under cursorSnaps' lock, so it is set under it.
linger := func(d time.Duration) {
cursorSnaps.Lock()
cursorSnapLinger = d
cursorSnaps.Unlock()
}
minWas, lingerWas := cursorSnapMin, cursorSnapLinger
cursorSnapMin, cursorSnapLinger = 0, 100*time.Millisecond
t.Cleanup(func() { cursorSnapMin, cursorSnapLinger = minWas, lingerWas })
cursorSnapMin = 0
linger(time.Hour)
t.Cleanup(func() { cursorSnapMin = minWas; linger(lingerWas) })
config, err := os.UserConfigDir()
if err != nil {
@@ -99,6 +110,10 @@ func TestCursorEditorCopiedOncePerChange(t *testing.T) {
t.Fatalf("copies after one change: %d, want 2", n)
}
// one more read lets the copy go with a short linger
linger(100 * time.Millisecond)
List(All)
deadline := time.Now().Add(5 * time.Second)
for len(cursorTempCopies(t)) > 0 {
if time.Now().After(deadline) {