Files
mesh-tools/node-tools/cmd/node-tools/ownconsumer_test.go
T
jschoubben fb4fe282c7 H1 one consumer loop: serve's first views wiring in one tested function
Review round 1: no test failed when serve went back to firstview.Start.
startViews starts the follower and answers the options runtime.Run is
given; its test holds that on a module's own account the follower binds
nothing and the runtime is given the hearer, and on the machine's
account the follower binds its own.

Builds: design 47 H §752, H-A-113, H-A-132, H-A-133

Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
2026-11-20 11:33:19 +01:00

77 lines
3.1 KiB
Go

package main
import (
"context"
"encoding/json"
"errors"
"testing"
"github.com/novox/mesh-tools/node-tools/internal/bus"
"github.com/novox/mesh-tools/node-tools/internal/firstview"
"github.com/novox/mesh-tools/node-tools/internal/runtime"
)
// In a module's own runtime the branch follower binds no consumer of its own and hears each event the runtime hands it from the
// module's one consumer; a runtime that is not the module's own keeps the follower's own binding (design 47 H §752, H-A-132).
func TestTheFollowerHearsOnTheOwnModulesOneConsumer(t *testing.T) {
for _, row := range []struct {
name string
served []runtime.Served
own string
binds bool
}{
{"a module's own runtime", []runtime.Served{{Module: "mesh-delivery-indexer"}}, "mesh-delivery-indexer", false},
{"the machine's runtime", []runtime.Served{{Module: "mesh-delivery-indexer"}}, runtime.NodeRuntimeModule, true},
} {
t.Run(row.name, func(t *testing.T) {
bound := 0
d := firstview.Deps{Consume: func(func(bus.Envelope) error) (func(), error) { bound++; return func() {}, nil }}
got, hearer := followerOnOwnConsumer(d, row.served, row.own)
var heard []string
stop, err := got.Consume(func(env bus.Envelope) error { heard = append(heard, env.Key); return nil })
if err != nil {
t.Fatal(err)
}
if (bound == 1) != row.binds || (hearer == nil) != row.binds {
t.Fatalf("bound its own consumer %d time(s), hearer given to the runtime %v; want its own binding %v", bound, hearer != nil, row.binds)
}
if row.binds {
return
}
env := bus.Envelope{Key: "mesh-catalog.catalog-revision.main", Body: json.RawMessage(`{}`)}
if err := hearer.hear(env); err != nil || len(heard) != 1 || heard[0] != env.Key {
t.Fatalf("the event handed by the runtime: err %v, the follower heard %q", err, heard)
}
stop()
if err := hearer.hear(env); err != nil || len(heard) != 1 {
t.Fatalf("after the follower stopped: err %v, heard %q", err, heard)
}
})
}
}
// 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)
}
})
}
}