Files
magpie/internal/access/lansecret_unlocked_test.go
T
yetone 23d4dc591a access: LANSecret answers "not shared" from settings, before caller-keys.json's lock
#1665: every agent's sync (keptaway's keeping) asks access.LANSecret.
LANSecret took mu first, which also makes and locks
caller-keys.json.lock, even when the gateway isn't shared on the LAN and
the answer is "" from settings alone. A sandbox start's lock trace
showed ~102 caller-keys.json.lock takes in its first 20s. Now the
not-shared case returns before the lock. The shared case is unchanged.
After the change, the same interposed start counted 7 opens of
caller-keys.json.lock over the run.

TestLANSecretNotSharedTakesNoLock holds mu and expects LANSecret to
return "" within 5s. Once ConfigureLAN(true, false) shares the gateway,
it expects a key. With the settings check moved back under the lock by
hand, it goes red:
  --- FAIL: TestLANSecretNotSharedTakesNoLock (5.25s)
      lansecret_unlocked_test.go:29: not shared, under another's lock: answered=false ""; want "" at once

What was run (worktree at 9fd80b212 plus this change and the commits
before it):
  gofmt -l internal/: nothing
  go vet ./internal/access: exit 0
  GOOS=linux go vet -tags nogui ./...: exit 0
  GOOS=windows go vet -tags nogui ./...: exit 0
  go test -timeout 45m -count=1 ./internal/access: ok 21.008s
2026-10-20 20:23:55 +08:00

39 lines
972 B
Go

package access
import (
"testing"
"time"
)
// #1665: with the gateway not shared, LANSecret answers from the settings
// alone, without taking caller-keys.json's lock (every agent's sync asks
// it, ~100 times in a start's first 20s, each a lock file opened and
// locked); shared, it reads the key under the lock, as before.
func TestLANSecretNotSharedTakesNoLock(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
answer := func() (string, bool) {
got := make(chan string, 1)
go func() { got <- LANSecret() }()
select {
case s := <-got:
return s, true
case <-time.After(5 * time.Second):
return "", false
}
}
mu.Lock()
s, ok := answer()
mu.Unlock()
if !ok || s != "" {
t.Fatalf("not shared, under another's lock: answered=%v %q; want \"\" at once", ok, s)
}
if err := ConfigureLAN(true, false); err != nil {
t.Fatal(err)
}
if s, ok := answer(); !ok || s == "" {
t.Fatalf("shared: answered=%v %q; want the key", ok, s)
}
}