mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-09-23 11:46:28 +00:00
518a5e2294
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
60 lines
1.5 KiB
Go
60 lines
1.5 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)
|
|
|
|
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")
|
|
}
|
|
}
|