diff --git a/node-tools/cmd/node-tools/main.go b/node-tools/cmd/node-tools/main.go index 4be5517..0d840a2 100644 --- a/node-tools/cmd/node-tools/main.go +++ b/node-tools/cmd/node-tools/main.go @@ -91,11 +91,8 @@ func serve(ctx context.Context) error { // ADR 0615 §1; design 46 7-approval-mark §5). // In a module's own runtime the follower hears on the module's one consumer, bound by the runtime for its bundle (design 47 // H §752, H-A-132). - followed, hearer := followerOnOwnConsumer(firstview.DepsOf(conn, log.Printf), served, cred.Module) - if hearer != nil { - opts = append(opts, runtime.WithHearer(cred.Module, hearer.hear)) - } - source, stopViews := firstview.StartWith(context.Background(), followed, log.Printf) + source, stopViews, hearing := startViews(context.Background(), firstview.DepsOf(conn, log.Printf), served, cred.Module, log.Printf) + opts = append(opts, hearing...) defer stopViews() approvals := approvalsState{conn: conn, module: cred.Module} gate := approval.New(approval.Deps{Node: conn.Node(), Runner: cred.Module, Catalog: source.ApprovalCatalog(definitionsOf(seatList)), Wait: source.WaitCatalog, diff --git a/node-tools/cmd/node-tools/ownconsumer.go b/node-tools/cmd/node-tools/ownconsumer.go index 6338e47..f374313 100644 --- a/node-tools/cmd/node-tools/ownconsumer.go +++ b/node-tools/cmd/node-tools/ownconsumer.go @@ -1,6 +1,7 @@ package main import ( + "context" "sync" "github.com/novox/mesh-tools/node-tools/internal/bus" @@ -53,3 +54,15 @@ func followerOnOwnConsumer(d firstview.Deps, served []runtime.Served, own string } return d, nil } + +// startViews is serve's first views: it starts the follower over d and answers the options runtime.Run is given, so that on a +// module's own account the follower binds nothing and hears on the module's one consumer (runtime.WithHearer), and on the +// machine's account the follower binds its own (design 47 H §752, H-A-132, H-A-133). +func startViews(ctx context.Context, d firstview.Deps, served []runtime.Served, own string, logf func(string, ...any)) (*firstview.Views, func(), []runtime.Option) { + followed, hearer := followerOnOwnConsumer(d, served, own) + source, stop := firstview.StartWith(ctx, followed, logf) + if hearer == nil { + return source, stop, nil + } + return source, stop, []runtime.Option{runtime.WithHearer(own, hearer.hear)} +} diff --git a/node-tools/cmd/node-tools/ownconsumer_test.go b/node-tools/cmd/node-tools/ownconsumer_test.go index 6e493ce..45fbba2 100644 --- a/node-tools/cmd/node-tools/ownconsumer_test.go +++ b/node-tools/cmd/node-tools/ownconsumer_test.go @@ -1,7 +1,9 @@ package main import ( + "context" "encoding/json" + "errors" "testing" "github.com/novox/mesh-tools/node-tools/internal/bus" @@ -47,3 +49,28 @@ func TestTheFollowerHearsOnTheOwnModulesOneConsumer(t *testing.T) { }) } } + +// serve's first views: on a module's own account the follower binds nothing and the runtime is given the hearer; on the +// machine's account the follower binds its own consumer and the runtime is given nothing (design 47 H §752, H-A-132). +func TestServesFollowerBindsOnlyOnTheMachinesAccount(t *testing.T) { + served := []runtime.Served{{Module: "mesh-delivery-indexer"}} + for _, row := range []struct { + own string + binds bool + }{{"mesh-delivery-indexer", false}, {runtime.NodeRuntimeModule, true}} { + t.Run(row.own, func(t *testing.T) { + bound := 0 + d := firstview.Deps{Module: row.own, + Ask: func(string, any) (json.RawMessage, error) { return nil, errors.New("no owner here") }, + Consume: func(func(bus.Envelope) error) (func(), error) { bound++; return func() {}, nil }} + ctx, cancel := context.WithCancel(context.Background()) + _, stop, opts := startViews(ctx, d, served, row.own, t.Logf) + cancel() + stop() + if (bound == 1) != row.binds || (len(opts) == 1) == row.binds { + t.Fatalf("the follower bound %d consumer(s), the runtime is given %d option(s); want the follower's own binding %v", + bound, len(opts), row.binds) + } + }) + } +}