Files
beacon-server/db/cmi_verify_integration_test.go
MrAlders0n 5ded8d925f Channel messages by site: a read model that serves region and scope pages while trusted (#246)
* feat(db): channel_message_iatas table and indexes

* feat(ingest): record channel message sites at insert and on every later hearing

* feat(db): channel message site backfill and two-range reconcile

* feat(db): serve region and scope message pages from the site read model when trusted

* feat: boot repair, periodic reconcile and first-time verification for the message site read model

* test(planguard): channel message site statements, diff and storediff cases

* feat(background): log when a read-only server trusts the message site read model

* test(planguard): storediff vars in query values, and message cases read from the data

* fix(ingest): record a stored hearing's channel message site right after the observation insert

* fix: bounded message dispatch for filtered all-channel pages, fresher read-only trust, earlier invalidation

* fix: record a hearing's site after its path is marked and skip the backstop when the message was found; read-only trust ends at the writer's deadline
2026-10-19 21:20:21 -04:00

99 lines
3.5 KiB
Go

// Copyright 2026 Beacon Contributors
// SPDX-License-Identifier: AGPL-3.0-or-later
package db
import (
"context"
"testing"
"time"
sqlc "github.com/MeshCore-Beacon/beacon-server/db/sqlc"
"github.com/jackc/pgx/v5/pgxpool"
)
func TestCmiFirstVerificationPostgres(t *testing.T) {
t.Run("empty", func(t *testing.T) {
f := newCmiFixture(t)
c := f.begin()
if _, err := c.Init(f.ctx); err != nil {
t.Fatal(err)
}
v, err := c.Verify(f.ctx)
if err != nil || !v.OK() || v.PagesCompared != 0 {
t.Fatalf("Verify = %+v, %v", v, err)
}
})
f := newCmiFixture(t)
buildCmiMessages(f)
f.exec("TRUNCATE channel_message_iatas") // as on a first install: the backfill fills it
c := f.begin()
if _, err := c.Init(f.ctx); err != nil {
t.Fatal(err)
}
if _, err := c.Backfill(f.ctx, time.Minute); err != nil {
t.Fatal(err)
}
v, err := c.Verify(f.ctx)
if err != nil || !v.OK() || v.PagesCompared < 10 {
t.Fatalf("Verify = %+v, %v", v, err)
}
first := c.State().FirstVerifiedAt
var loaded CmiState
if _, err := f.store.LoadState(f.ctx, cmiStateKey, &loaded); err != nil || first == nil || loaded.FirstVerifiedAt == nil || !loaded.FirstVerifiedAt.Equal(*first) {
t.Fatalf("first_verified_at %v persisted as %+v (%v)", first, loaded, err)
}
var reltuples float64
if err := f.pool.QueryRow(f.ctx, "SELECT reltuples FROM pg_class WHERE oid = 'channel_message_iatas'::regclass").Scan(&reltuples); err != nil || reltuples <= 0 {
t.Fatalf("reltuples = %v (%v): no ANALYZE", reltuples, err)
}
// Damage below the watermarks, where no reconcile looks: the newest message of the busiest sampled pair loses
// a site.
samples, err := f.store.q.ListChannelMessageIATASamples(f.ctx, sqlc.ListChannelMessageIATASamplesParams{AfterID: 0, RowLimit: 1})
if err != nil || len(samples) != 1 {
t.Fatalf("samples %v, %v", samples, err)
}
f.exec(`DELETE FROM channel_message_iatas WHERE (message_id, iata) = (
SELECT message_id, iata FROM channel_message_iatas WHERE channel_id = $1 AND iata = $2 ORDER BY message_id DESC LIMIT 1)`, samples[0].ChannelID, samples[0].Iata)
v, err = c.Verify(f.ctx)
if err != nil || v.Missing != 1 || v.Extra != 0 || len(v.PageDiffs) == 0 || v.OK() {
t.Fatalf("damaged Verify = %+v, %v", v, err)
}
if got := c.State().FirstVerifiedAt; got == nil || !got.Equal(*first) {
t.Fatalf("first_verified_at moved: %v -> %v", first, got)
}
t.Logf("diffs: %v", v.PageDiffs)
// Repair, then the read-only check on a read-only pool.
f.exec("TRUNCATE channel_message_iatas")
f.exec("DELETE FROM background_state WHERE key = 'cmi'")
c.Close(f.ctx)
c = f.begin()
if _, err := c.Init(f.ctx); err != nil {
t.Fatal(err)
}
if _, err := c.Backfill(f.ctx, time.Minute); err != nil {
t.Fatal(err)
}
if v, err := c.Verify(f.ctx); err != nil || !v.OK() {
t.Fatalf("Verify after the rebuild = %+v, %v", v, err)
}
cfg := f.pool.Config().Copy()
cfg.ConnConfig.RuntimeParams["default_transaction_read_only"] = "on"
ro, err := pgxpool.NewWithConfig(context.Background(), cfg)
if err != nil {
t.Fatal(err)
}
defer ro.Close()
rc, err := New(ro, ro, 0, 0).CheckCmiReadOnly(f.ctx)
if err != nil || !rc.Found || !rc.Verification.OK() || !rc.State.Complete || rc.State.FirstVerifiedAt == nil || rc.Age < 0 || rc.Age > time.Minute {
t.Fatalf("CheckCmiReadOnly = %+v, %v", rc, err)
}
f.exec("UPDATE background_state SET updated_at = now() - interval '11 minutes' WHERE key = 'cmi'")
if rc, err = New(ro, ro, 0, 0).CheckCmiReadOnly(f.ctx); err != nil || rc.Age < 11*time.Minute || rc.Age > 12*time.Minute {
t.Fatalf("age after 11 minutes = %v, %v", rc.Age, err)
}
}