From 8a881dc20360b0f893d2974afcd30c560cfc577e Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 11 Oct 2026 12:00:24 +0800 Subject: [PATCH] provider, agent: the sign-in watch tests count no reads of their own check, in both packages TestAccountDiscoveryOffReadsNoSignIn (internal/provider) is the sibling 5421f4468 left: its sanity read failed on "{}{}" when the FIFO was answered twice under load, and 909d76747 shipped past it as "fails on main too (1 in ~20)". It now takes one or more "{}" and counts only opens after the check, as the agent test does. Both tests also had a second race in that fix: a write the watch began while the check's read was still open (after its EOF, before close) is noted after the read returns, past `checked`, and reported as a read of a sign-in. Each test now waits until the notes stop for 20ms before counting. Ran: on origin/main's provider test, a 5ms delay before the read: 3/3 red; with the fix 5/5 green. Late close (1ms after EOF) plus 3ms before the watch notes an open: without the wait 6/10 red (".codex/auth.json"), with it 10/10 green. A stray read of a sign-in after the check is still reported (only that file). -count=30 of each test; go test ./internal/agent ./internal/provider; go vet and GOOS=linux go vet; gofmt -l clean. --- internal/agent/discovery_test.go | 5 +++++ internal/provider/discovery_test.go | 20 ++++++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/internal/agent/discovery_test.go b/internal/agent/discovery_test.go index 5f10f2309..df731ca45 100644 --- a/internal/agent/discovery_test.go +++ b/internal/agent/discovery_test.go @@ -95,6 +95,11 @@ func TestAccountDiscoveryOffAgentsReadNoSignIn(t *testing.T) { if b, err := os.ReadFile(files[0]); err != nil || len(b) == 0 || strings.ReplaceAll(string(b), "{}", "") != "" { t.Fatalf("the watch didn't answer a read: %q %v", b, err) } + // a write the watch began while the read was still open is noted + // after it returns; wait for the notes to stop before counting + for n := -1; n != len(opened()); time.Sleep(20 * time.Millisecond) { + n = len(opened()) + } checked := len(opened()) cfgDir := filepath.Join(home, ".config", "opencode") diff --git a/internal/provider/discovery_test.go b/internal/provider/discovery_test.go index 4f664b378..482be36c1 100644 --- a/internal/provider/discovery_test.go +++ b/internal/provider/discovery_test.go @@ -132,13 +132,21 @@ func TestAccountDiscoveryOffReadsNoSignIn(t *testing.T) { files = slices.Compact(files) opened := openWatch(t, files) - // the watch sees a read: this test's own - if b, err := os.ReadFile(codexAuthPath()); err != nil || string(b) != "{}" { + // the watch sees a read: this test's own. A reader slow to reach the + // end is handed "{}" again by the next loop's write, and noted again, + // so under load it reads "{}{}" + if b, err := os.ReadFile(codexAuthPath()); err != nil || len(b) == 0 || strings.ReplaceAll(string(b), "{}", "") != "" { t.Fatalf("the watch didn't answer a read: %q %v", b, err) } - if got := opened(); len(got) != 1 { + if got := opened(); len(got) == 0 || slices.ContainsFunc(got, func(p string) bool { return p != codexAuthPath() }) { t.Fatalf("the watch saw %v, want the test's own read", got) } + // a write the watch began while the read was still open is noted + // after it returns; wait for the notes to stop before counting + for n := -1; n != len(opened()); time.Sleep(20 * time.Millisecond) { + n = len(opened()) + } + checked := len(opened()) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() @@ -203,9 +211,9 @@ func TestAccountDiscoveryOffReadsNoSignIn(t *testing.T) { // anything started in the background has its moment time.Sleep(300 * time.Millisecond) - if got := opened(); len(got) != 1 { - read := slices.Compact(slices.Sorted(slices.Values(got[1:]))) - t.Errorf("with discovery off, %d reads of sign-ins: %s", len(got)-1, strings.Join(read, " ")) + if got := opened()[checked:]; len(got) != 0 { + read := slices.Compact(slices.Sorted(slices.Values(got))) + t.Errorf("with discovery off, %d reads of sign-ins: %s", len(got), strings.Join(read, " ")) } if s := ran(); s != "" { t.Errorf("with discovery off, these ran:\n%s", s)