Files
Pulse/internal/monitoring/resource_stale_thresholds_test.go
rcourtman f69860b153 fix(mock): keep PVE guest sightings fresh on slow mock ticks
The mock update loop sights PVE nodes and their guests through ten fixed
cohorts, sized for the default two-second tick (every node within 20s).
The count did not follow PULSE_MOCK_UPDATE_INTERVAL, so the public demo's
15s tick sighted each node's guests only every 150s while the registry
judges Proxmox sightings stale after 60s. Within minutes most running
guests read as warning/telemetry_stale with proxmox source "stale", and
the Proxmox overview's Attention chip climbed to 64 of 77 guests.

Rotate the PVE estate through the largest divisor of the cohort count
that still sights every node within 30s, down to every node per tick. A
divisor keeps the loop's cohort index mapping each node to one tick per
rotation, and per-second churn never exceeds the two-second default. For
ticks slower than that budget, derive the mock Proxmox stale threshold
from the actual sighting cadence (mock.ProxmoxRefreshInterval), never
below the configured PVE derivation, the same way 1d9e66b24c derived the
supplemental provider thresholds.

Mock-only: real PVE polling rebuilds every guest of an instance with the
poll's sample time, and its threshold already derives from the poll
interval or the adaptive scheduler's maximum.
2026-10-22 11:10:48 +01:00

113 lines
4.7 KiB
Go

package monitoring
import (
"testing"
"time"
"github.com/rcourtman/pulse-go-rewrite/internal/config"
"github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
)
func TestResourceStaleThresholdsDeriveMockSupplementalCadence(t *testing.T) {
supplementalSources := []unifiedresources.DataSource{
unifiedresources.SourceTrueNAS,
unifiedresources.SourceVMware,
unifiedresources.SourceAvailability,
}
t.Run("mock disabled leaves supplemental sources on registry defaults", func(t *testing.T) {
thresholds := resourceStaleThresholdsForConfig(nil, false, mockResourceCadences{supplemental: func() time.Duration {
t.Fatal("supplemental cadence must not be consulted while mock mode is off")
return 0
}})
for _, source := range supplementalSources {
if _, ok := thresholds[source]; ok {
t.Fatalf("unexpected %s threshold override with mock disabled", source)
}
}
})
t.Run("fast mock cadence keeps the default platform threshold", func(t *testing.T) {
thresholds := resourceStaleThresholdsForConfig(nil, true, mockResourceCadences{supplemental: func() time.Duration {
return 20 * time.Second
}})
for _, source := range supplementalSources {
if got := thresholds[source]; got != defaultPlatformResourceStaleThreshold {
t.Fatalf("%s threshold = %s, want default %s", source, got, defaultPlatformResourceStaleThreshold)
}
}
})
// The public demo runs PULSE_MOCK_UPDATE_INTERVAL well above the default;
// a fixed 120s threshold there flagged every TrueNAS/VMware/availability
// row as a stale source for most of each refresh cycle.
t.Run("slow mock cadence widens supplemental thresholds", func(t *testing.T) {
thresholds := resourceStaleThresholdsForConfig(nil, true, mockResourceCadences{supplemental: func() time.Duration {
return 300 * time.Second
}})
want := 600 * time.Second
for _, source := range supplementalSources {
if got := thresholds[source]; got != want {
t.Fatalf("%s threshold = %s, want %s", source, got, want)
}
}
})
t.Run("supplemental derivation leaves polled sources untouched", func(t *testing.T) {
base := resourceStaleThresholdsForConfig(nil, false, mockResourceCadences{})
derived := resourceStaleThresholdsForConfig(nil, true, mockResourceCadences{supplemental: func() time.Duration {
return 300 * time.Second
}})
for _, source := range []unifiedresources.DataSource{
unifiedresources.SourceProxmox,
unifiedresources.SourcePBS,
unifiedresources.SourcePMG,
} {
if base[source] != derived[source] {
t.Fatalf("%s threshold changed with mock enabled: %s -> %s", source, base[source], derived[source])
}
}
})
}
func TestResourceStaleThresholdsDeriveMockProxmoxCadence(t *testing.T) {
proxmoxCadence := func(cadence time.Duration) mockResourceCadences {
return mockResourceCadences{proxmox: func() time.Duration { return cadence }}
}
configured := resourceStaleThresholdsForConfig(nil, false, mockResourceCadences{})[unifiedresources.SourceProxmox]
t.Run("mock disabled ignores the mock Proxmox cadence", func(t *testing.T) {
thresholds := resourceStaleThresholdsForConfig(nil, false, mockResourceCadences{proxmox: func() time.Duration {
t.Fatal("Proxmox cadence must not be consulted while mock mode is off")
return 0
}})
if got := thresholds[unifiedresources.SourceProxmox]; got != configured {
t.Fatalf("Proxmox threshold = %s with mock disabled, want configured %s", got, configured)
}
})
// The public demo's 15s tick sights PVE guests every 30s, inside the
// configured threshold, so the threshold must not widen.
t.Run("cadence inside the budget keeps the configured threshold", func(t *testing.T) {
if got := resourceStaleThresholdsForConfig(nil, true, proxmoxCadence(30*time.Second))[unifiedresources.SourceProxmox]; got != configured {
t.Fatalf("Proxmox threshold = %s, want configured %s", got, configured)
}
})
// A tick slower than the budget sights every guest once per tick; a 60s
// tick judged by the 60s threshold flagged every guest stale on every
// tick boundary.
t.Run("slow tick derives the threshold from the sighting cadence", func(t *testing.T) {
if got, want := resourceStaleThresholdsForConfig(nil, true, proxmoxCadence(60*time.Second))[unifiedresources.SourceProxmox], 120*time.Second; got != want {
t.Fatalf("Proxmox threshold = %s, want %s", got, want)
}
})
t.Run("mock cadence never narrows a slower configured PVE interval", func(t *testing.T) {
cfg := &config.Config{PVEPollingInterval: 5 * time.Minute}
if got, want := resourceStaleThresholdsForConfig(cfg, true, proxmoxCadence(20*time.Second))[unifiedresources.SourceProxmox], 10*time.Minute; got != want {
t.Fatalf("Proxmox threshold = %s, want %s", got, want)
}
})
}