250adf5e(#3185) has Keep, turned off, wait on filememo.Written for the setting, but it took the channel after the look (level, which is settings.Load in the gateway), not before it. A save in between, the user turning Keep awake on, had Forget close the channel from before the look, and Keep waited on the one made after it until its beat, filememo.OthersEvery(): 5s polled, a minute while Watch runs (where the watch's event for the same save still woke it, if that came after Written). A write from the CLI or a second magpie, which only the watch's wake tells of, had the same window. The window is narrow, from Load's read of settings.json (its unlock, for a save in this process, which waits for that) to Written, and wider only while let() lets go of a hold. It came up in review of #3632. Keep now takes Written before the look, as middleware's observer, OTel's session watch, pluginrt.KeepFresh and the agent's Codex daemon check do, and waits on that channel when the look found it off. On, it waits for its tick alone, as before: the channel is taken on every pass but waited on only after a look that found it off. What was run (tree e8e23dae, on origin/mainef0a03e1, go1.26.8, linux/amd64): - TestKeepOffSeesAWriteDuringTheLook (new): the level func turns the setting on and writes it (filememo.Forget) right after its read, at least 2s before the next beat, and Keep must ask it again before that beat. It times its own level func's second call, not a hold: a Keep that another test's failure leaves running holds through the same stand-in. ok at -count=5, 0.00s each. - Broken by hand, each put back after, -count=3 of the new test alone and of the whole package: - ef0a03e1's awake.go (Written taken after the look): red 3 of 3 and 3 of 3, the new test only, "turned on during the look, the setting was looked at again 4.999540055s after it was written, at the beat (00:22:45), not at once", 4.46s to 5.00s across the six. - Written taken right after level(): the same, 4.44s to 5.00s. - Written taken before the look but not waited on when off: the new test red 3 of 3 and 3 of 3, 4.43s to 5.00s; in the whole package TestKeep and TestKeepOffWaitsForTheSetting go red beside it. - Written taken once, above the loop: every test passes, the new one too. After the first write Keep would spin while off, and no test writes and leaves it off; noted on #3687, with the other test fixes this file needs. - The new test started 1.5s before a beat, so that it waits that beat out before the write (a scratch wrapper, not committed): ok 3 of 3, and red 3 of 3 with ef0a03e1's awake.go, 5.00s each. - Outside graftcp, ./internal/awake with -race -count=20, -count=20, and -race -shuffle=on -count=20: 100 of 100 ok each. The new test took 0.00s in 19 of each 20, and 1.87s, 1.91s and 1.90s in the one that started under 2s before a beat. TestKeepOffWaitsForTheSetting, unchanged here, is red about once in 100 runs on098ac969(4 of 400) when a 5s beat lands in its 50ms window: #3687. It didn't come up in these runs. - gofmt -l clean; GOOS=linux and darwin go vet -tags nogui ./..., GOOS=windows go vet ./..., GOOS=linux and darwin go build -tags nogui ./... and GOOS=windows go build ./...: exit 0 each. go vet and go build ./... without the tag on linux stop at pkg-config here (no libsoup-3.0 or gio-unix-2.0), exit 1. - Full suite outside graftcp (systemd-run, umask 077, Bun 1.4.2 first on PATH as on CI, -timeout 45m, log kept): 66 ok, awake, beat, filememo and settings among them, and 6 red, none in what this changes: - agent TestInstalledVersion, TestHowInstalled, TestCursorFolderFromTheEditorIsNotTheCLI, TestCopilotFolderFromAnIDEPluginIsNotTheCLI, TestQoderSettingsFromAnIDEPluginIsNotTheCLI and TestOmpDetectedWithPiDir: #3141; TestReasonixCLIRequiresSupportedNativeVersion and TestReasonixCLIRecoversAfterProbeTimeout: #3641. - edit TestWriteAtomicReadOnlyHardLink and testenv TestProgram: this box's umask 077, #3640. - gateway TestPluginToldWhereTheAgentWorks: this box's /tmp/.git, #3675. - gui TestGatewayHeldByAContainer and TestGatewayFixQuitsOnlyAMagpie: #3649. - provider: "fatal error: close of synctest channel from outside bubble", main's CI red, #3632. Not tried: a real gateway with Keep awake turned on inside the window (it is too narrow to hit by hand; the test puts the write there), and macOS and Windows, vet and build only (nothing here depends on the OS). Reviewed on macOS arm64, go1.26.8: internal/awake -race -count=20 ok. Broken by hand: main's awake.go turned TestKeepOffSeesAWriteDuringTheLook red twice of two ("looked at again 4.09s / 5.00s after it was written, at the beat"). The other loops waiting on filememo.Written (middleware points, otlp sessions, pluginrt config, codex daemon) already take it before their look. gofmt clean; vet ok for linux nogui, windows, darwin.
253 lines
7.3 KiB
Go
253 lines
7.3 KiB
Go
package awake
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/beat"
|
|
"github.com/yetone/magpie/internal/filememo"
|
|
)
|
|
|
|
func TestWant(t *testing.T) {
|
|
now := time.Now()
|
|
for _, c := range []struct {
|
|
st State
|
|
want bool
|
|
}{
|
|
{State{}, false},
|
|
{State{Busy: true}, true},
|
|
{State{Last: now.Add(-time.Minute)}, true},
|
|
{State{Last: now.Add(-Grace - time.Second)}, false},
|
|
} {
|
|
if got := want(c.st, now); got != c.want {
|
|
t.Errorf("want(%+v) = %v, want %v", c.st, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestKeep holds sleep off while the agents work and the setting is on, once,
|
|
// and lets go when they are idle, when it is turned off and when Keep ends.
|
|
func TestKeep(t *testing.T) {
|
|
oldHold, oldTick := hold, tick
|
|
defer func() { hold, tick = oldHold, oldTick }()
|
|
tick = time.Millisecond
|
|
var held, takes atomic.Int32
|
|
hold = func(bool) (func(), error) {
|
|
held.Add(1)
|
|
takes.Add(1)
|
|
return func() { held.Add(-1) }, nil
|
|
}
|
|
var mu sync.Mutex
|
|
on, st := true, State{}
|
|
// the setting is written as settings.Save writes it (filememo.Forget)
|
|
set := func(o bool, s State) { mu.Lock(); on, st = o, s; mu.Unlock(); filememo.Forget() }
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
done := make(chan struct{})
|
|
go func() {
|
|
Keep(ctx, func() Level {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if on {
|
|
return System
|
|
}
|
|
return Off
|
|
},
|
|
func() State { mu.Lock(); defer mu.Unlock(); return st })
|
|
close(done)
|
|
}()
|
|
wait := func(n int32, what string) {
|
|
t.Helper()
|
|
for end := time.Now().Add(2 * time.Second); held.Load() != n; {
|
|
if time.Now().After(end) {
|
|
t.Fatalf("%s: held %d, want %d", what, held.Load(), n)
|
|
}
|
|
time.Sleep(time.Millisecond)
|
|
}
|
|
}
|
|
wait(0, "idle")
|
|
set(true, State{Busy: true})
|
|
wait(1, "busy")
|
|
time.Sleep(20 * time.Millisecond)
|
|
if takes.Load() != 1 {
|
|
t.Fatalf("taken %d times while busy, want once", takes.Load())
|
|
}
|
|
set(true, State{Last: time.Now()})
|
|
time.Sleep(20 * time.Millisecond)
|
|
wait(1, "within grace")
|
|
set(true, State{Last: time.Now().Add(-Grace - time.Minute)})
|
|
wait(0, "past grace")
|
|
set(false, State{Busy: true})
|
|
time.Sleep(20 * time.Millisecond)
|
|
wait(0, "off")
|
|
set(true, State{Busy: true})
|
|
wait(1, "on again")
|
|
cancel()
|
|
<-done
|
|
wait(0, "ended")
|
|
}
|
|
|
|
// TestKeepDisplay holds the display on only at Display, and takes the hold
|
|
// anew when the level changes while the agents work (#975).
|
|
func TestKeepDisplay(t *testing.T) {
|
|
oldHold, oldTick := hold, tick
|
|
defer func() { hold, tick = oldHold, oldTick }()
|
|
tick = time.Millisecond
|
|
var mu sync.Mutex
|
|
var holds []bool // display of each hold taken
|
|
live := 0
|
|
hold = func(display bool) (func(), error) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
holds = append(holds, display)
|
|
live++
|
|
return func() { mu.Lock(); live--; mu.Unlock() }, nil
|
|
}
|
|
level := System
|
|
set := func(l Level) { mu.Lock(); level = l; mu.Unlock(); filememo.Forget() }
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
done := make(chan struct{})
|
|
go func() {
|
|
Keep(ctx, func() Level { mu.Lock(); defer mu.Unlock(); return level },
|
|
func() State { return State{Busy: true} })
|
|
close(done)
|
|
}()
|
|
waitFor := func(what string, ok func() bool) {
|
|
t.Helper()
|
|
for end := time.Now().Add(2 * time.Second); ; {
|
|
mu.Lock()
|
|
good := ok()
|
|
mu.Unlock()
|
|
if good {
|
|
return
|
|
}
|
|
if time.Now().After(end) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
t.Fatalf("%s: holds %v, live %d", what, holds, live)
|
|
}
|
|
time.Sleep(time.Millisecond)
|
|
}
|
|
}
|
|
waitFor("system", func() bool { return len(holds) == 1 && !holds[0] && live == 1 })
|
|
set(Display)
|
|
waitFor("display", func() bool { return len(holds) == 2 && holds[1] && live == 1 })
|
|
time.Sleep(20 * time.Millisecond)
|
|
mu.Lock()
|
|
if len(holds) != 2 {
|
|
t.Errorf("taken again at the same level: %v", holds)
|
|
}
|
|
mu.Unlock()
|
|
set(System)
|
|
waitFor("system again", func() bool { return len(holds) == 3 && !holds[2] && live == 1 })
|
|
set(Off)
|
|
waitFor("off", func() bool { return live == 0 })
|
|
cancel()
|
|
<-done
|
|
}
|
|
|
|
// Turned off, Keep looks at the setting again when it is written, not each
|
|
// few seconds (#3185), and turning it on is still taken at once.
|
|
func TestKeepOffWaitsForTheSetting(t *testing.T) {
|
|
oldHold, oldTick := hold, tick
|
|
defer func() { hold, tick = oldHold, oldTick }()
|
|
tick = time.Millisecond
|
|
var held atomic.Int32
|
|
hold = func(bool) (func(), error) { held.Add(1); return func() { held.Add(-1) }, nil }
|
|
var looks atomic.Int32
|
|
var on atomic.Bool
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
done := make(chan struct{})
|
|
go func() {
|
|
Keep(ctx, func() Level {
|
|
looks.Add(1)
|
|
if on.Load() {
|
|
return System
|
|
}
|
|
return Off
|
|
}, func() State { return State{Busy: true} })
|
|
close(done)
|
|
}()
|
|
defer func() { cancel(); <-done }()
|
|
for end := time.Now().Add(2 * time.Second); looks.Load() == 0; time.Sleep(time.Millisecond) {
|
|
if time.Now().After(end) {
|
|
t.Fatal("Keep never looked at the setting")
|
|
}
|
|
}
|
|
time.Sleep(50 * time.Millisecond)
|
|
if n := looks.Load(); n != 1 {
|
|
t.Fatalf("turned off, Keep looked at the setting %d times in 50ms with nothing written", n)
|
|
}
|
|
on.Store(true)
|
|
filememo.Forget()
|
|
for end := time.Now().Add(2 * time.Second); held.Load() != 1; time.Sleep(time.Millisecond) {
|
|
if time.Now().After(end) {
|
|
t.Fatal("turning the setting on held nothing")
|
|
}
|
|
}
|
|
}
|
|
|
|
// Turned off, a setting turned on while Keep looks, after the look read it,
|
|
// is looked at again at once, not at the beat after: Keep waits on the
|
|
// Written it took before the look, which that write closes.
|
|
func TestKeepOffSeesAWriteDuringTheLook(t *testing.T) {
|
|
oldHold := hold
|
|
defer func() { hold = oldHold }()
|
|
hold = func(bool) (func(), error) { return func() {}, nil }
|
|
type write struct{ at, next time.Time }
|
|
wrote := make(chan write, 1)
|
|
// when this test's level is asked again: a hold could be taken by a
|
|
// Keep that another test's failure left running
|
|
again := make(chan time.Time, 1)
|
|
var on atomic.Bool
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
done := make(chan struct{})
|
|
go func() {
|
|
Keep(ctx, func() Level {
|
|
if on.Load() {
|
|
select {
|
|
case again <- time.Now():
|
|
default:
|
|
}
|
|
return System
|
|
}
|
|
// the beat Keep waits for, turned off (beat.Next: the next
|
|
// multiple of OthersEvery on the clock), 2s away at least, so
|
|
// that a look at once has room before it
|
|
every := filememo.OthersEvery()
|
|
next := time.Now().Truncate(every).Add(every)
|
|
if time.Until(next) < 2*time.Second {
|
|
<-beat.Next(every)
|
|
next = time.Now().Truncate(every).Add(every)
|
|
}
|
|
// the user turns it on just after this read, as settings.save
|
|
// writes it (filememo.Forget)
|
|
at := time.Now()
|
|
on.Store(true)
|
|
filememo.Forget()
|
|
wrote <- write{at, next}
|
|
return Off
|
|
}, func() State { return State{Busy: true} })
|
|
close(done)
|
|
}()
|
|
defer func() { cancel(); <-done }()
|
|
var w write
|
|
select {
|
|
case w = <-wrote:
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatal("Keep never looked at the setting")
|
|
}
|
|
select {
|
|
case at := <-again:
|
|
// both on the wall clock, as beat.Next goes by it (Truncate drops
|
|
// the monotonic reading)
|
|
if !at.Before(w.next) {
|
|
t.Fatalf("turned on during the look, the setting was looked at again %v after it was written, at the beat (%s), not at once", at.Sub(w.at), w.next.Format(time.TimeOnly))
|
|
}
|
|
case <-time.After(time.Until(w.next) + 10*time.Second):
|
|
t.Fatal("turned on during the look, the setting was never looked at again")
|
|
}
|
|
}
|