Files
pulse/internal/mock/fixture_data_version_test.go
pulse-triage[bot] 7348c17905 Stabilize mock snapshot memoization proof
Pause the automatic mock metric ticker while the test asserts same-version snapshot identity, then restore it during cleanup. This prevents the full race suite from invalidating the fixture between the two reads it is comparing.

Change-source: pulse-maintainer
2026-08-27 06:44:56 +01:00

71 lines
1.8 KiB
Go

package mock
import (
"testing"
)
func withMockEnabledForTest(t *testing.T) {
t.Helper()
previous := IsMockEnabled()
if err := SetEnabled(true); err != nil {
t.Fatalf("enable mock mode: %v", err)
}
t.Cleanup(func() {
if err := SetEnabled(previous); err != nil {
t.Fatalf("restore mock mode: %v", err)
}
})
}
func TestFixtureDataVersionAdvancesOnMetricTick(t *testing.T) {
withMockEnabledForTest(t)
before := FixtureDataVersion()
updateMetrics(GetConfig())
after := FixtureDataVersion()
if after <= before {
t.Fatalf("expected FixtureDataVersion to advance on a metric tick, got %d -> %d", before, after)
}
}
func TestUnifiedResourceSnapshotMemoizedPerDataVersion(t *testing.T) {
withMockEnabledForTest(t)
// This test advances the fixture explicitly below. Pause the automatic
// metric ticker so a slow race-enabled snapshot build cannot change the
// data version between the two reads whose memoization identity we are
// asserting.
stopUpdateLoop()
t.Cleanup(func() {
if IsMockEnabled() {
startUpdateLoop()
}
})
first, firstFreshness := UnifiedResourceSnapshot()
if len(first) == 0 {
t.Fatal("expected mock resources")
}
second, secondFreshness := UnifiedResourceSnapshot()
if len(second) != len(first) {
t.Fatalf("expected identical memoized result, got %d vs %d resources", len(first), len(second))
}
if &first[0] != &second[0] {
t.Fatal("expected the memoized snapshot to be returned for an unchanged data version")
}
if !firstFreshness.Equal(secondFreshness) {
t.Fatalf("expected identical freshness, got %v vs %v", firstFreshness, secondFreshness)
}
updateMetrics(GetConfig())
third, _ := UnifiedResourceSnapshot()
if len(third) == 0 {
t.Fatal("expected mock resources after tick")
}
if &first[0] == &third[0] {
t.Fatal("expected a rebuilt snapshot after the data version advanced")
}
}