main's body is `serve(ctx)`, which the process calls with the service manager's signals and one integration test calls with a credential file and the node-engine's environment: it serves a bundle's tool and seat verbs over a seat list read from a stand-in of the seat mesh-catalog, waits a verb its definition's timeout, and rereads the list for a verb the first read does not name. Builds: design 47 H §774, §760, H-A-16, H-A-61 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
165 lines
6.2 KiB
Go
165 lines
6.2 KiB
Go
//go:build integration
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/seatdef"
|
|
"git.novox.be/novox/mesh-sdk/go/seatlist"
|
|
|
|
"github.com/novox/mesh-tools/node-tools/internal/bus"
|
|
mt "github.com/novox/mesh-tools/node-tools/internal/meshtest"
|
|
)
|
|
|
|
// catalogStandIn is the seat mesh-catalog's verb `seat-definitions`, as the seat list's reader asks it: the seat node-desk whose
|
|
// first listing names the verbs who and slow (slow with a timeout of one second) and, from the second listing on, also the verb
|
|
// fresh, which sends its answer in chunks.
|
|
type catalogStandIn struct {
|
|
mu sync.Mutex
|
|
listings int
|
|
}
|
|
|
|
func (c *catalogStandIn) definition(withFresh bool) json.RawMessage {
|
|
one := 1
|
|
verbs := []seatdef.Verb{{Name: "who"}, {Name: "slow", Timeout: &one}}
|
|
if withFresh {
|
|
verbs = append(verbs, seatdef.Verb{Name: "fresh", Chunks: true})
|
|
}
|
|
raw, _ := json.Marshal(seatdef.Definition{Format: seatdef.Format, Seat: "node-desk", Scope: seatdef.ScopeNode, Grain: seatdef.SeatList,
|
|
Decision: "ADR 0001", Domain: seatdef.Domains[0], Verbs: verbs})
|
|
return raw
|
|
}
|
|
|
|
func (c *catalogStandIn) handle(body json.RawMessage) (any, error) {
|
|
var asked struct {
|
|
Seat string `json:"seat"`
|
|
}
|
|
_ = json.Unmarshal(body, &asked)
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
if asked.Seat == "" {
|
|
c.listings++
|
|
return seatlist.Listing{Revision: uint64(c.listings), Seats: []seatlist.Entry{{Seat: "node-desk", Source: seatlist.SourceTrunk,
|
|
Digest: map[bool]string{false: "d1", true: "d2"}[c.listings > 1]}}}, nil
|
|
}
|
|
withFresh := c.listings > 1
|
|
return seatlist.Answer{Seat: asked.Seat, Found: true, Revision: uint64(c.listings), Entry: &seatlist.Entry{Seat: asked.Seat,
|
|
Source: seatlist.SourceTrunk, Digest: map[bool]string{false: "d1", true: "d2"}[withFresh], Definition: c.definition(withFresh)}}, nil
|
|
}
|
|
|
|
// **The tool runner, started whole by its entry** (design 47 H §774, H-A-16, H-A-61): the process's own `serve`, on an embedded bus with
|
|
// JetStream, with a credential file and the environment the node-engine writes, serving one bundle's tool and seat verbs, over the
|
|
// seat list read from a stand-in of the seat mesh-catalog.
|
|
// - the bundle's tool and its seat verb are answered on the bus (§734's way of serving a verb from a bundle);
|
|
// - a verb whose definition names a timeout of one second is waited one second by the serving runner, not the runner's own
|
|
// thirty (design 47 H §760, H-A-43);
|
|
// - a bundle's ask of a seat verb its first seat list does not name is asked after one reread of the list, with its call token
|
|
// for a verb that sends chunks (design 47 G A-490m).
|
|
func TestTheToolRunnerStartedByItsEntryServesABundleOverTheSeatListItReads(t *testing.T) {
|
|
mesh := mt.New(t)
|
|
mesh.Issue(t, mt.MembershipOf("ledger", "anchor", true, map[string][]string{"node-desk": {"who", "slow"}}))
|
|
dir := t.TempDir()
|
|
credential, _ := json.Marshal(bus.Credential{URL: mt.URL(t), Module: runtimeModule, Node: "anchor"})
|
|
file := filepath.Join(dir, "broker")
|
|
if err := os.WriteFile(file, credential, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("MESH_BROKER_FILE", file)
|
|
t.Setenv("MESH_TOOL_MODULES", "ledger="+mt.Fixture("whole-bundle.py"))
|
|
t.Setenv("MESH_CONSOLE_LISTEN", "127.0.0.1:0")
|
|
|
|
open := func(module, node string) *bus.Conn {
|
|
c, err := bus.Connect(bus.Credential{URL: mt.URL(t), Module: module, Node: node, User: node + "." + module, Password: "unused"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c.Logf = func(string, ...any) {}
|
|
t.Cleanup(c.Close)
|
|
return c
|
|
}
|
|
catalog := &catalogStandIn{}
|
|
holder := open("mesh-catalog", "anchor")
|
|
if _, err := holder.HandleSubject("mesh.seat.mesh-catalog.tool.seat-definitions", func(body json.RawMessage) (any, error) { return catalog.handle(body) }); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var mu sync.Mutex
|
|
var fresh []map[string]any
|
|
if _, err := holder.HandleSubject("mesh.seat.node-desk.tool.fresh", func(body json.RawMessage) (any, error) {
|
|
var got map[string]any
|
|
_ = json.Unmarshal(body, &got)
|
|
mu.Lock()
|
|
fresh = append(fresh, got)
|
|
mu.Unlock()
|
|
return map[string]any{"ok": true}, nil
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
holder.Flush()
|
|
|
|
ctx, stop := context.WithCancel(context.Background())
|
|
ended := make(chan error, 1)
|
|
go func() { ended <- serve(ctx) }()
|
|
t.Cleanup(func() {
|
|
stop()
|
|
select {
|
|
case err := <-ended:
|
|
if err != nil {
|
|
t.Errorf("serve ended with %v", err)
|
|
}
|
|
case <-time.After(30 * time.Second):
|
|
t.Error("the tool runner did not stop within 30s")
|
|
}
|
|
})
|
|
// a module's account, which the approval gate runs unmarked while the catalog half is unread (the stand-in serves no catalog half)
|
|
asker := open("shop", "workstation")
|
|
ask := func(key string) (string, error) {
|
|
answered, err := asker.Ask(key, map[string]any{}, "")
|
|
return string(answered.Result), err
|
|
}
|
|
|
|
// the bundle's tool and its seat verb are served
|
|
for _, key := range []string{"ledger.who", "seat:node-desk.who@anchor"} {
|
|
mt.Until(t, func() error {
|
|
got, err := ask(key)
|
|
if err == nil && !strings.Contains(got, `"tool":`) {
|
|
t.Errorf("%s answered %s, want the bundle's", key, got)
|
|
}
|
|
return err
|
|
})
|
|
}
|
|
|
|
// a verb whose definition names a timeout of one second is waited one second
|
|
began := time.Now()
|
|
if _, err := ask("seat:node-desk.slow@anchor"); err == nil {
|
|
t.Error("the verb that answers after three seconds was answered within its timeout of one second")
|
|
}
|
|
if took := time.Since(began); took < time.Second || took > 2500*time.Millisecond {
|
|
t.Errorf("the verb of a timeout of one second was waited %s, want about one second, not the runner's own %s", took, bus.RequestTimeout)
|
|
}
|
|
|
|
// the bundle's ask of a verb its first list does not name is asked after one reread, with its call token
|
|
mt.Until(t, func() error {
|
|
got, err := ask("ledger.reach")
|
|
if err == nil && strings.Contains(got, "error") {
|
|
t.Errorf("the bundle's ask of node-desk.fresh failed: %s", got)
|
|
}
|
|
return err
|
|
})
|
|
catalog.mu.Lock()
|
|
listings := catalog.listings
|
|
catalog.mu.Unlock()
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if listings != 2 || len(fresh) != 1 || fresh[0]["call"] == nil || fresh[0]["call"] == "" {
|
|
t.Errorf("the list was read %d times (want 2) and the holder of node-desk.fresh was asked %v (want once, with a call token)", listings, fresh)
|
|
}
|
|
}
|