Design 47 H2: the kept call paced, take once, page by cursor, settings, class and caps, check-needs #209

Merged
mesh-tools merged 35 commits from feat/47-H2 into main 2026-11-22 03:50:59 +00:00
3 changed files with 174 additions and 0 deletions
Showing only changes of commit eee02a20a9 - Show all commits
+100
View File
@@ -0,0 +1,100 @@
package stdio
import (
"bufio"
"encoding/json"
"fmt"
"io"
"sync"
"testing"
"time"
)
// 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.
func TestOneRuntimeChannelServesConcurrentStateCalls(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")
}
}
+27
View File
@@ -4,7 +4,9 @@ package process
import (
"errors"
"fmt"
"strings"
"sync"
"testing"
"time"
@@ -199,6 +201,31 @@ func TestStatePutAndKeysOnAGrantedBucket(t *testing.T) {
if err != nil || strings.Join(keys, ",") != "a,b" {
t.Fatalf("Keys: %v, %v, want a,b", keys, err)
}
// Design 47 H2 §782: one connection's state calls are safe at once: gets, puts, keys and watches from many goroutines
// through the one handle, under the race detector.
var wg sync.WaitGroup
errs := make(chan error, 16*4)
for i := range 16 {
wg.Add(1)
go func() {
defer wg.Done()
key := fmt.Sprintf("c%d", i)
_, err := s.Put(key, i)
errs <- err
_, err = s.Get(key)
errs <- err
_, err = s.Keys()
errs <- err
errs <- s.Watch(key, func(stdio.StateChange) error { return nil })
}()
}
wg.Wait()
close(errs)
for err := range errs {
if err != nil {
t.Fatalf("a concurrent state call: %v", err)
}
}
}
// 1840/S2: given a process connection granted its bucket, which already holds two keys and a deleted one,
+47
View File
@@ -1488,3 +1488,50 @@ func TestFirstViewsSayTheOwnBuild(t *testing.T) {
t.Errorf("nothing kept: %v", read)
}
}
// Design 47 H2 §782: one process reader's pictures are safe at once: gets, keys and watches from many goroutines while
// the owner's events change the picture and further views are followed, under the race detector.
func TestOneProcessReaderServesConcurrentReads(t *testing.T) {
made := time.Date(2026, 11, 22, 0, 0, 0, 0, time.UTC)
r := &ProcessReader{Module: "reader", Call: func(string, any) (json.RawMessage, error) {
return json.Marshal(Answer{V: Version, Owner: "o", State: "s", Revision: 1, Created: made, Values: map[string]json.RawMessage{"a": json.RawMessage(`1`)}})
}}
p, err := r.Follow("o", "s", "o.o_read")
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if err := p.Follower().Start(ctx); err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for i := range 200 {
p.Follower().Apply(Event{V: Version, Owner: "o", State: "s", Key: fmt.Sprintf("k%d", i%7), Revision: uint64(i + 2), Created: made, Value: json.RawMessage(`2`)})
}
}()
for i := range 16 {
wg.Add(1)
go func() {
defer wg.Done()
w, err := p.Watch(ctx, ">")
if err != nil {
t.Error(err)
return
}
defer w.Stop()
for range 20 {
_, _ = p.Get(ctx, "a")
_, _ = p.Keys(ctx)
_ = r.Followers()
}
if _, err := r.Follow("o", fmt.Sprintf("s%d", i), "o.o_read"); err != nil {
t.Error(err)
}
}()
}
wg.Wait()
}