node-tools: the runner makes its twin from the membership's spec, removes it when withdrawn

The grants part no longer composes the tool runner's `_tools` twin: at a child's
first subscribe the runner creates or updates `<node>_<module>_tools` on EVENTS and
on every heard stream from the spec the membership's entry carries (filters, ack
wait, max deliver, max ack pending, deliver policy), and binds by name an entry an
older grants part wrote without one. When the grants part withdraws the module's
membership on this node, the runner removes the twins from the streams the last
entry named and makes none again until a membership is issued; it removes nothing
at its start. The heard entry is mesh-sdk's type, so the hearing keys its loops by
stream and consumer.

Builds: design 47 H2 H-A-472 (fifth, sixth, seventh, eighth and ninth addenda)
This commit is contained in:
2026-11-22 19:35:08 +01:00
parent 51a24ea801
commit ba82044cc0
3 changed files with 264 additions and 31 deletions
+118
View File
@@ -4,11 +4,15 @@ import (
"encoding/json"
"errors"
"fmt"
"slices"
"strings"
"sync"
"testing"
"time"
"github.com/nats-io/nats.go"
"git.novox.be/novox/mesh-sdk/go/membership"
"github.com/novox/mesh-tools/node-tools/internal/bus"
mt "github.com/novox/mesh-tools/node-tools/internal/meshtest"
)
@@ -368,3 +372,117 @@ func (l *syncLogs) Has(fragment string) bool {
}
return false
}
// Design 47 H2, H-A-472 fifth to ninth addenda: the runner makes its own twin `<node>_<module>_tools` of every stream its
// membership hears, EVENTS among them, from the spec the entry carries, at a child's first subscribe; a twin it holds is
// brought to the spec at the next; and when the grants part withdraws the membership, the runner removes the twins from
// the streams the entry named and makes none again.
func TestTheRunnersTwinIsMadeFromItsMembershipsSpecAndRemovedWhenWithdrawn(t *testing.T) {
reader := mt.MembershipOf("reader", "anchor", false, nil)
reader.Hears = []bus.HeardIssued{
{Stream: bus.EventsStream, Consumer: "anchor_reader", Filters: []string{"mesh.mod.alpha.event.>", "mesh.again.anchor_reader_tools.>"},
AckWaitSeconds: 7, MaxDeliver: 3, DeliverPolicy: membership.DeliverFromNow},
{Stream: "MAIL", Consumer: "anchor_reader", Filters: []string{"mesh.stream.mail.event.mail.>"}, AckWaitSeconds: 9,
MaxDeliver: 4, MaxAckPending: 2, DeliverPolicy: membership.DeliverAll},
}
mesh, conn := streamMesh(t, reader)
nc, err := nats.Connect(mt.URL(t))
if err != nil {
t.Fatal(err)
}
t.Cleanup(nc.Close)
js, _ := nc.JetStream()
// Waiting on MAIL before the twin exists: a twin that delivers all hears it.
mesh.Emit(t, "mesh.stream.mail.event.mail.home.message.added", map[string]any{"n": 1})
var mu sync.Mutex
heard := map[string]int{}
consume := func() (func(), error) {
return conn.ConsumeAs("reader", func(env bus.Envelope) error {
mu.Lock()
heard[env.Key]++
mu.Unlock()
return nil
})
}
stop, err := consume()
if err != nil {
t.Fatal(err)
}
mesh.Emit(t, "mesh.mod.alpha.event.happened", map[string]any{"n": 1})
mt.Until(t, func() error {
mu.Lock()
defer mu.Unlock()
if heard["alpha.happened"] != 1 || heard["mail.mail.home.message.added"] != 1 {
return fmt.Errorf("heard %v, want one on EVENTS and one on MAIL", heard)
}
return nil
})
for _, row := range []struct {
stream string
filters []string
ackWait time.Duration
maxDeliver int
maxAckPending int
deliver nats.DeliverPolicy
}{
{bus.EventsStream, []string{"mesh.mod.alpha.event.>", "mesh.again.anchor_reader_tools.>"}, 7 * time.Second, 3, 0, nats.DeliverNewPolicy},
{"MAIL", []string{"mesh.stream.mail.event.mail.>"}, 9 * time.Second, 4, 2, nats.DeliverAllPolicy},
} {
info, err := js.ConsumerInfo(row.stream, "anchor_reader_tools")
if err != nil {
t.Fatalf("the twin on %s: %v", row.stream, err)
}
c := info.Config
if !slices.Equal(c.FilterSubjects, row.filters) || c.AckWait != row.ackWait || c.MaxDeliver != row.maxDeliver ||
(row.maxAckPending != 0 && c.MaxAckPending != row.maxAckPending) || c.DeliverPolicy != row.deliver {
t.Errorf("the twin on %s is %+v, want the membership's spec", row.stream, c)
}
}
// A twin held is brought to the spec at the next first subscribe.
stop()
reader.Hears[1].MaxDeliver = 6
mesh.Issue(t, reader)
mt.Until(t, func() error {
if m := conn.Membership("reader"); m == nil || m.Hears[1].MaxDeliver != 6 {
return errors.New("the new spec is not issued yet")
}
return nil
})
if stop, err = consume(); err != nil {
t.Fatal(err)
}
mt.Until(t, func() error {
info, err := js.ConsumerInfo("MAIL", "anchor_reader_tools")
if err != nil || info.Config.MaxDeliver != 6 {
return fmt.Errorf("the twin on MAIL is not brought to the spec: %v", err)
}
return nil
})
// Withdrawn: the twins go from both streams, and a subscribe after makes none.
kv, err := js.KeyValue(membership.GrantsBucket)
if err != nil {
t.Fatal(err)
}
if err := kv.Delete(membership.GrantsKey("anchor", "reader")); err != nil {
t.Fatal(err)
}
mt.Until(t, func() error {
for _, stream := range []string{bus.EventsStream, "MAIL"} {
if _, err := js.ConsumerInfo(stream, "anchor_reader_tools"); !errors.Is(err, nats.ErrConsumerNotFound) {
return fmt.Errorf("the twin on %s is still there (%v)", stream, err)
}
}
return nil
})
stop()
if !conn.Withdrawn("reader") {
t.Error("the module is not marked withdrawn")
}
if again, err := consume(); err == nil {
again()
t.Error("a subscribe after the withdrawal made the twin again")
}
}