test: pin mixed-slot supersession and independent expiry

This commit is contained in:
pingqiu committed 2026-11-22 17:18:19 -08:00
1 parent f305ecd6cf
commit d06879b76c
2 files changed
+53 -6

No files matched your search

@@ -10,7 +10,7 @@ import (
// ============================================================
// P14 S4 — Observation Store
//
// Push-ingested latest-wins observation store per ServerID. The
// Push-ingested latest-wins observation store per replica slot. The
// store computes derived freshness on read, based on wall-clock
// time vs ObservedAt + FreshnessWindow. Expired observations stay
// visible; they are just semantically ineligible for supported
@@ -231,11 +231,10 @@ var ErrInvalidObservation = errors.New("observation_store: invalid observation")
// ExpiresAt, and fires the registered mutation callback if
// anything changed.
//
// Latest-wins per ServerID. If an incoming observation has an
// ObservedAt strictly older than the currently-stored one from
// the same server, the store keeps the newer one (ignores the
// stale ingest). No error is returned for that case; it's a
// routine ordering event, not a bug.
// Latest-wins per (ServerID, volume, replica). A slot report strictly
// older than that slot's stored fact is ignored; other slots in the
// observation are accepted independently. No error is returned for
// superseded reports.
func (s *ObservationStore) Ingest(obs Observation) error {
_, err := s.ingest(obs)
return err
@@ -40,3 +40,51 @@ func TestObservationStore_InterleavedSendersOrderPerSlot(t *testing.T) {
})
}
}
func TestObservationStore_MixedSlotReportKeepsOnlyNewerFacts(t *testing.T) {
base := time.Unix(1000, 0)
now := base.Add(4 * time.Second)
s := NewObservationStore(FreshnessConfig{FreshnessWindow: 10 * time.Second}, func() time.Time { return now })
report := func(at time.Duration, slots ...SlotFact) []SlotHealthTransition {
t.Helper()
tr, err := s.IngestWithHealthTransitions(Observation{ServerID: "one-host", ObservedAt: base.Add(at), Slots: slots})
if err != nil {
t.Fatal(err)
}
return tr
}
a, b, c := healthySlot("a", "r1"), healthySlot("b", "r1"), healthySlot("c", "r1")
report(3*time.Second, a)
report(time.Second, b)
a.UnhealthyReason, b.UnhealthyReason, c.UnhealthyReason = "stale", "new-b", "new-c"
mutations := 0
s.SetOnMutation(func() { mutations++ })
tr := report(2*time.Second, a, b, c)
if len(tr) != 2 || tr[0].VolumeID != "b" || tr[0].New != "new-b" || tr[1].VolumeID != "c" || tr[1].New != "new-c" {
t.Fatalf("mixed report transitions=%+v; want only b and c", tr)
}
if got, ok := s.SlotFactOnServer("one-host", "a", "r1"); !ok || got.UnhealthyReason != "" {
t.Fatalf("stale a replaced its newer fact: %+v, %v", got, ok)
}
if s.SupersededCount() != 1 || mutations != 1 {
t.Fatalf("mixed report: superseded=%d mutations=%d; want 1/1", s.SupersededCount(), mutations)
}
rev := s.Snapshot().revision
if tr := report(time.Second, a, b, c); len(tr) != 0 {
t.Fatalf("all-stale report emitted transitions: %+v", tr)
}
if s.SupersededCount() != 4 || mutations != 1 || s.Snapshot().revision != rev {
t.Fatalf("all-stale report: superseded=%d mutations=%d revision=%d; want 4/1/%d", s.SupersededCount(), mutations, s.Snapshot().revision, rev)
}
// The server's timestamp stays at its newest report; each accepted slot
// keeps its own expiry, including after a delayed mixed report.
now = base.Add(12500 * time.Millisecond)
if _, ok := s.SlotFactOnServer("one-host", "a", "r1"); !ok {
t.Fatal("a lost its later expiry to another slot's older report")
}
for _, volume := range []string{"b", "c"} {
if _, ok := s.SlotFactOnServer("one-host", volume, "r1"); ok {
t.Fatalf("%s inherited a's later freshness", volume)
}
}
}