mesh-build-node: announce the states it keeps from its code, so readers are granted from its word (issue 1790, ADR 0495 §2 §5)
Kept (toolchains, keys, steps, each read by another module today) is said with stdio.Keeps before the module serves; announcetest.Check holds the announcement equal to the code. The module is pinned to mesh-sdk feat/r4e-sdk 9cb4d59.
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
// What this module keeps on the bus, announced from its code (novox/hq ADR 0495 §2 §5; issue 1790): every state it
|
||||
// keeps, each readable by others, since another module reads each today (the builder, the checker and the indexer read toolchains; the builder and the checker read steps; gitea reads keys). A state of its own that it
|
||||
// did not name here is refused at the call (stdio.Keeps).
|
||||
|
||||
import (
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
"git.novox.be/novox/mesh-sdk/go/announce"
|
||||
)
|
||||
|
||||
// Kept are the states the module keeps.
|
||||
var Kept = []announce.State{
|
||||
{Name: "toolchains", Readable: true},
|
||||
{Name: "keys", Readable: true},
|
||||
{Name: "steps", Readable: true},
|
||||
}
|
||||
|
||||
// keepStates announces Kept as this module's (stdio.Keeps): main calls it before it serves.
|
||||
func keepStates() error { return stdio.Keeps(Kept...) }
|
||||
@@ -0,0 +1,63 @@
|
||||
//go:build integration
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"github.com/nats-io/nats.go"
|
||||
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
"git.novox.be/novox/mesh-sdk/go/announcetest"
|
||||
)
|
||||
|
||||
// 1790 (ADR 0495 §5): served as the runtime serves it, the module announces on the bus every state it keeps, as its
|
||||
// code declares them. The runtime's part, publishing what the bundle asks to publish, is played by relay.
|
||||
func TestTheModuleAnnouncesEveryStateItKeeps(t *testing.T) {
|
||||
const module = "mesh-build-node"
|
||||
announcetest.Check(t, module, Kept, func(url string) func() {
|
||||
t.Setenv("MESH_MODULE", module)
|
||||
t.Setenv("MESH_NODE", "shanks")
|
||||
nc, err := nats.Connect(url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := keepStates(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
inR, inW := io.Pipe()
|
||||
outR, outW := io.Pipe()
|
||||
go func() { _ = stdio.ServeOn(module, nil, inR, outW) }()
|
||||
go relay(t, module, nc, outR, inW)
|
||||
return func() { _ = inW.Close(); _ = outW.Close(); nc.Close() }
|
||||
})
|
||||
}
|
||||
|
||||
// relay is the runtime's side of the channel: each mesh/publish the bundle asks is published on its module's event
|
||||
// subject, mesh.mod.<module>.event.<key>, and answered.
|
||||
func relay(t *testing.T, module string, nc *nats.Conn, from io.Reader, to io.Writer) {
|
||||
sc := bufio.NewScanner(from)
|
||||
sc.Buffer(make([]byte, 1<<20), 1<<24)
|
||||
enc := json.NewEncoder(to)
|
||||
for sc.Scan() {
|
||||
var m struct {
|
||||
ID json.RawMessage `json:"id"`
|
||||
Method string `json:"method"`
|
||||
Params struct {
|
||||
Key string `json:"key"`
|
||||
Body json.RawMessage `json:"body"`
|
||||
} `json:"params"`
|
||||
}
|
||||
if json.Unmarshal(sc.Bytes(), &m) != nil || m.Method != "mesh/publish" {
|
||||
continue
|
||||
}
|
||||
if err := nc.Publish("mesh.mod."+module+".event."+m.Params.Key, m.Params.Body); err != nil {
|
||||
t.Errorf("publishing %s: %v", m.Params.Key, err)
|
||||
}
|
||||
_ = nc.Flush()
|
||||
_ = enc.Encode(map[string]any{"id": m.ID, "result": map[string]any{}})
|
||||
}
|
||||
}
|
||||
@@ -57,6 +57,11 @@ func main() {
|
||||
go m.Start(ctx)
|
||||
}
|
||||
go startKeeper(ctx)
|
||||
// Every state it keeps, said on the bus before it serves (ADR 0495 §2; issue 1790).
|
||||
if err := keepStates(); err != nil {
|
||||
logf("announcing the states it keeps: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if err := stdio.Serve("", m.Tools(m.StepTools())); err != nil {
|
||||
logf("%v", err)
|
||||
os.Exit(1)
|
||||
|
||||
Reference in New Issue
Block a user