Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/pins_test.go
T
jschoubben 5dba7fe68b mesh-catalog's revision names its base; validate's case before reads it, not the number before
A take-in that moves nothing keeps no revision for its source number, so validate's case before,
read as the step's revision minus one, named a revision never kept and could not run. The take-in
now writes, on the catalog revision it keeps, the field base: the newest kept revision below it,
the one it pins for its pipeline, in the same write; absent on the branch's first take-in. The
assigner's validate reads the base from the revision it already asks for; where the revision names
none the case before is empty and its end says so, never a could-not-run.

- mesh-delivery-contracts: the seat mesh-catalog's CatalogRevision gains base (schema, fixture,
  generated Go and TypeScript by seatgen).
- mesh-delivery-indexer: keepBranchEntries names the base it pins; the pin test's rows check it.
- mesh-delivery-assigner: before reads the base; the validate test asks a revision with none, and
  both stand-ins name the base as the take-in does.

Builds: design 47 H-A-151, H-A-154 (hq design/47-H1-answers-59 01842bbe), H-A-147, H-A-108 (3), design 47 H §743
2026-11-20 17:00:55 +01:00

176 lines
7.5 KiB
Go

package index
import (
"context"
"encoding/json"
"fmt"
"slices"
"testing"
"time"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
runner "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
)
// Design 47 H §749 (G §579, A-169, A-171): the indexer pins a revision on a pipeline-started that names it and frees it on its
// pipeline-ended, counts a record once, and removes a revision only while its consumers are caught up, judged against the pins
// its own state holds, never a count of pins stated: one two pipelines 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{Module: testSelf, Revisions: newMemObjects(1 << 20), Pins: newMem(), Findings: newFindings(), Now: func() time.Time { return clock },
PinsPending: func(context.Context) (uint64, error) { return pending, nil }}
kept := func() []string {
n, _ := ix.Revisions.Names(ctx)
return slices.DeleteFunc(n, func(s string) bool { _, _, ok := RevisionGroup(s); return !ok })
}
started := func(pipeline string, revision int64) {
t.Helper()
if err := ix.HeardPins(ctx, &runner.PipelineStarted{Branch: "main", Pipeline: pipeline, Revision: revision}, nil); err != nil {
t.Fatal(err)
}
}
ended := func(pipeline string, revision int64) {
t.Helper()
if err := ix.HeardPins(ctx, nil, &runner.PipelineEnded{Branch: "main", Pipeline: pipeline, Revision: revision}); err != nil {
t.Fatal(err)
}
}
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)
}
}
reader := meshcatalog.CatalogEntry{Module: "reader", Repository: "novox/r", Manifest: map[string]any{}}
keep := func(commit string, source int64) {
t.Helper()
e := reader
e.Commit = commit
if err := ix.KeepBranchEntries(ctx, "main", source, map[string]meshcatalog.CatalogEntry{"reader": e}, nil); err != nil {
t.Fatal(err)
}
}
keep("c1", 1)
pending = 3 // pinned before the next keeps, which judge the revisions while the consumers are behind
started("main.5", 1)
started("main.5", 1) // heard twice: pins once
started("main.6", 1)
keep("c2", 2)
keep("c3", 3)
ix.JudgePins(ctx)
want("while messages of the consumer are pending", "main.1", "main.2", "main.3")
pending = 0
ix.JudgePins(ctx)
want("pinned by two records, the middle one neither pinned nor current", "main.1", "main.3")
if v, _, _, _ := ix.Pins.Get(ctx, "main.1"); !slices.Equal(mustRecords(t, v), []string{"pipeline.main.5", "pipeline.main.6"}) {
t.Errorf("the pin of main.1 holds %s, want the two pipelines' records once each", v)
}
ended("main.5", 1)
ix.JudgePins(ctx)
want("one of two records freed it", "main.1", "main.3")
ended("main.6", 1)
ix.JudgePins(ctx)
want("both freed it", "main.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; indexer_pin_drop of its key
// (design 47 H §765, H-A-57) drops the pin and clears both.
started("main.7", 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.PinDrop(ctx, "main.1", "operator", "names no running pipeline"); !r.Stored {
t.Fatalf("the pin drop answered %q", r.Answer)
}
if f := ix.Findings.Held(); len(f) != 0 {
t.Errorf("findings %v after the pin drop, want none", f)
}
if keys, _ := ix.Pins.Keys(ctx); slices.Contains(keys, "main.1") {
t.Errorf("the state holds %v after the pin drop, want no main.1", keys)
}
}
func mustRecords(t *testing.T, v []byte) []string {
t.Helper()
var pv PinValue
if err := json.Unmarshal(v, &pv); err != nil {
t.Fatal(err)
}
slices.Sort(pv.Records)
return pv.Records
}
// H-A-147 (§743, §749, H-A-108 (3)): a take-in with a mover pins, in its write, the branch's previous revision (the newest kept
// before the write) for the same pipeline record as its own, so the validate step's case before can read it while the pipeline
// runs, and names it as its base on the revision it writes (H-A-151, H-A-154); a take-in with no revision before it pins only
// its own and names no base; the pipeline's end frees both.
func TestATakeInPinsTheBranchsPreviousRevisionForItsPipeline(t *testing.T) {
ctx := context.Background()
reader := meshcatalog.CatalogEntry{Module: "reader", Repository: "novox/r", Manifest: map[string]any{}}
mover := []meshcatalog.TakeInEntry{{Module: "reader", Commit: "c", Fingerprint: "f", Repository: "novox/r"}}
for _, tc := range []struct {
name string
before []int64 // sources taken in with no mover before the take-in of source 5
whileRuns []string
afterTheEnd []string
base int64 // the base main.5 names (H-A-154)
}{
{name: "the previous revision kept and pinned", before: []int64{3, 4}, whileRuns: []string{"main.4", "main.5"}, afterTheEnd: []string{"main.5"}, base: 4},
{name: "the newest kept before, whatever its number", before: []int64{2}, whileRuns: []string{"main.2", "main.5"}, afterTheEnd: []string{"main.5"}, base: 2},
{name: "no revision before it pins only its own", whileRuns: []string{"main.5"}, afterTheEnd: []string{"main.5"}},
} {
t.Run(tc.name, func(t *testing.T) {
ix := &Indexer{Module: testSelf, Revisions: newMemObjects(1 << 20), Pins: newMem(), Findings: newFindings(),
Now: func() time.Time { return time.Date(2026, 11, 20, 16, 0, 0, 0, time.UTC) },
PinsPending: func(context.Context) (uint64, error) { return 0, nil }}
keep := func(source int64, movers []meshcatalog.TakeInEntry) {
t.Helper()
e := reader
e.Commit = fmt.Sprintf("c%d", source)
if err := ix.KeepBranchEntries(ctx, "main", source, map[string]meshcatalog.CatalogEntry{"reader": e}, movers); err != nil {
t.Fatal(err)
}
}
kept := func() []string {
n, _ := ix.Revisions.Names(ctx)
n = slices.DeleteFunc(n, func(s string) bool { _, _, ok := RevisionGroup(s); return !ok })
slices.Sort(n)
return n
}
for _, s := range tc.before {
keep(s, nil)
}
keep(5, mover)
if rev, _, err := ReadRevision(ctx, ix.Revisions, "main.5"); err != nil || rev.Base != tc.base {
t.Errorf("main.5 names the base %d (%v), want %d, the revision its take-in pinned (H-A-154)", rev.Base, err, tc.base)
}
if got := kept(); !slices.Equal(got, tc.whileRuns) {
t.Errorf("while the pipeline runs: kept %v, want %v", got, tc.whileRuns)
}
for _, k := range tc.whileRuns {
if v, _, _, _ := ix.Pins.Get(ctx, k); !slices.Equal(mustRecords(t, v), []string{"pipeline.main.5"}) {
t.Errorf("the pin of %s holds %s, want the pipeline's record", k, v)
}
}
if err := ix.HeardPins(ctx, nil, &runner.PipelineEnded{Branch: "main", Pipeline: "main.5", Revision: 5}); err != nil {
t.Fatal(err)
}
ix.JudgePins(ctx)
if got := kept(); !slices.Equal(got, tc.afterTheEnd) {
t.Errorf("after the pipeline's end: kept %v, want %v", got, tc.afterTheEnd)
}
if keys, _ := ix.Pins.Keys(ctx); len(keys) > 0 {
for _, k := range keys {
if v, _, _, _ := ix.Pins.Get(ctx, k); len(mustRecords(t, v)) > 0 {
t.Errorf("after the pipeline's end the pin of %s holds %s, want none", k, v)
}
}
}
})
}
}