mesh/state.page reads a state from an ordered watch starting after the cursor's sequence, never a replay of the bucket per page: each kept message once across the pages of a read, a key's older kept revisions included, at most 768 KB a page, next the opaque cursor, empty at the end. A value written after the read reached its bucket comes marked seen-again (the cursor carries that mark; the runner keeps nothing per read); a deleted key gets no entry. A state kept one bucket per node is read one node's bucket after another. Builds: design 47 H2 §779, H-A-254, H-A-382
292 lines
11 KiB
Go
292 lines
11 KiB
Go
//go:build integration
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"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)
|
|
ledger := mt.MembershipOf("ledger", "anchor", true, map[string][]string{"node-desk": {"who", "slow"}})
|
|
ledger.State = []bus.StateIssued{{Name: "pages", Bucket: "ledger_pages", Writes: true}}
|
|
mesh.Issue(t, ledger)
|
|
// a state of 10,000 keys of 300 bytes each, about four pages of 768 KB (design 47 H2 §779)
|
|
pages := mesh.Bucket(t, "ledger_pages")
|
|
value := []byte(`"` + strings.Repeat("v", 298) + `"`)
|
|
for i := range 10000 {
|
|
if _, err := pages.Put(fmt.Sprintf("k%05d", i), value); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
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")
|
|
heldFile := filepath.Join(dir, "held")
|
|
t.Setenv("HELD_FILE", heldFile)
|
|
|
|
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()
|
|
|
|
// The journal, read for the drain's line at the stop (design 47 H2 §813 (12)).
|
|
journal := &said{}
|
|
log.SetOutput(journal)
|
|
t.Cleanup(func() { log.SetOutput(os.Stderr) })
|
|
// the setting drain-bound as the node-engine composes it (H-A-370, H-A-377)
|
|
t.Setenv("MESH_CONFIG_DRAIN_BOUND", "2s")
|
|
|
|
ctx, stop := context.WithCancel(context.Background())
|
|
ended := make(chan error, 1)
|
|
go func() { ended <- serve(ctx) }()
|
|
var stopped sync.Once
|
|
stopServe := func() {
|
|
stopped.Do(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")
|
|
}
|
|
})
|
|
}
|
|
t.Cleanup(stopServe)
|
|
// 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()
|
|
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)
|
|
}
|
|
mu.Unlock()
|
|
|
|
// a state read in pages by the bundle, each after the cursor the page before answered, delivers each kept message once
|
|
// across the pages, and a key written again mid-read comes again in a later page marked seen-again (design 47 H2 §779,
|
|
// H-A-382)
|
|
type paged struct {
|
|
Keys []string `json:"keys"`
|
|
Again []string `json:"again"`
|
|
Next string `json:"next"`
|
|
Error any `json:"error"`
|
|
}
|
|
readPage := func(cursor string) paged {
|
|
t.Helper()
|
|
answered, err := asker.AskWithin("ledger.page", map[string]any{"cursor": cursor}, "", 30*time.Second)
|
|
var p paged
|
|
if err != nil || json.Unmarshal(answered.Result, &p) != nil || p.Error != nil {
|
|
t.Fatalf("the page after %q was answered %.300s, %v", cursor, answered.Result, err)
|
|
}
|
|
return p
|
|
}
|
|
handed, again, count := map[string]int{}, []string{}, 0
|
|
for p, n := readPage(""), 1; ; p, n = readPage(p.Next), n+1 {
|
|
count++
|
|
for _, k := range p.Keys {
|
|
handed[k]++
|
|
}
|
|
again = append(again, p.Again...)
|
|
if n == 1 {
|
|
if len(p.Keys) == 0 || p.Keys[0] != "k00000" || p.Next == "" {
|
|
t.Fatalf("the first page holds %d keys from %v and next %q; want k00000 first and more to follow", len(p.Keys), p.Keys[:min(1, len(p.Keys))], p.Next)
|
|
}
|
|
if _, err := pages.Put("k00000", []byte(`"again"`)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if p.Next == "" {
|
|
break
|
|
}
|
|
if count > 20 {
|
|
t.Fatal("the read did not end within 20 pages")
|
|
}
|
|
}
|
|
if count < 3 || len(handed) != 10000 || len(again) != 1 || again[0] != "k00000" {
|
|
t.Errorf("the read took %d pages, handed %d keys and marked %v seen-again; want at least 3 pages, 10,000 keys and k00000", count, len(handed), again)
|
|
}
|
|
for k, n := range handed {
|
|
if n != 1 {
|
|
t.Errorf("%s was handed %d times outside of seen-again, want once", k, n)
|
|
}
|
|
}
|
|
|
|
// at its stop the runner drains (design 47 H2 §813 (12), H-A-353; the bound 2 s here for the setting's 30 s): a call of 1 s in
|
|
// flight is answered, one of 6 s is cut at the bound, said, and its caller answered nothing; a call after the stop is answered nothing
|
|
type answer struct {
|
|
got string
|
|
err error
|
|
}
|
|
hold := func(seconds int, within time.Duration) chan answer {
|
|
out := make(chan answer, 1)
|
|
go func() {
|
|
answered, err := asker.AskWithin("ledger.hold", map[string]any{"seconds": seconds}, "", within)
|
|
out <- answer{string(answered.Result), err}
|
|
}()
|
|
return out
|
|
}
|
|
short, long := hold(1, 10*time.Second), hold(6, 5*time.Second)
|
|
// both calls are in flight once the bundle noted them
|
|
mt.Until(t, func() error {
|
|
if noted, _ := os.ReadFile(heldFile); strings.Count(string(noted), "hold") != 2 {
|
|
return fmt.Errorf("the bundle noted %q, want both calls", noted)
|
|
}
|
|
return nil
|
|
})
|
|
stop()
|
|
// a call after the stop began is answered nothing, never refused, so the caller's kept call paces and meets the new runner
|
|
// (H-A-369): until the drain began it is still answered
|
|
mt.Until(t, func() error {
|
|
answered, err := asker.AskWithin("ledger.who", map[string]any{}, "", 500*time.Millisecond)
|
|
if err == nil || err.Error() != "timeout" {
|
|
return fmt.Errorf("a call after the stop was answered %s, %v; want no answer", answered.Result, err)
|
|
}
|
|
return nil
|
|
})
|
|
if got := <-short; got.err != nil || !strings.Contains(got.got, `"held":1`) {
|
|
t.Errorf("the call of 1 s in flight at the stop was answered %s, %v; want it finished", got.got, got.err)
|
|
}
|
|
if got := <-long; got.err == nil || strings.Contains(got.err.Error(), "cut") {
|
|
t.Errorf("the call of 6 s was answered %s, %v; want no answer, a transport fault for its caller", got.got, got.err)
|
|
}
|
|
stopServe()
|
|
if !journal.has("cut ledger's call hold") {
|
|
t.Errorf("the journal does not say the cut call: %s", journal.String())
|
|
}
|
|
}
|
|
|
|
// said is the runner's journal in a test, safe to read while it writes.
|
|
type said struct {
|
|
mu sync.Mutex
|
|
b strings.Builder
|
|
}
|
|
|
|
func (s *said) Write(p []byte) (int, error) { s.mu.Lock(); defer s.mu.Unlock(); return s.b.Write(p) }
|
|
func (s *said) String() string { s.mu.Lock(); defer s.mu.Unlock(); return s.b.String() }
|
|
func (s *said) has(words string) bool { return strings.Contains(s.String(), words) }
|