Files
pulse/internal/monitoring/mock_unified_view_cache_test.go
T
rcourtman 518a5e2294 Cache mock unified snapshots instead of rebuilding registries per read
In mock mode every unified read-state access built two throwaway
registries: mock.UnifiedResourceSnapshot constructed one to derive the
resource list, and the monitor's currentUnifiedStateView ingested that
list into another, deep-cloning all resources both ways. Chart requests,
broadcasts, alert evaluation, and API reads each repaid that full cost —
the dominant share of the demo's 76TB/9.5d allocation churn, since every
one of those reads runs against a world that only changes on the 2-second
mock tick.

Introduce fixtureDataVersion, a token that advances on every observable
mock-graph change (metric ticks and the structural changes that bump
fixtureRevision, which stays structural-only so seeded trend history
remains reusable). Memoize the package-level UnifiedResourceSnapshot and
the monitor's mock-branch state view against it, so consumers between
ticks share one immutable build. Sharing mirrors the semantics the
persistent-store ReadState path has always had in real mode: all
consumers were audited — they ingest (which clones), copy before
top-level writes, or build fresh outputs. Real-mode paths are untouched.

Contract-Neutral: mock snapshot memoization: identical data served from cache, no contract delta
2026-08-05 17:17:45 +01:00

41 lines
1.2 KiB
Go

package monitoring
import (
"testing"
"github.com/rcourtman/pulse-go-rewrite/internal/mock"
)
func TestCurrentUnifiedStateViewCachedBetweenMockTicks(t *testing.T) {
previous := mock.IsMockEnabled()
mustSetMockEnabled(t, true)
t.Cleanup(func() { mustSetMockEnabled(t, previous) })
m := &Monitor{}
first := m.currentUnifiedStateView()
if first.readState == nil {
t.Fatal("expected a read state from the mock branch")
}
second := m.currentUnifiedStateView()
if first.readState != second.readState {
t.Fatal("expected the cached view while the fixture data version is unchanged")
}
if len(first.resources) == 0 || len(second.resources) != len(first.resources) {
t.Fatalf("expected identical resource sets, got %d vs %d", len(first.resources), len(second.resources))
}
// Toggling mock mode rebuilds the fixture graph and advances the data
// version, which must invalidate the cached view.
mustSetMockEnabled(t, false)
mustSetMockEnabled(t, true)
third := m.currentUnifiedStateView()
if third.readState == nil {
t.Fatal("expected a read state after the fixture graph rebuilt")
}
if third.readState == first.readState {
t.Fatal("expected a rebuilt view after the fixture data version advanced")
}
}