Files
mesh-controller/cmd/mesh-controller/declaration_sweep_wanted_test.go
T
jschoubben 76c48fa074 Keep the files of every composition a want still names, not only each key's newest (issue 1945; the review's C1)
A node applies exactly the composition its want names (ADR 0539 clause 1), which may be a revision a newer one
replaced: its files over 64 KiB stay until no want names it.
2026-10-24 18:24:19 +02:00

44 lines
2.0 KiB
Go

package main
import (
"encoding/json"
"strings"
"testing"
sdkdelivery "git.novox.be/novox/mesh-sdk/go/delivery"
"github.com/nats-io/nats.go/jetstream"
)
// 1945 (the review's C1): the sweep keeps the files of a key's newest composition and of every older revision a want
// of the node still names, so a node told to apply a replaced revision finds its files; an older revision no want names,
// and a deleted key, keep nothing.
func TestTheSweepKeepsWhatAWantStillNames(t *testing.T) {
revision := func(content string, rev uint64, op jetstream.KeyValueOp) fakeComposedEntry {
c := sdkdelivery.Composed{Node: "ace", Module: "m", FP: sdkdelivery.SourcePrefix2 + strings.Repeat("a", 64),
Resources: []json.RawMessage{json.RawMessage(`{"id":"m.f","type":"file","module":"m","path":"/etc/m","content":"` + content + `"}`)}}
raw, err := sdkdelivery.EncodeComposed(c)
if err != nil {
t.Fatal(err)
}
return fakeComposedEntry{key: "k", value: raw, rev: rev, op: op}
}
digestOfEntry := func(e fakeComposedEntry) string {
c, _ := sdkdelivery.DecodeComposed(e.value)
d, _ := sdkdelivery.ComposedDigestOf(c)
return d
}
one, two, three := revision("one", 1, jetstream.KeyValuePut), revision("two", 2, jetstream.KeyValuePut), revision("three", 3, jetstream.KeyValuePut)
history := []jetstream.KeyValueEntry{one, two, three}
kept := keptRevisions(history, map[string]bool{digestOfEntry(one): true})
if len(kept) != 2 || string(kept[0]) != string(one.value) || string(kept[1]) != string(three.value) {
t.Errorf("kept %d revisions, want the wanted first and the newest", len(kept))
}
if kept := keptRevisions(history, map[string]bool{}); len(kept) != 1 {
t.Errorf("with no want, kept %d revisions, want the newest alone", len(kept))
}
deleted := []jetstream.KeyValueEntry{one, fakeComposedEntry{key: "k", rev: 2, op: jetstream.KeyValueDelete}}
if kept := keptRevisions(deleted, map[string]bool{}); len(kept) != 0 {
t.Errorf("a deleted key kept %d revisions", len(kept))
}
}