fix(metrics): skip the detached flusher spawn under BEADS_TEST_MODE (#5248)
* fix(metrics): skip detached flusher spawn under BEADS_TEST_MODE (bd mybd-syou)
10+ test files hand-set BD_DISABLE_EVENT_FLUSH=1 to stop the detached
`bd send-metrics` child (spawned in internal/metrics/spawn.go) from racing
t.TempDir's RemoveAll (gastownhall/beads#5032, commit 08881217b). Add a
source-level guard so MaybeSpawnFlusher also skips the spawn when
BEADS_TEST_MODE=1, matching the detection idiom the storage layer already
uses (os.Getenv("BEADS_TEST_MODE") == "1"). BD_DISABLE_EVENT_FLUSH keeps
working unchanged; existing test files that set it are left alone for now.
Agent-Signature: claude-sonnet-5-medium on behalf of matt wilkie
* test(metrics): assert the extracted spawn gate; make unset case hermetic
Review round: shouldSpawnFlusher() extracts the whole spawn decision so the
guard test fails on regression instead of silently forking a child, and the
'unset' subtest clears BEADS_TEST_MODE explicitly — the repo's own runner
(scripts/test.sh) exports it, so ambient reads broke a supported test mode.
Agent-Signature: claude-fable-5-high on behalf of maphew
* test(metrics): clear CI's BD_DISABLE_EVENT_FLUSH before exercising the test-mode gate
CI exports BD_DISABLE_EVENT_FLUSH=1 workflow-wide, so the precondition
assertion fired before the guard under test was ever reached (codex review).
Agent-Signature: claude-fable-5-high on behalf of matt wilkie
This commit is contained in:
parent
c860100b84
commit
8b0048fa28
2 changed files with 112 additions and 4 deletions
|
|
@ -15,11 +15,27 @@ const SendMetricsSubcommand = "send-metrics"
|
|||
// before control returns to main()'s spawn tail.
|
||||
const EnvIsFlusher = "BD_IS_FLUSHER"
|
||||
|
||||
// EnvTestMode is the same test-run flag the storage layer already checks
|
||||
// (e.g. internal/storage/dolt/open.go, store.go): `os.Getenv(EnvTestMode) ==
|
||||
// "1"`. MaybeSpawnFlusher honors it too so test suites no longer need to
|
||||
// hand-set BD_DISABLE_EVENT_FLUSH=1 just to stop the detached send-metrics
|
||||
// child from racing t.TempDir's RemoveAll (gastownhall/beads#5032).
|
||||
const EnvTestMode = "BEADS_TEST_MODE"
|
||||
|
||||
// inTestMode reports whether the process is running under a beads test
|
||||
// suite, using the same idiom as the rest of the codebase.
|
||||
func inTestMode() bool {
|
||||
return os.Getenv(EnvTestMode) == "1"
|
||||
}
|
||||
|
||||
// shouldSpawnFlusher is the whole spawn gate, extracted so tests can assert
|
||||
// the decision without forking a real detached child.
|
||||
func shouldSpawnFlusher() bool {
|
||||
return os.Getenv(EnvIsFlusher) != "1" && Enabled() && !flushDisabledByEnv() && !inTestMode()
|
||||
}
|
||||
|
||||
func MaybeSpawnFlusher() {
|
||||
if os.Getenv(EnvIsFlusher) == "1" {
|
||||
return
|
||||
}
|
||||
if !Enabled() || flushDisabledByEnv() {
|
||||
if !shouldSpawnFlusher() {
|
||||
return
|
||||
}
|
||||
self, err := os.Executable()
|
||||
|
|
|
|||
92
internal/metrics/spawn_test.go
Normal file
92
internal/metrics/spawn_test.go
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
package metrics
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestInTestModeDetectsEnv exercises the extracted boolean decision directly:
|
||||
// inTestMode must match on BEADS_TEST_MODE=1 exactly, the same idiom the
|
||||
// storage layer uses, and treat anything else (unset, empty, "0", "true") as
|
||||
// not-test-mode.
|
||||
func TestInTestModeDetectsEnv(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
val string
|
||||
set bool
|
||||
want bool
|
||||
}{
|
||||
{name: "unset", set: false, want: false},
|
||||
{name: "empty", val: "", set: true, want: false},
|
||||
{name: "one", val: "1", set: true, want: true},
|
||||
{name: "true-word", val: "true", set: true, want: false},
|
||||
{name: "zero", val: "0", set: true, want: false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if tc.set {
|
||||
t.Setenv(EnvTestMode, tc.val)
|
||||
} else {
|
||||
// Hermetic unset: the repo's own runner exports
|
||||
// BEADS_TEST_MODE=1 (scripts/test.sh), so relying on the
|
||||
// ambient environment would fail under a supported test
|
||||
// mode. t.Setenv registers the restore; Unsetenv then
|
||||
// clears it for this subtest.
|
||||
t.Setenv(EnvTestMode, "")
|
||||
os.Unsetenv(EnvTestMode)
|
||||
}
|
||||
if got := inTestMode(); got != tc.want {
|
||||
t.Errorf("inTestMode() = %v, want %v (val=%q set=%v)", got, tc.want, tc.val, tc.set)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestMaybeSpawnFlusherNoOpInTestMode guards the source-level test-mode guard:
|
||||
// with BEADS_TEST_MODE=1 set, MaybeSpawnFlusher must skip spawning the
|
||||
// detached send-metrics child even though metrics are otherwise enabled and
|
||||
// BD_DISABLE_EVENT_FLUSH is unset. If the guard regresses this would fork a
|
||||
// real child process during `go test`.
|
||||
func TestMaybeSpawnFlusherNoOpInTestMode(t *testing.T) {
|
||||
t.Setenv("HOME", t.TempDir())
|
||||
t.Setenv(EnvTestMode, "1")
|
||||
// CI exports BD_DISABLE_EVENT_FLUSH=1 workflow-wide (main.yml/pr.yml env
|
||||
// blocks), which would trip the flushDisabledByEnv precondition below
|
||||
// before the test-mode gate is ever exercised. Clear it hermetically.
|
||||
t.Setenv(EnvDisableEventFlush, "")
|
||||
os.Unsetenv(EnvDisableEventFlush)
|
||||
|
||||
if _, err := Init("0.0.0-test", true, ""); err != nil {
|
||||
t.Fatalf("Init: %v", err)
|
||||
}
|
||||
if !Enabled() {
|
||||
t.Fatalf("Enabled() = false, want true")
|
||||
}
|
||||
if flushDisabledByEnv() {
|
||||
t.Fatalf("flushDisabledByEnv() = true, want false (BD_DISABLE_EVENT_FLUSH unset)")
|
||||
}
|
||||
|
||||
// The only thing preventing a spawn here is the BEADS_TEST_MODE guard —
|
||||
// assert the extracted decision directly, so a regression fails the test
|
||||
// instead of silently forking a real child.
|
||||
if shouldSpawnFlusher() {
|
||||
t.Fatalf("shouldSpawnFlusher() = true under BEADS_TEST_MODE=1, want false")
|
||||
}
|
||||
|
||||
// Positive control: with test mode hermetically cleared (and every other
|
||||
// gate still open), the spawn decision must flip to true — proving the
|
||||
// assertion above is gated on BEADS_TEST_MODE and not on some other
|
||||
// condition that happens to hold in this environment.
|
||||
t.Setenv(EnvTestMode, "")
|
||||
os.Unsetenv(EnvTestMode)
|
||||
t.Setenv(EnvDisableEventFlush, "")
|
||||
os.Unsetenv(EnvDisableEventFlush)
|
||||
if !shouldSpawnFlusher() {
|
||||
t.Fatalf("shouldSpawnFlusher() = false with BEADS_TEST_MODE cleared, want true")
|
||||
}
|
||||
|
||||
// And the exported entry point still returns without spawning under the
|
||||
// guard (smoke path).
|
||||
t.Setenv(EnvTestMode, "1")
|
||||
MaybeSpawnFlusher()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue