Files
mesh-sdk/go/state_test.go
T
jschoubben f90189353c Fold H2's new tests into existing ones: go/ at 327 unit tests (from 331), TypeScript at 48 (from 49)
The eight Go test functions H2 added become subtests of existing tests
(catalogue's state test, the bundle's state test, the views test, the process
state test, the testbus bus test, the seal byte-for-byte test), and four more
are folded beside them (two seal, two process watch); the two TypeScript seal
tests become one, and two more TypeScript tests are folded into their
neighbours. No assertion is lost.

Builds: design 47 H2 §819
2026-11-22 02:25:44 +01:00

613 lines
27 KiB
Go

package stdio
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"sort"
"strings"
"sync"
"testing"
"time"
)
// A Go bundle keeps and watches its state through the runtime (novox/hq ADR 0201): the watch's
// current values are handled before it returns, its changes are named by the watch's id, a change for
// no watch of this bundle is refused, and put, get and keys are asked of the runtime as the module.
func TestABundleKeepsAndWatchesStateThroughTheRuntime(t *testing.T) {
servesAlone(t)
t.Setenv(ServedAs, "keeper")
inR, inW := io.Pipe()
outR, outW := io.Pipe()
seen := make(chan string, 16)
watched := make(chan error, 1)
slowWatched, release := make(chan error, 1), make(chan struct{})
go func() {
_ = ServeOn("", []Tool{
{Name: "start", Run: func(map[string]any) (any, error) {
go func() {
watched <- State("servers").Watch("", func(c StateChange) error {
seen <- c.Op + " " + c.Key + " " + string(c.Value)
return nil
})
}()
return "started", nil
}},
{Name: "slow", Run: func(map[string]any) (any, error) {
go func() {
slowWatched <- State("slow").Watch("", func(c StateChange) error {
<-release
seen <- "slow " + c.Key
return errors.New("the handler's own error")
})
}()
return "started", nil
}},
{Name: "refused", Run: func(map[string]any) (any, error) {
return nil, State("gone").Watch("", func(StateChange) error { return nil })
}},
{Name: "remember", Run: func(a map[string]any) (any, error) {
return State("servers").Put(a["key"].(string), a["value"])
}},
{Name: "recall", Run: func(a map[string]any) (any, error) { return State("servers").Get(a["key"].(string)) }},
{Name: "pulse", Run: func(a map[string]any) (any, error) {
if a["create"] == true {
return State("servers").CreateTTL(a["key"].(string), 1, 90*time.Second)
}
return State("servers").PutTTL(a["key"].(string), 1, 90*time.Second)
}},
{Name: "take", Run: func(a map[string]any) (any, error) {
return State("servers").TakeOnce(a["seat"].(string), a["ask"].(string), 10*time.Minute)
}},
{Name: "page", Run: func(a map[string]any) (any, error) {
return State("servers").Page(a["cursor"].(string), 1000, time.Second)
}},
}, inR, outW)
}()
lines := bufio.NewScanner(outR)
next := func() map[string]any {
done := make(chan map[string]any, 1)
go func() {
if lines.Scan() {
var m map[string]any
_ = json.Unmarshal(lines.Bytes(), &m)
done <- m
}
}()
select {
case m := <-done:
return m
case <-time.After(3 * time.Second):
t.Fatal("the bundle said nothing")
return nil
}
}
send := func(m map[string]any) { b, _ := json.Marshal(m); _, _ = inW.Write(append(b, '\n')) }
send(map[string]any{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": map[string]any{"name": "start"}})
got := map[string]map[string]any{}
for len(got) < 2 {
m := next()
if m["method"] != nil {
got[m["method"].(string)] = m
} else {
got["answer"] = m
}
}
watch := got["mesh/state.watch"]
if p := watch["params"].(map[string]any); p["state"] != "servers" || p["key"] != "" {
t.Fatalf("asked to watch %v", p)
}
// The current value, for this watch, before the watch is answered.
send(map[string]any{"jsonrpc": "2.0", "id": 50, "method": "mesh/state", "params": map[string]any{"watch": watch["id"],
"change": map[string]any{"state": "servers", "key": "all.one", "op": "put", "value": map[string]any{"url": "http://one"}, "current": true}}})
if r := next(); r["id"] != float64(50) || r["error"] != nil {
t.Fatalf("a current value was answered %v", r)
}
if s := <-seen; s != `put all.one {"url":"http://one"}` {
t.Fatalf("handled %q", s)
}
select {
case err := <-watched:
t.Fatalf("the watch returned before it was answered: %v", err)
default:
}
send(map[string]any{"jsonrpc": "2.0", "id": watch["id"], "result": map[string]any{}})
if err := <-watched; err != nil {
t.Fatal(err)
}
// 1952 (the runtime's per-key time to live, mesh-tools 1583fa85): an expiry is handed over as its own operation.
send(map[string]any{"jsonrpc": "2.0", "id": 49, "method": "mesh/state", "params": map[string]any{"watch": watch["id"],
"change": map[string]any{"state": "servers", "key": "all.gone", "op": OpExpire}}})
if r := next(); r["id"] != float64(49) || r["error"] != nil {
t.Fatalf("an expiry was answered %v", r)
}
if s := <-seen; s != "expire all.gone " {
t.Fatalf("handled %q", s)
}
// A change for a watch this bundle never asked for is refused.
send(map[string]any{"jsonrpc": "2.0", "id": 51, "method": "mesh/state", "params": map[string]any{"watch": "bundle-999",
"change": map[string]any{"key": "x", "op": "put"}}})
if r := next(); r["id"] != float64(51) || !strings.Contains(r["error"].(map[string]any)["message"].(string), "names no watch") {
t.Fatalf("a change for no watch was answered %v", r)
}
// Issue 774 (the provisioner's "names no watch of this bundle"): a change is answered as soon as it is taken, so a
// slow handler never counts against the runtime's bound on the current values; Watch still returns only once the
// current values were handled, and a handler's error stays the bundle's to say.
send(map[string]any{"jsonrpc": "2.0", "id": 60, "method": "tools/call", "params": map[string]any{"name": "slow"}})
var slowAsk map[string]any
for i := 0; i < 2; i++ {
if m := next(); m["method"] == "mesh/state.watch" {
slowAsk = m
}
}
for i, key := range []string{"a", "b"} {
send(map[string]any{"jsonrpc": "2.0", "id": 61 + i, "method": "mesh/state", "params": map[string]any{"watch": slowAsk["id"],
"change": map[string]any{"state": "slow", "key": key, "op": "put", "value": 1, "current": true}}})
if r := next(); r["id"] != float64(61+i) || r["error"] != nil {
t.Fatalf("a change to a slow handler was answered %v", r)
}
}
send(map[string]any{"jsonrpc": "2.0", "id": slowAsk["id"], "result": map[string]any{}})
select {
case err := <-slowWatched:
t.Fatalf("the watch returned before its current values were handled: %v", err)
case <-time.After(50 * time.Millisecond):
}
close(release)
if a, b := <-seen, <-seen; a != "slow a" || b != "slow b" {
t.Fatalf("handled out of order: %q, %q", a, b)
}
if err := <-slowWatched; err != nil {
t.Fatal(err)
}
// A watch the runtime refuses is given up, and a change the runtime still hands to it is refused saying so.
send(map[string]any{"jsonrpc": "2.0", "id": 70, "method": "tools/call", "params": map[string]any{"name": "refused"}})
goneAsk := next()
send(map[string]any{"jsonrpc": "2.0", "id": goneAsk["id"], "error": map[string]any{"code": -32000,
"message": "the current values of gone did not arrive in 10s"}})
if r := next(); r["id"] != float64(70) {
t.Fatalf("the refused watch answered %v", r)
}
send(map[string]any{"jsonrpc": "2.0", "id": 71, "method": "mesh/state", "params": map[string]any{"watch": goneAsk["id"],
"change": map[string]any{"state": "gone", "key": "k", "op": "put"}}})
if r := next(); r["id"] != float64(71) || !strings.Contains(fmt.Sprint(r), "gave the watch") || !strings.Contains(fmt.Sprint(r), "did not arrive in 10s") {
t.Fatalf("a change for a given-up watch was answered %v", r)
}
// Put: asked of the runtime as the module, the revision is the tool's answer.
send(map[string]any{"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": map[string]any{"name": "remember",
"arguments": map[string]any{"key": "all.two", "value": map[string]any{"url": "http://two"}}}})
put := next()
if put["method"] != "mesh/state.put" || put["params"].(map[string]any)["key"] != "all.two" || put["params"].(map[string]any)["state"] != "servers" {
t.Fatalf("asked %v", put)
}
send(map[string]any{"jsonrpc": "2.0", "id": put["id"], "result": map[string]any{"revision": 9}})
if r := next(); r["id"] != float64(2) || r["result"].(map[string]any)["content"].([]any)[0].(map[string]any)["text"] != "9" {
t.Fatalf("the put answered %v", r)
}
// Issue 774 (sweep item 5): a value over a module state's largest is refused before the runtime is asked, naming the
// key, its size and the limit; one the runtime's bus refuses anyway comes back as the same refusal.
send(map[string]any{"jsonrpc": "2.0", "id": 30, "method": "tools/call", "params": map[string]any{"name": "remember",
"arguments": map[string]any{"key": "all.big", "value": strings.Repeat("x", 300*1024)}}})
if r := next(); r["id"] != float64(30) || !strings.Contains(fmt.Sprint(r), "all.big in the state servers is 307202 bytes, over the 262144") {
t.Fatalf("a value over the limit was not refused before the runtime was asked: %v", r)
}
send(map[string]any{"jsonrpc": "2.0", "id": 31, "method": "tools/call", "params": map[string]any{"name": "remember",
"arguments": map[string]any{"key": "all.near", "value": "x"}}})
near := next()
send(map[string]any{"jsonrpc": "2.0", "id": near["id"], "error": map[string]any{"code": -32000,
"message": "nats: API error: code=400 err_code=10054 description=message size exceeds maximum allowed"}})
if r := next(); r["id"] != float64(31) || !strings.Contains(fmt.Sprint(r), "written again it is refused again") {
t.Fatalf("the bus's refusal was not named as too large: %v", r)
}
// PutTTL and CreateTTL ask the runtime's put and create with the key's time to live in whole seconds; a create the
// runtime refuses as changed (-32010) is ErrChanged.
for i, create := range []bool{false, true} {
send(map[string]any{"jsonrpc": "2.0", "id": 20 + i, "method": "tools/call", "params": map[string]any{"name": "pulse",
"arguments": map[string]any{"key": "pulse.S1", "create": create}}})
asked := next()
p := asked["params"].(map[string]any)
want := map[bool]string{false: "mesh/state.put", true: "mesh/state.create"}[create]
if asked["method"] != want || p["ttl_seconds"] != float64(90) || p["key"] != "pulse.S1" {
t.Fatalf("asked %v", asked)
}
if create {
send(map[string]any{"jsonrpc": "2.0", "id": asked["id"], "error": map[string]any{"code": CodeChanged, "message": "exists"}})
if r := next(); r["id"] != float64(21) || !strings.Contains(fmt.Sprint(r), "exists") {
t.Fatalf("a refused create answered %v", r)
}
continue
}
send(map[string]any{"jsonrpc": "2.0", "id": asked["id"], "result": map[string]any{"revision": 4}})
if r := next(); r["id"] != float64(20) {
t.Fatalf("the put answered %v", r)
}
}
// Design 47 H2 §775, H-A-359: a bundle takes an ask once: the create of <seat>.<ask> with the ask's bound as its time
// to live answers true, the runtime's refusal as changed answers false, and an ask with no id is refused unasked.
for i, exists := range []bool{false, true} {
send(map[string]any{"jsonrpc": "2.0", "id": 40 + i, "method": "tools/call", "params": map[string]any{"name": "take",
"arguments": map[string]any{"seat": "git", "ask": "a-7"}}})
asked := next()
p := asked["params"].(map[string]any)
if asked["method"] != "mesh/state.create" || p["key"] != "git.a-7" || p["ttl_seconds"] != float64(600) {
t.Fatalf("asked %v", asked)
}
if exists {
send(map[string]any{"jsonrpc": "2.0", "id": asked["id"], "error": map[string]any{"code": CodeChanged, "message": "exists"}})
} else {
send(map[string]any{"jsonrpc": "2.0", "id": asked["id"], "result": map[string]any{"revision": 5}})
}
r := next()
if text := fmt.Sprint(r["result"]); r["id"] != float64(40+i) || !strings.Contains(text, map[bool]string{false: "true", true: "false"}[exists]) {
t.Fatalf("a take, the key existing %v, answered %v", exists, r)
}
}
send(map[string]any{"jsonrpc": "2.0", "id": 42, "method": "tools/call", "params": map[string]any{"name": "take",
"arguments": map[string]any{"seat": "git", "ask": ""}}})
if r := next(); r["id"] != float64(42) || !strings.Contains(fmt.Sprint(r), "is not an ask id") {
t.Fatalf("a take with no ask id answered %v", r)
}
// Design 47 H2 §779: a page read hands the runtime the opaque cursor and answers the cursor that continues it, and a
// key written again since an earlier page comes marked seen-again; a kept delete comes as an entry marked deleted with
// no value (H-A-451 (1)).
send(map[string]any{"jsonrpc": "2.0", "id": 43, "method": "tools/call", "params": map[string]any{"name": "page",
"arguments": map[string]any{"cursor": "c.17"}}})
if asked := next(); asked["method"] != "mesh/state.page" || asked["params"].(map[string]any)["cursor"] != "c.17" || asked["params"].(map[string]any)["after"] != nil {
t.Fatalf("asked %v", asked)
} else {
send(map[string]any{"jsonrpc": "2.0", "id": asked["id"], "result": map[string]any{"next": "c.19", "entries": []any{
map[string]any{"key": "a", "value": 1, "revision": 18}, map[string]any{"key": "b", "value": 2, "revision": 19, "seen-again": true},
map[string]any{"key": "c", "revision": 20, "deleted": true}}}})
}
if r := next(); r["id"] != float64(43) || !strings.Contains(fmt.Sprint(r), `"next":"c.19"`) || !strings.Contains(fmt.Sprint(r), `"revision":19,"seen-again":true`) ||
strings.Count(fmt.Sprint(r), "seen-again") != 1 || !strings.Contains(fmt.Sprint(r), `"key":"c","value":null,"revision":20,"deleted":true`) ||
strings.Count(fmt.Sprint(r), "deleted") != 1 {
t.Fatalf("the page answered %v", r)
}
if !errors.Is(&refused{code: CodeChanged, message: "x"}, ErrChanged) {
t.Fatal("a refusal coded -32010 is not ErrChanged")
}
if !errors.Is(&refused{code: CodeNoMethod, message: "no mesh/state.page"}, errors.ErrUnsupported) || errors.Is(&refused{code: -32000}, errors.ErrUnsupported) {
t.Fatal("only a refusal coded -32601 is ErrUnsupported")
}
// Get of a key with no value is nil.
send(map[string]any{"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": map[string]any{"name": "recall",
"arguments": map[string]any{"key": "nothing"}}})
get := next()
send(map[string]any{"jsonrpc": "2.0", "id": get["id"], "result": nil})
if r := next(); r["id"] != float64(3) || r["result"].(map[string]any)["content"].([]any)[0].(map[string]any)["text"] != "null" {
t.Fatalf("the get answered %v", r)
}
_ = inW.Close()
// ADR 0580 clause 1: the watched copy answers keys and values after the watch's current values, a later put or delete
// changes its answer, a follower's then sees the copy that took its change, each state is watched once, and a
// value kept once is read by key once.
handles, watches, gets := map[string]func(StateChange) error{}, map[string]int{}, 0
w := WatchedStatesOver(func(state string, handle func(StateChange) error) error {
watches[state]++
handles[state] = handle
return handle(StateChange{State: state, Key: "a", Op: OpPut, Value: []byte("1"), Current: true})
}, func(state, key string) ([]byte, bool, error) { gets++; return []byte(key), key != "missing", nil })
var thenSaw []string
w.Follow("placement", func(c StateChange) error {
v, found, _ := w.Get("placement", c.Key)
thenSaw = append(thenSaw, fmt.Sprintf("%s %s %s %v", c.Op, c.Key, v, found))
return nil
})
if keys, err := w.Keys("placement"); err != nil || fmt.Sprint(keys) != "[a]" {
t.Fatalf("the copy's keys are %v (%v)", keys, err)
}
_ = handles["placement"](StateChange{Key: "b", Op: OpPut, Value: []byte("2")})
_ = handles["placement"](StateChange{Key: "a", Op: OpDelete})
vals, _ := w.Values("placement")
if v, found, _ := w.Get("placement", "a"); found || string(vals["b"]) != "2" || len(vals) != 1 || v != nil {
t.Fatalf("after a put and a delete the copy is %v", vals)
}
if fmt.Sprint(thenSaw) != "[put a 1 true put b 2 true delete a false]" {
t.Fatalf("then saw %v", thenSaw)
}
_, _ = w.Keys("placement")
if watches["placement"] != 1 {
t.Fatalf("the state was watched %d times", watches["placement"])
}
// A read that starts a state's watch before its follower joins loses that follower no change: the follower is run over
// the copy's current values when it joins, then hears every later change (review of ADR 0580 §2).
if _, _, err := w.Get("haves", "n1.a"); err != nil {
t.Fatal(err)
}
_ = handles["haves"](StateChange{Key: "n1.b", Op: OpPut, Value: []byte("2")}) // in the gap: no follower yet
var joined []string
w.Follow("haves", func(c StateChange) error { joined = append(joined, c.Key); return nil })
_ = handles["haves"](StateChange{Key: "n2.c", Op: OpPut, Value: []byte("3")})
if fmt.Sprint(joined) != "[a n1.b n2.c]" || watches["haves"] != 1 {
t.Fatalf("a follower joining a watched state heard %v (watched %d times)", joined, watches["haves"])
}
for range 2 {
if v, found, _ := w.Once("manifests", "d1"); !found || string(v) != "d1" {
t.Fatalf("once read %q %v", v, found)
}
_, _, _ = w.Once("manifests", "missing")
}
if gets != 3 {
t.Fatalf("a kept value was read by key %d times, want once, and the missing one twice", gets)
}
// Issue 1954: a state the process alone writes reads its own put and delete back from the copy at once, and the
// watch's changes of that key up to the write are not taken over it; a later change is.
_ = handles["placement"](StateChange{Key: "own", Op: OpPut, Value: []byte("old"), Revision: 5})
if err := w.Wrote("placement", "own", []byte("mine"), 7); err != nil {
t.Fatal(err)
}
_ = w.Wrote("placement", "gone", []byte("x"), 8)
_ = w.Removed("placement", "gone")
for _, c := range []StateChange{{Key: "own", Op: OpPut, Value: []byte("old"), Revision: 6},
{Key: "own", Op: OpPut, Value: []byte("mine"), Revision: 7}, {Key: "gone", Op: OpPut, Value: []byte("x"), Revision: 8}} {
_ = handles["placement"](c)
}
own, _, _ := w.Get("placement", "own")
if _, found, _ := w.Get("placement", "gone"); found || string(own) != "mine" {
t.Fatalf("after its own writes and the watch's older changes the copy holds own=%s, gone found %v", own, found)
}
_ = handles["placement"](StateChange{Key: "gone", Op: OpPut, Value: []byte("again"), Revision: 9})
if v, _, _ := w.Get("placement", "gone"); string(v) != "again" {
t.Fatalf("a change after its own delete is not taken: %s", v)
}
// Design 47 H2 §782: one bundle's state calls through its one runtime channel are safe at once: gets, puts, pages,
// watches and bus-state calls from many goroutines, the runtime answering each, under the race detector.
t.Run("one runtime channel serves concurrent state calls", func(t *testing.T) {
inR, inW := io.Pipe()
outR, outW := io.Pipe()
t.Cleanup(func() { _ = inW.Close(); _ = outR.Close() })
const many = 16
go func() {
_ = ServeOn("", []Tool{{Name: "many", Run: func(map[string]any) (any, error) {
var wg sync.WaitGroup
errs := make(chan error, many*6)
for i := range many {
wg.Add(1)
go func() {
defer wg.Done()
s, key := State("servers"), fmt.Sprintf("k%d", i)
_, err := s.Put(key, i)
errs <- err
_, err = s.Get(key)
errs <- err
_, err = s.Page("", 1000, time.Second)
errs <- err
errs <- s.Watch(key, func(StateChange) error { return nil })
_, err = BusState("mesh/objects.info", map[string]any{"objects": "o"})
errs <- err
_, err = s.Info()
errs <- err
}()
}
wg.Wait()
close(errs)
for err := range errs {
if err != nil {
return nil, err
}
}
return "done", nil
}}}, inR, outW)
}()
var wmu sync.Mutex
send := func(m map[string]any) {
b, _ := json.Marshal(m)
wmu.Lock()
defer wmu.Unlock()
_, _ = inW.Write(append(b, '\n'))
}
send(map[string]any{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": map[string]any{"name": "many"}})
lines := bufio.NewScanner(outR)
answered := make(chan map[string]any, 1)
go func() {
n := 1000
for lines.Scan() {
var m map[string]any
_ = json.Unmarshal(lines.Bytes(), &m)
method, _ := m["method"].(string)
switch {
case m["id"] == float64(1):
answered <- m
return
case method == "mesh/state.watch":
n++
id := m["id"]
key := m["params"].(map[string]any)["key"]
// The runtime hands the current value over, then answers the watch, on a goroutine of its own, as
// the tool runner's answers come in any order.
go func(n int) {
send(map[string]any{"jsonrpc": "2.0", "id": n, "method": "mesh/state", "params": map[string]any{"watch": id,
"change": map[string]any{"state": "servers", "key": key, "op": "put", "value": 1, "current": true}}})
send(map[string]any{"jsonrpc": "2.0", "id": id, "result": map[string]any{}})
}(n)
case method == "mesh/state.page":
go send(map[string]any{"jsonrpc": "2.0", "id": m["id"], "result": map[string]any{"entries": []any{}}})
case method == "mesh/state.get":
go send(map[string]any{"jsonrpc": "2.0", "id": m["id"], "result": map[string]any{"key": "k", "value": 1, "revision": 2}})
case method != "":
go send(map[string]any{"jsonrpc": "2.0", "id": m["id"], "result": map[string]any{"revision": 1}})
}
}
}()
select {
case m := <-answered:
if m["error"] != nil || fmt.Sprint(m["result"]) != "map[content:[map[text:\"done\" type:text]]]" {
t.Fatalf("the concurrent calls answered %v", m)
}
case <-time.After(10 * time.Second):
t.Fatal("the concurrent calls did not end")
}
})
// Design 47 H2 §812 (H-A-380, H-A-386): prune-mark and prune-target are read through the settings reader with their
// defaults, and a bundle's Put judges the fill the runtime's put answer carries: past 70 % the oldest keys by revision
// are deleted to 50 %, a key kept under _keep/<key> survives, and no reader (Keys, Page) counts the keep.
t.Run("a write past its mark prunes to its target and a kept key survives", func(t *testing.T) {
t.Run("settings", func(t *testing.T) {
if p, err := PruneSettings(); err != nil || p != (Prune{Mark: 70, Target: 50}) {
t.Fatalf("the defaults are %+v, %v", p, err)
}
t.Setenv(SettingWord(PruneMarkSetting), "80%")
t.Setenv(SettingWord(PruneTargetSetting), "60")
if p, err := PruneSettings(); err != nil || p != (Prune{Mark: 80, Target: 60}) {
t.Fatalf("the settings read %+v, %v", p, err)
}
t.Setenv(SettingWord(PruneTargetSetting), "90")
if _, err := PruneSettings(); err == nil || !strings.Contains(err.Error(), "prune-target") {
t.Fatalf("a target above the mark was taken: %v", err)
}
t.Setenv(SettingWord(PruneTargetSetting), "half")
if _, err := PruneSettings(); err == nil || !strings.Contains(err.Error(), "MESH_CONFIG_PRUNE_TARGET") {
t.Fatalf("a target that is no percentage was taken: %v", err)
}
})
servesAlone(t)
t.Setenv(ServedAs, "bridge")
inR, inW := io.Pipe()
outR, outW := io.Pipe()
pruned := State("performed").Pruned(Prune{Mark: 70, Target: 50})
go func() {
_ = ServeOn("", []Tool{
{Name: "write", Run: func(a map[string]any) (any, error) { return pruned.Put(a["key"].(string), a["value"]) }},
{Name: "keep", Run: func(a map[string]any) (any, error) {
return nil, pruned.Keep(a["key"].(string), time.Now().Add(time.Hour))
}},
{Name: "keys", Run: func(map[string]any) (any, error) { return State("performed").Keys() }},
{Name: "page", Run: func(map[string]any) (any, error) { return State("performed").Page("", 1<<20, time.Second) }},
}, inR, outW)
}()
// The runtime: a state of a cap of 10000 bytes whose fill is what its current values put.
const limit = 10_000
var mu sync.Mutex
values, revs, next := map[string]json.RawMessage{}, map[string]uint64{}, uint64(0)
held := func() uint64 {
var n uint64
for k, v := range values {
n += PutBytes(k, v)
}
return n
}
var mostHeld uint64
answers := make(chan map[string]any, 1)
send := func(m map[string]any) { b, _ := json.Marshal(m); _, _ = inW.Write(append(b, '\n')) }
go func() {
lines := bufio.NewScanner(outR)
lines.Buffer(make([]byte, 1<<20), 1<<22)
for lines.Scan() {
var m struct {
ID any `json:"id"`
Method string `json:"method"`
Params map[string]any `json:"params"`
Result any `json:"result"`
Error any `json:"error"`
}
_ = json.Unmarshal(lines.Bytes(), &m)
if m.Method == "" {
answers <- map[string]any{"result": m.Result, "error": m.Error}
continue
}
mu.Lock()
var result any = map[string]any{}
key, _ := m.Params["key"].(string)
switch m.Method {
case "mesh/state.put":
v, _ := json.Marshal(m.Params["value"])
next++
values[key], revs[key] = v, next
mostHeld = max(mostHeld, held())
result = map[string]any{"revision": next, "fill": map[string]any{"count": len(values), "bytes": held(), "max_bytes": limit}}
case "mesh/state.delete":
delete(values, key)
case "mesh/state.keys":
keys := []string{}
for k := range values {
keys = append(keys, k)
}
sort.Strings(keys)
result = keys
case "mesh/state.page":
var entries []StateEntry
for k, v := range values {
entries = append(entries, StateEntry{Key: k, Value: v, Revision: revs[k]})
}
sort.Slice(entries, func(i, j int) bool { return entries[i].Revision < entries[j].Revision })
result = StatePage{Entries: entries}
}
mu.Unlock()
if m.ID != nil {
send(map[string]any{"jsonrpc": "2.0", "id": m.ID, "result": result})
}
}
}()
call := func(name string, args map[string]any) any {
t.Helper()
send(map[string]any{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": map[string]any{"name": name, "arguments": args}})
select {
case a := <-answers:
r, _ := a["result"].(map[string]any)
if a["error"] != nil || r == nil || r["isError"] == true {
t.Fatalf("%s answered %v %v", name, a["error"], r)
}
var out any
_ = json.Unmarshal([]byte(r["content"].([]any)[0].(map[string]any)["text"].(string)), &out)
return out
case <-time.After(3 * time.Second):
t.Fatalf("%s answered nothing", name)
return nil
}
}
value := strings.Repeat("x", 300)
call("write", map[string]any{"key": "k00", "value": value})
call("keep", map[string]any{"key": "k00"})
for _, k := range []string{"k01", "k02", "k03", "k04", "k05", "k06", "k07", "k08", "k09", "k10", "k11", "k12", "k13", "k14", "k15", "k16", "k17", "k18", "k19"} {
call("write", map[string]any{"key": k, "value": value})
mu.Lock()
over := held()*100 > 70*limit
mu.Unlock()
if over {
t.Fatalf("the state holds more than 70 %% of its cap after %s: no prune", k)
}
}
mu.Lock()
_, k00 := values["k00"]
_, k01 := values["k01"]
_, k19 := values["k19"]
_, keep := values[KeepKey("k00")]
after := held()
mu.Unlock()
if mostHeld*100 <= 70*limit {
t.Fatalf("the test never took the state past its mark (%d bytes at most)", mostHeld)
}
if !k00 || !keep || k01 || !k19 {
t.Fatalf("after the prunes k00 %v, its keep %v, k01 %v, k19 %v: want the kept key and its keep, the oldest other gone, the newest there", k00, keep, k01, k19)
}
if after*100 > 70*limit {
t.Fatalf("the state holds %d of %d bytes", after, limit)
}
if keys := call("keys", nil); strings.Contains(strings.Join(strs(keys), " "), KeepPrefix) {
t.Fatalf("Keys counted the keep: %v", keys)
}
page := call("page", nil).(map[string]any)
for _, e := range page["entries"].([]any) {
if IsKeep(e.(map[string]any)["key"].(string)) {
t.Fatalf("Page counted the keep: %v", e)
}
}
})
}
func strs(v any) []string {
var out []string
for _, s := range v.([]any) {
out = append(out, s.(string))
}
return out
}