State pins (4 MiB; one key per revision <branch>.<revision>, made, pins-stated) applied from a durable consumer of mesh-delivery's pinned, freed, heads-replaced and pins-stated, acknowledged after the apply; a revision is removed when no record pins it and it is not current, only while the consumer has nothing pending and after a pins-stated later than made; finding pin-missing; tools indexer_pins, indexer_unpin, indexer_consumer (the state consumers, written by mesh-sdk's process.ConsumerLedger). Builds: design 47 G §153, §178, §189, §215, §216, §579, A-169, A-171, A-176 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
122 lines
5.0 KiB
Go
122 lines
5.0 KiB
Go
package index
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery"
|
|
)
|
|
|
|
// Design 47 G §579, A-169, A-171: the indexer keeps the records pinning each catalog graph revision, counts a record once, and
|
|
// removes a revision only while its consumer is caught up and after a pins-stated later than made: one two records pin stays
|
|
// until both free it, one neither pinned nor current goes, a revision pinned and not held is said, and unpin clears it.
|
|
func TestTheIndexerRemovesARevisionOnlyWhenNoRecordPinsItAndItIsNotCurrent(t *testing.T) {
|
|
ctx := context.Background()
|
|
clock := time.Date(2026, 11, 12, 9, 0, 0, 0, time.UTC)
|
|
pending := uint64(0)
|
|
ix := &Indexer{Revisions: newMemObjects(1 << 20), Pins: newMem(), Findings: newFindings(), Now: func() time.Time { return clock },
|
|
PinsPending: func(context.Context) (uint64, error) { return pending, nil }}
|
|
reader := meshcatalog.CatalogEntry{Module: "reader", Repository: "novox/r", Manifest: map[string]any{}}
|
|
for _, commit := range []string{"c1", "c2", "c3"} {
|
|
e := reader
|
|
e.Commit = commit
|
|
if err := ix.KeepBranchEntries(ctx, "main", 5, map[string]meshcatalog.CatalogEntry{"reader": e}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
kept := func() []string {
|
|
n, _ := ix.Revisions.Names(ctx)
|
|
return slices.DeleteFunc(n, func(s string) bool { _, _, ok := RevisionGroup(s); return !ok })
|
|
}
|
|
say := func(subject string, body any) {
|
|
t.Helper()
|
|
raw, _ := json.Marshal(body)
|
|
if err := ix.HeardPins(ctx, subject, raw); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
pins := func(record string, revisions ...int64) meshdelivery.Pins {
|
|
p := meshdelivery.Pins{Record: record, Milestone: "47/G"}
|
|
for _, r := range revisions {
|
|
p.Catalog = append(p.Catalog, meshdelivery.PinsCatalogItem{Branch: "main", Revision: r})
|
|
}
|
|
return p
|
|
}
|
|
want := func(what string, names ...string) {
|
|
t.Helper()
|
|
if got := kept(); !slices.Equal(got, names) {
|
|
t.Errorf("%s: kept %v, want %v", what, got, names)
|
|
}
|
|
}
|
|
say(meshdelivery.SubjectPinned, pins("r1", 1))
|
|
say(meshdelivery.SubjectPinned, pins("r1", 1)) // heard twice: pins once
|
|
say(meshdelivery.SubjectPinned, pins("r2", 1))
|
|
ix.JudgePins(ctx)
|
|
want("before a pins-stated later than made is applied", "trunk.1", "trunk.2", "trunk.3")
|
|
clock = clock.Add(time.Second)
|
|
say(meshdelivery.SubjectPinsStated, meshdelivery.PinsStatedEvent{At: clock})
|
|
pending = 3
|
|
ix.JudgePins(ctx)
|
|
want("while messages of the consumer are pending", "trunk.1", "trunk.2", "trunk.3")
|
|
pending = 0
|
|
ix.JudgePins(ctx)
|
|
want("pinned by two records, the middle one neither pinned nor current", "trunk.1", "trunk.3")
|
|
say(meshdelivery.SubjectFreed, pins("r1", 1))
|
|
ix.JudgePins(ctx)
|
|
want("one of two records freed it", "trunk.1", "trunk.3")
|
|
say(meshdelivery.SubjectFreed, pins("r2", 1))
|
|
ix.JudgePins(ctx)
|
|
want("both freed it", "trunk.3")
|
|
if keys, _ := ix.Pins.Keys(ctx); slices.Contains(keys, "main.1") {
|
|
t.Errorf("the state holds %v, want no key of the removed revision", keys)
|
|
}
|
|
// A record pins a revision no longer held: its key says missing and the finding names it; unpin clears both.
|
|
say(meshdelivery.SubjectPinned, pins("r3", 1))
|
|
if f := ix.Findings.Held(); len(f) != 1 || f[0].Kind != PinMissingKind {
|
|
t.Fatalf("findings %v, want one %s", f, PinMissingKind)
|
|
}
|
|
if r := ix.Unpin(ctx, "main.1", "r3", "operator", "freed was lost"); !r.Stored {
|
|
t.Fatalf("unpin answered %q", r.Answer)
|
|
}
|
|
if f := ix.Findings.Held(); len(f) != 0 {
|
|
t.Errorf("findings %v after unpin, want none", f)
|
|
}
|
|
}
|
|
|
|
func TestAPinsStatedBeforeTheStateWasMadeCountsNothing(t *testing.T) {
|
|
ctx := context.Background()
|
|
clock := time.Date(2026, 11, 12, 9, 0, 0, 0, time.UTC)
|
|
ix := &Indexer{Revisions: newMemObjects(1 << 20), Pins: newMem(), Now: func() time.Time { return clock },
|
|
PinsPending: func(context.Context) (uint64, error) { return 0, nil }}
|
|
raw, _ := json.Marshal(meshdelivery.PinsStatedEvent{At: clock.Add(-time.Hour)})
|
|
if err := ix.HeardPins(ctx, meshdelivery.SubjectPinsStated, raw); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ok, err := ix.pinsCounted(ctx); err != nil || ok {
|
|
t.Errorf("counted %v, %v after a pins-stated older than made, want not counted", ok, err)
|
|
}
|
|
}
|
|
|
|
func TestHeadsReplacedDropsTheCandidatesGraph(t *testing.T) {
|
|
ctx := context.Background()
|
|
ix := &Indexer{Revisions: newMemObjects(1 << 20), Pins: newMem()}
|
|
rev, objects, err := CatalogRevisionOf(meshcatalog.CatalogRevision{Kind: "candidate", Group: "feat-x", Heads: "0123456789ab", Revision: 1}, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := KeepRevision(ctx, ix.Revisions, rev, objects); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw, _ := json.Marshal(meshdelivery.HeadsReplacedEvent{Group: "feat-x", Heads: "0123456789ab", Repository: "novox/r", Number: 1})
|
|
if err := ix.HeardPins(ctx, meshdelivery.SubjectHeadsReplaced, raw); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n, _ := ix.Revisions.Names(ctx); len(n) != 0 {
|
|
t.Errorf("the objects hold %v, want the candidate's graph dropped", n)
|
|
}
|
|
}
|