While no entry of the revision invokes a queue of a build seat (seat:<seat>.accept.<queue>, read by catalogue.QueueInvoke), the part composes no named worker, and at the first such pass it purges accept.<queue>.* of each build seat's stream and deletes the named workers, saying each in its journal; a failed read of the catalog half purges nothing. Its account alone is granted the stream's purge and the named workers' delete. The hand tool grants_named_purge records its hand act first, then asks the process to do it. Builds: design 47 G §544, §600, §601, A-231 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
97 lines
4.0 KiB
Go
97 lines
4.0 KiB
Go
package grants
|
|
|
|
import (
|
|
"errors"
|
|
"slices"
|
|
"testing"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
|
|
"mesh-grants/internal/buscompose"
|
|
)
|
|
|
|
type purged struct{ acts []string }
|
|
|
|
func (p *purged) PurgeSubject(stream, subject string) error {
|
|
p.acts = append(p.acts, "purge "+stream+" "+subject)
|
|
return nil
|
|
}
|
|
|
|
func (p *purged) DeleteConsumer(stream, name string) error {
|
|
p.acts = append(p.acts, "delete "+stream+"."+name)
|
|
return nil
|
|
}
|
|
|
|
type memStore map[string][]byte
|
|
|
|
func (m memStore) Get(state, key string) ([]byte, uint64, bool, error) {
|
|
v, ok := m[state+"/"+key]
|
|
return v, uint64(len(m)), ok, nil
|
|
}
|
|
func (m memStore) Put(state, key string, value []byte) (uint64, error) {
|
|
m[state+"/"+key] = value
|
|
return uint64(len(m)), nil
|
|
}
|
|
func (m memStore) Keys(string) ([]string, error) { return nil, nil }
|
|
func (m memStore) Delete(state, key string) error { delete(m, state+"/"+key); return nil }
|
|
|
|
// Design 47 G §544, A-231: at the first revision in which no entry invokes a queue of a build seat the part purges each
|
|
// queue's named asks and deletes its named worker, once, and again only after they were used and left again; a failed read
|
|
// of the catalog half purges nothing; grants_named_purge records its hand act first and then has the process purge.
|
|
func TestTheNamedAsksArePurgedAtTheFirstRevisionThatLeavesThem(t *testing.T) {
|
|
recs := buscompose.Records{Nodes: []string{"one"}, Assigned: map[string][]buscompose.Declared{"one": {{Module: "go-module",
|
|
Holds: []buscompose.Seat{{Name: "node-build-go", Scope: "node", Accepts: []string{"check"}, PulledByKind: true}}}}}}
|
|
unused := Inputs{Manifests: map[string]catalogue.Manifest{"runner": {Module: "runner", Invokes: []string{"seat:node-build-go.run"}}}}
|
|
used := Inputs{Manifests: map[string]catalogue.Manifest{"runner": {Module: "runner", Invokes: []string{"seat:node-build-go.accept.check"}}}}
|
|
unread := unused
|
|
unread.CatalogUnread = "the catalog half of the branch main"
|
|
bus := &purged{}
|
|
p := &Part{NamedPurge: bus, Store: memStore{}}
|
|
want := []string{"purge SEAT_NODE_BUILD_GO mesh.seat.node-build-go.accept.check.*",
|
|
"delete SEAT_NODE_BUILD_GO.SEAT_NODE_BUILD_GO_CHECK_named_worker"}
|
|
for i, c := range []struct {
|
|
in Inputs
|
|
byHand string
|
|
acts int
|
|
}{{unread, "", 0}, {used, "", 0}, {unused, "", 2}, {unused, "", 2}, {used, "", 2}, {unused, "", 4}, {used, "jochen@shanks", 6}} {
|
|
var res Result
|
|
p.purgeNamed(&res, c.in, recs, c.byHand)
|
|
if len(bus.acts) != c.acts {
|
|
t.Fatalf("pass %d: %d acts, want %d: %v", i, len(bus.acts), c.acts, bus.acts)
|
|
}
|
|
}
|
|
if !slices.Equal(bus.acts[:2], want) {
|
|
t.Errorf("purged %v, want %v", bus.acts[:2], want)
|
|
}
|
|
// The named workers are composed while an entry invokes a queue, and not while none does.
|
|
for _, c := range []struct {
|
|
in Inputs
|
|
named bool
|
|
}{{used, true}, {unused, false}, {unread, true}} {
|
|
d := buscompose.Declared{Holds: slices.Clone(recs.Assigned["one"][0].Holds)}
|
|
withNoNamedAsks(NamedAsksUnused(c.in), &d)
|
|
tr := buscompose.SeatTrafficOf("go-module", d.Holds, nil, nil)
|
|
has := slices.ContainsFunc(tr.Workers, func(w buscompose.Worker) bool { return w.Consumer == "SEAT_NODE_BUILD_GO_CHECK_named_worker" })
|
|
if has != c.named {
|
|
t.Errorf("a named worker composed %v, want %v (%v)", has, c.named, c.in.Manifests["runner"].Invokes)
|
|
}
|
|
}
|
|
// The hand tool: nothing is asked when the hand act is not recorded; once it is, the process purges at its ask.
|
|
if _, err := p.AskNamedPurge("jochen@shanks", "the part stopped first", func(string, string) error { return errors.New("down") }); err == nil {
|
|
t.Error("asked though the hand act was not recorded")
|
|
}
|
|
if a := p.asked(); a.NamedPurge {
|
|
t.Error("an ask was written though the hand act was not recorded")
|
|
}
|
|
var acts []string
|
|
if _, err := p.AskNamedPurge("jochen@shanks", "the part stopped first", func(what, why string) error {
|
|
acts = append(acts, what+": "+why)
|
|
return nil
|
|
}); err != nil || len(acts) != 1 {
|
|
t.Fatalf("the hand act: %v %v", err, acts)
|
|
}
|
|
if a := p.asked(); !a.NamedPurge || a.By != "jochen@shanks" {
|
|
t.Errorf("the ask read back: %+v", a)
|
|
}
|
|
}
|