Files
Pulse/internal/unifiedresources/monitor_adapter_read_refresh_test.go
rcourtman b32a080060 Serve state reads without queueing behind registry rebuilds
Every /api/state request and websocket hydrate ran a full unified
registry rebuild while holding the adapter's mutation lock, including
synchronous SQLite writes for change records and identity pins plus
re-reads of overrides and pins on registry construction. On slow
volumes a single rebuild holds the lock for the duration of those
transactions, and every state read queues behind it. #1665 hit exactly
this with the data dir on NFS, where the same instance's metrics logs
show single commits taking 30+ seconds, and the UI sat on /api/state
for minutes.

Read paths now refresh through TryReplaceRegistryForRead. It skips
while the current generation is younger than two seconds, collapsing
same-cycle rebuild storms, and it never queues behind an in-flight
ingest rebuild since that rebuild is already publishing a generation
at least as fresh. Consume-once supplemental payloads are only drained
once a rebuild commits. Cold start still blocks and builds the first
generation so a fresh session cannot render empty. Ingest boundaries
keep rebuilding eagerly through the unchanged populate methods.

Contract-Neutral: internal registry rebuild scheduling on the state read path; no wire contract or payload change
2026-08-02 12:34:50 +01:00

127 lines
3.8 KiB
Go

package unifiedresources
import (
"testing"
"time"
"github.com/rcourtman/pulse-go-rewrite/internal/models"
)
func readRefreshSnapshot(name string) models.StateSnapshot {
return models.StateSnapshot{
LastUpdate: time.Now().UTC(),
VMs: []models.VM{{
ID: "lab:node-a:101",
VMID: 101,
Name: name,
Node: "node-a",
Instance: "lab",
Status: "running",
LastSeen: time.Now().UTC(),
}},
}
}
func TestTryReplaceRegistryForReadColdStartBlocksAndBuilds(t *testing.T) {
adapter := NewMonitorAdapter(NewRegistry(NewMemoryStore()))
supplied := false
rebuilt := adapter.TryReplaceRegistryForRead(readRefreshSnapshot("database"), time.Minute, func() map[DataSource][]IngestRecord {
supplied = true
return nil
})
if !rebuilt {
t.Fatal("cold-start read refresh must build the first generation")
}
if !supplied {
t.Fatal("committed rebuild must materialize the supplemental payload")
}
if adapter.LastRebuiltAt().IsZero() {
t.Fatal("rebuild must publish a generation timestamp")
}
if len(adapter.GetAll()) != 1 {
t.Fatalf("expected 1 resource after cold-start build, got %d", len(adapter.GetAll()))
}
}
func TestTryReplaceRegistryForReadSkipsFreshGeneration(t *testing.T) {
adapter := NewMonitorAdapter(NewRegistry(NewMemoryStore()))
adapter.PopulateFromSnapshot(readRefreshSnapshot("database"))
rebuilt := adapter.TryReplaceRegistryForRead(readRefreshSnapshot("renamed"), time.Minute, func() map[DataSource][]IngestRecord {
t.Fatal("skipped refresh must not drain the supplemental payload")
return nil
})
if rebuilt {
t.Fatal("read refresh must skip while the generation is fresh")
}
if got := adapter.GetAll(); len(got) != 1 || got[0].Name != "database" {
t.Fatalf("fresh generation must be served unchanged, got %+v", got)
}
}
func TestTryReplaceRegistryForReadRebuildsStaleGeneration(t *testing.T) {
adapter := NewMonitorAdapter(NewRegistry(NewMemoryStore()))
adapter.PopulateFromSnapshot(readRefreshSnapshot("database"))
time.Sleep(20 * time.Millisecond)
rebuilt := adapter.TryReplaceRegistryForRead(readRefreshSnapshot("renamed"), 10*time.Millisecond, nil)
if !rebuilt {
t.Fatal("read refresh must rebuild once the generation is older than maxAge")
}
if got := adapter.GetAll(); len(got) != 1 || got[0].Name != "renamed" {
t.Fatalf("stale refresh must publish the new snapshot, got %+v", got)
}
}
func TestTryReplaceRegistryForReadDoesNotQueueBehindInflightRebuild(t *testing.T) {
store := &blockingChangeStore{
MemoryStore: NewMemoryStore(),
entered: make(chan struct{}),
release: make(chan struct{}),
}
adapter := NewMonitorAdapter(NewRegistry(store))
// Publish a baseline generation so the read path is past cold start. An
// empty snapshot records no changes, leaving the store helper armed for
// the ingest rebuild below.
adapter.PopulateFromSnapshot(models.StateSnapshot{LastUpdate: time.Now().UTC()})
if adapter.LastRebuiltAt().IsZero() {
t.Fatal("baseline rebuild must publish a generation")
}
ingestDone := make(chan struct{})
go func() {
adapter.PopulateFromSnapshot(readRefreshSnapshot("database"))
close(ingestDone)
}()
select {
case <-store.entered:
case <-time.After(time.Second):
t.Fatal("ingest rebuild did not reach change publication")
}
readDone := make(chan bool, 1)
go func() {
readDone <- adapter.TryReplaceRegistryForRead(readRefreshSnapshot("read"), time.Nanosecond, func() map[DataSource][]IngestRecord {
return nil
})
}()
select {
case rebuilt := <-readDone:
if rebuilt {
t.Fatal("read refresh must not rebuild while an ingest rebuild is in flight")
}
case <-time.After(time.Second):
t.Fatal("read refresh queued behind the in-flight ingest rebuild")
}
close(store.release)
select {
case <-ingestDone:
case <-time.After(time.Second):
t.Fatal("ingest rebuild did not complete after release")
}
}