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
This commit is contained in:
@@ -1,37 +0,0 @@
|
||||
package catalogue
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Design 47 H2 §812, §814: the validator reads every state, log and objects entry of a manifest that names no cap, in
|
||||
// the old sections and in requires alike; a capped entry, and an entry of another template, is none of them.
|
||||
func TestAManifestSaysEachKeptStoreThatNamesNoCap(t *testing.T) {
|
||||
m, err := decodeForTest([]byte(`{"module":"x","version":"1",
|
||||
"state":["seen",{"name":"held","max-mib":8}],
|
||||
"logs":[{"name":"acts"},{"name":"kept","max-mib":64}],
|
||||
"objects":[{"name":"outputs","max-mib":256}],
|
||||
"requires":[{"name":"performed","storage":"key-value","template":"state","options":{"history":1}},
|
||||
{"name":"taken","storage":"key-value","template":"state","options":{"max-mib":8,"key-ttl":true}},
|
||||
{"name":"archives","storage":"object-store","template":"objects"},
|
||||
{"name":"finding-events","storage":"stream","template":"events"}]}`))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []Uncapped{{Name: "seen", Kind: "state", OldSection: true}, {Name: "acts", Kind: "log", OldSection: true},
|
||||
{Name: "performed", Kind: "state"}, {Name: "archives", Kind: "objects"}}
|
||||
if got := m.UncappedStores(); !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("the uncapped stores are %+v, want %+v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// Design 47 H2 §810 (H-A-394): a manifest says its class, core or leaf; one that names none is a leaf.
|
||||
func TestAManifestSaysItsClassAndNoneIsALeaf(t *testing.T) {
|
||||
for raw, leaf := range map[string]bool{`"class":"core",`: false, `"class":"leaf",`: true, ``: true} {
|
||||
m, err := decodeForTest([]byte(`{` + raw + `"module":"x","version":"1"}`))
|
||||
if err != nil || m.Leaf() != leaf || m.UnknownField() != "" {
|
||||
t.Errorf("%s read as class %q, leaf %v (%v, unknown %q), want leaf %v", raw, m.Class, m.Leaf(), err, m.UnknownField(), leaf)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package catalogue
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -28,6 +29,35 @@ func TestAManifestMaySayWhatStateItKeepsAndReads(t *testing.T) {
|
||||
if string(out) != `["servers",{"name":"seen","history":5,"ttl-seconds":3600}]` {
|
||||
t.Fatalf("written back as %s", out)
|
||||
}
|
||||
// Design 47 H2 §812, §814: the validator reads every state, log and objects entry of a manifest that names no cap, in
|
||||
// the old sections and in requires alike; a capped entry, and an entry of another template, is none of them.
|
||||
t.Run("each kept store that names no cap", func(t *testing.T) {
|
||||
m, err := decodeForTest([]byte(`{"module":"x","version":"1",
|
||||
"state":["seen",{"name":"held","max-mib":8}],
|
||||
"logs":[{"name":"acts"},{"name":"kept","max-mib":64}],
|
||||
"objects":[{"name":"outputs","max-mib":256}],
|
||||
"requires":[{"name":"performed","storage":"key-value","template":"state","options":{"history":1}},
|
||||
{"name":"taken","storage":"key-value","template":"state","options":{"max-mib":8,"key-ttl":true}},
|
||||
{"name":"archives","storage":"object-store","template":"objects"},
|
||||
{"name":"finding-events","storage":"stream","template":"events"}]}`))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []Uncapped{{Name: "seen", Kind: "state", OldSection: true}, {Name: "acts", Kind: "log", OldSection: true},
|
||||
{Name: "performed", Kind: "state"}, {Name: "archives", Kind: "objects"}}
|
||||
if got := m.UncappedStores(); !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("the uncapped stores are %+v, want %+v", got, want)
|
||||
}
|
||||
})
|
||||
// Design 47 H2 §810 (H-A-394): a manifest says its class, core or leaf; one that names none is a leaf.
|
||||
t.Run("its class, none a leaf", func(t *testing.T) {
|
||||
for raw, leaf := range map[string]bool{`"class":"core",`: false, `"class":"leaf",`: true, ``: true} {
|
||||
m, err := decodeForTest([]byte(`{` + raw + `"module":"x","version":"1"}`))
|
||||
if err != nil || m.Leaf() != leaf || m.UnknownField() != "" {
|
||||
t.Errorf("%s read as class %q, leaf %v (%v, unknown %q), want leaf %v", raw, m.Class, m.Leaf(), err, m.UnknownField(), leaf)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 1154/AC1: a state entry may say key-ttl; it is read, written back, and a bare name or an entry without it reads as before.
|
||||
|
||||
@@ -1,100 +0,0 @@
|
||||
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")
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,6 @@ package process
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
|
||||
@@ -64,54 +60,3 @@ func (f *pruneKV) Purge(ctx context.Context, key string, _ ...jetstream.KVDelete
|
||||
type putEntry struct{ fakeEntry }
|
||||
|
||||
func (putEntry) Operation() jetstream.KeyValueOp { return jetstream.KeyValuePut }
|
||||
|
||||
// Design 47 H2 §812 (H-A-380, H-A-386): a process's write that takes its state past prune-mark prunes it, oldest first
|
||||
// by revision, to prune-target, in one journal line, reading the figures only at the bind and after the prune; a key
|
||||
// kept under _keep/<key> survives, and no reader counts the keep.
|
||||
func TestAProcessWritePastItsMarkPrunesToItsTargetAndAKeptKeySurvives(t *testing.T) {
|
||||
var lines []string
|
||||
sayPrune = func(line string) { lines = append(lines, line) }
|
||||
t.Cleanup(func() { sayPrune = func(string) {} })
|
||||
kv := &pruneKV{fakeKV: newFakeKV(), cap: 10_000}
|
||||
s, err := stateOn(kv).Pruned(stdio.Prune{Mark: 70, Target: 50})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
value := strings.Repeat("x", 300)
|
||||
if _, err := s.Put("k00", value); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Keep("k00", time.Now().Add(time.Hour)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Keep("k01", time.Now().Add(-time.Hour)); err == nil {
|
||||
t.Fatal("a keep until a time past was taken")
|
||||
}
|
||||
for i := 1; i < 18; i++ {
|
||||
if _, err := s.Put(fmt.Sprintf("k%02d", i), value); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(lines) == 0 && kv.held()*100 > 70*10_000 {
|
||||
t.Fatalf("the state holds %d of 10000 bytes after k%02d and was not pruned", kv.held(), i)
|
||||
}
|
||||
}
|
||||
if len(lines) != 1 || !strings.Contains(lines[0], "pruned the state mesh-mail's sends") {
|
||||
t.Fatalf("the prunes said %q, want one line", lines)
|
||||
}
|
||||
if kv.statuss != 2 {
|
||||
t.Fatalf("the figures were read %d times, want 2: at the bind and after the prune", kv.statuss)
|
||||
}
|
||||
if held := kv.held(); held*100 > 50*10_000 {
|
||||
t.Fatalf("the prune left %d of 10000 bytes, above its target of 50 %%", held)
|
||||
}
|
||||
keys, err := s.Keys()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if keys[0] != "k00" || strings.Contains(strings.Join(keys, " "), "_keep") || keys[1] == "k01" || keys[len(keys)-1] != "k17" {
|
||||
t.Fatalf("after the prune the readers see %v: want k00 kept, the oldest others gone, k17 there, no keep", keys)
|
||||
}
|
||||
if e, _ := s.Get(stdio.KeepKey("k00")); e == nil {
|
||||
t.Fatal("the keep itself was pruned")
|
||||
}
|
||||
}
|
||||
|
||||
+67
-18
@@ -3,6 +3,7 @@ package process
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -250,6 +251,56 @@ func TestStatePutWritesAKeyWhole(t *testing.T) {
|
||||
t.Errorf("seat %q, ask %q was taken", bad[0], bad[1])
|
||||
}
|
||||
}
|
||||
// Design 47 H2 §812 (H-A-380, H-A-386): a process's write that takes its state past prune-mark prunes it, oldest first
|
||||
// by revision, to prune-target, in one journal line, reading the figures only at the bind and after the prune; a key
|
||||
// kept under _keep/<key> survives, and no reader counts the keep.
|
||||
t.Run("a write past its mark prunes to its target and a kept key survives", func(t *testing.T) {
|
||||
var lines []string
|
||||
sayPrune = func(line string) { lines = append(lines, line) }
|
||||
t.Cleanup(func() { sayPrune = func(string) {} })
|
||||
kv := &pruneKV{fakeKV: newFakeKV(), cap: 10_000}
|
||||
s, err := stateOn(kv).Pruned(stdio.Prune{Mark: 70, Target: 50})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
value := strings.Repeat("x", 300)
|
||||
if _, err := s.Put("k00", value); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Keep("k00", time.Now().Add(time.Hour)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Keep("k01", time.Now().Add(-time.Hour)); err == nil {
|
||||
t.Fatal("a keep until a time past was taken")
|
||||
}
|
||||
for i := 1; i < 18; i++ {
|
||||
if _, err := s.Put(fmt.Sprintf("k%02d", i), value); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(lines) == 0 && kv.held()*100 > 70*10_000 {
|
||||
t.Fatalf("the state holds %d of 10000 bytes after k%02d and was not pruned", kv.held(), i)
|
||||
}
|
||||
}
|
||||
if len(lines) != 1 || !strings.Contains(lines[0], "pruned the state mesh-mail's sends") {
|
||||
t.Fatalf("the prunes said %q, want one line", lines)
|
||||
}
|
||||
if kv.statuss != 2 {
|
||||
t.Fatalf("the figures were read %d times, want 2: at the bind and after the prune", kv.statuss)
|
||||
}
|
||||
if held := kv.held(); held*100 > 50*10_000 {
|
||||
t.Fatalf("the prune left %d of 10000 bytes, above its target of 50 %%", held)
|
||||
}
|
||||
keys, err := s.Keys()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if keys[0] != "k00" || strings.Contains(strings.Join(keys, " "), "_keep") || keys[1] == "k01" || keys[len(keys)-1] != "k17" {
|
||||
t.Fatalf("after the prune the readers see %v: want k00 kept, the oldest others gone, k17 there, no keep", keys)
|
||||
}
|
||||
if e, _ := s.Get(stdio.KeepKey("k00")); e == nil {
|
||||
t.Fatal("the keep itself was pruned")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 1840/S1: given a state, when its keys are listed, then they come sorted, and none is an empty list, not an error.
|
||||
@@ -321,25 +372,23 @@ func TestStateWatchHandsCurrentValuesThenChanges(t *testing.T) {
|
||||
t.Fatalf("no change %+v", want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 1840/S2: a key narrowing reaches the bus as a subject pattern: `*` one name, `**` the rest, empty every key.
|
||||
func TestStateWatchNarrowing(t *testing.T) {
|
||||
for key, want := range map[string]string{"": ">", "a.b": "a.b", "a.*": "a.*", "a.**": "a.>", "**": ">"} {
|
||||
if got := watchPattern(key); got != want {
|
||||
t.Errorf("watchPattern(%q) = %q, want %q", key, got, want)
|
||||
// 1840/S2: a key narrowing reaches the bus as a subject pattern: `*` one name, `**` the rest, empty every key.
|
||||
t.Run("narrowing", func(t *testing.T) {
|
||||
for key, want := range map[string]string{"": ">", "a.b": "a.b", "a.*": "a.*", "a.**": "a.>", "**": ">"} {
|
||||
if got := watchPattern(key); got != want {
|
||||
t.Errorf("watchPattern(%q) = %q, want %q", key, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 1840/S2: given a handler that refuses a current value, then Watch returns its error.
|
||||
func TestStateWatchReturnsAHandlersErrorOnCurrentValues(t *testing.T) {
|
||||
kv := newFakeKV()
|
||||
kv.initial = []jetstream.KeyValueEntry{watchEntry{fakeEntry{key: "a", value: []byte(`1`), rev: 1}, jetstream.KeyValuePut}}
|
||||
boom := errors.New("boom")
|
||||
if err := stateOn(kv).Watch("", func(stdio.StateChange) error { return boom }); !errors.Is(err, boom) {
|
||||
t.Fatalf("Watch: %v, want the handler's error", err)
|
||||
}
|
||||
})
|
||||
// 1840/S2: given a handler that refuses a current value, then Watch returns its error.
|
||||
t.Run("a handler's error on the current values is returned", func(t *testing.T) {
|
||||
kv := newFakeKV()
|
||||
kv.initial = []jetstream.KeyValueEntry{watchEntry{fakeEntry{key: "a", value: []byte(`1`), rev: 1}, jetstream.KeyValuePut}}
|
||||
boom := errors.New("boom")
|
||||
if err := stateOn(kv).Watch("", func(stdio.StateChange) error { return boom }); !errors.Is(err, boom) {
|
||||
t.Fatalf("Watch: %v, want the handler's error", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 1840/S2: given a connection that closes before the current values arrive, then Watch returns an error.
|
||||
|
||||
@@ -1,177 +0,0 @@
|
||||
package stdio
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 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.
|
||||
func TestABundleWritePastItsMarkPrunesToItsTargetAndAKeptKeySurvives(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
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/crypto/nacl/box"
|
||||
@@ -56,40 +55,6 @@ func anonymousVectors(t *testing.T) anonymousFixture {
|
||||
return f
|
||||
}
|
||||
|
||||
// The anonymous-box fixture the TypeScript tests read is what box.SealAnonymous makes, and each opens with the
|
||||
// recipient's key as the vault's intake opens it (box.OpenAnonymous). MESH_SDK_WRITE_FIXTURES=1 writes it anew.
|
||||
func TestTheAnonymousBoxFixtureIsGosBytes(t *testing.T) {
|
||||
want := anonymousVectors(t)
|
||||
if os.Getenv("MESH_SDK_WRITE_FIXTURES") == "1" {
|
||||
raw, _ := json.MarshalIndent(want, "", " ")
|
||||
if err := os.WriteFile(anonymousFixturePath, append(raw, '\n'), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
raw, err := os.ReadFile(anonymousFixturePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var got anonymousFixture
|
||||
if err := json.Unmarshal(raw, &got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if g, w := mustJSON(got), mustJSON(want); g != w {
|
||||
t.Fatalf("the fixture is not Go's bytes:\n%s\nwant\n%s", g, w)
|
||||
}
|
||||
var pub, priv [32]byte
|
||||
p, _ := base64.StdEncoding.DecodeString(got.Recipient.Public)
|
||||
k, _ := base64.StdEncoding.DecodeString(got.Recipient.Private)
|
||||
copy(pub[:], p)
|
||||
copy(priv[:], k)
|
||||
for _, v := range got.Vectors {
|
||||
sealed, _ := base64.StdEncoding.DecodeString(v.Sealed)
|
||||
if out, ok := box.OpenAnonymous(nil, sealed, &pub, &priv); !ok || string(out) != v.Plaintext {
|
||||
t.Errorf("a box does not open to %q: %q, %v", v.Plaintext, out, ok)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func mustJSON(v any) string {
|
||||
raw, _ := json.Marshal(v)
|
||||
return string(raw)
|
||||
|
||||
+68
-35
@@ -6,6 +6,8 @@ import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/crypto/nacl/box"
|
||||
)
|
||||
|
||||
type fixture struct {
|
||||
@@ -75,6 +77,39 @@ func TestSealIsByteForByteTheLicenceManagersBox(t *testing.T) {
|
||||
t.Fatalf("reopened %q, %v", plain, err)
|
||||
}
|
||||
}
|
||||
// The anonymous-box fixture the TypeScript tests read is what box.SealAnonymous makes, and each opens with the
|
||||
// recipient's key as the vault's intake opens it (box.OpenAnonymous). MESH_SDK_WRITE_FIXTURES=1 writes it anew.
|
||||
t.Run("the anonymous box fixture is Go's bytes", func(t *testing.T) {
|
||||
want := anonymousVectors(t)
|
||||
if os.Getenv("MESH_SDK_WRITE_FIXTURES") == "1" {
|
||||
raw, _ := json.MarshalIndent(want, "", " ")
|
||||
if err := os.WriteFile(anonymousFixturePath, append(raw, '\n'), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
raw, err := os.ReadFile(anonymousFixturePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var got anonymousFixture
|
||||
if err := json.Unmarshal(raw, &got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if g, w := mustJSON(got), mustJSON(want); g != w {
|
||||
t.Fatalf("the fixture is not Go's bytes:\n%s\nwant\n%s", g, w)
|
||||
}
|
||||
var pub, priv [32]byte
|
||||
p, _ := base64.StdEncoding.DecodeString(got.Recipient.Public)
|
||||
k, _ := base64.StdEncoding.DecodeString(got.Recipient.Private)
|
||||
copy(pub[:], p)
|
||||
copy(priv[:], k)
|
||||
for _, v := range got.Vectors {
|
||||
sealed, _ := base64.StdEncoding.DecodeString(v.Sealed)
|
||||
if out, ok := box.OpenAnonymous(nil, sealed, &pub, &priv); !ok || string(out) != v.Plaintext {
|
||||
t.Errorf("a box does not open to %q: %q, %v", v.Plaintext, out, ok)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// A box the agent module's TypeScript sealed (the fixture the licence manager keeps) opens here too.
|
||||
@@ -117,40 +152,38 @@ func TestABoxOpensOnlyForItsRecipient(t *testing.T) {
|
||||
if again.Eph == box.Eph || again.IV == box.IV {
|
||||
t.Fatal("two boxes share a one-time key or iv")
|
||||
}
|
||||
}
|
||||
|
||||
// A box changed in any field, or of another version, does not open.
|
||||
func TestATamperedBoxDoesNotOpen(t *testing.T) {
|
||||
f := readFixture(t)
|
||||
good := f.Sealed[1].Box
|
||||
flip := func(s string) string {
|
||||
b, _ := base64.StdEncoding.DecodeString(s)
|
||||
b[len(b)-1] ^= 1
|
||||
return base64.StdEncoding.EncodeToString(b)
|
||||
}
|
||||
for name, box := range map[string]Box{
|
||||
"ct": {V: 1, Eph: good.Eph, IV: good.IV, Tag: good.Tag, Ct: flip(good.Ct)},
|
||||
"tag": {V: 1, Eph: good.Eph, IV: good.IV, Tag: flip(good.Tag), Ct: good.Ct},
|
||||
"iv": {V: 1, Eph: good.Eph, IV: flip(good.IV), Tag: good.Tag, Ct: good.Ct},
|
||||
"eph": {V: 1, Eph: flip(good.Eph), IV: good.IV, Tag: good.Tag, Ct: good.Ct},
|
||||
"version": {V: 2, Eph: good.Eph, IV: good.IV, Tag: good.Tag, Ct: good.Ct},
|
||||
} {
|
||||
if _, err := Open(box, f.Recipient.PrivateKey); err == nil {
|
||||
t.Fatalf("a box with a changed %s opened", name)
|
||||
// A box changed in any field, or of another version, does not open.
|
||||
t.Run("a tampered box does not open", func(t *testing.T) {
|
||||
f := readFixture(t)
|
||||
good := f.Sealed[1].Box
|
||||
flip := func(s string) string {
|
||||
b, _ := base64.StdEncoding.DecodeString(s)
|
||||
b[len(b)-1] ^= 1
|
||||
return base64.StdEncoding.EncodeToString(b)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A key that is not X25519 PEM is refused, not misread.
|
||||
func TestAKeyOfAnotherKindIsRefused(t *testing.T) {
|
||||
if _, err := Seal("x", "not a key"); err == nil {
|
||||
t.Fatal("sealed to text")
|
||||
}
|
||||
f := readFixture(t)
|
||||
if _, err := Open(f.Sealed[0].Box, f.Recipient.PublicKey); err == nil {
|
||||
t.Fatal("opened with a public key")
|
||||
}
|
||||
if _, err := Seal("x", f.Recipient.PrivateKey); err == nil {
|
||||
t.Fatal("sealed to a private key")
|
||||
}
|
||||
for name, box := range map[string]Box{
|
||||
"ct": {V: 1, Eph: good.Eph, IV: good.IV, Tag: good.Tag, Ct: flip(good.Ct)},
|
||||
"tag": {V: 1, Eph: good.Eph, IV: good.IV, Tag: flip(good.Tag), Ct: good.Ct},
|
||||
"iv": {V: 1, Eph: good.Eph, IV: flip(good.IV), Tag: good.Tag, Ct: good.Ct},
|
||||
"eph": {V: 1, Eph: flip(good.Eph), IV: good.IV, Tag: good.Tag, Ct: good.Ct},
|
||||
"version": {V: 2, Eph: good.Eph, IV: good.IV, Tag: good.Tag, Ct: good.Ct},
|
||||
} {
|
||||
if _, err := Open(box, f.Recipient.PrivateKey); err == nil {
|
||||
t.Fatalf("a box with a changed %s opened", name)
|
||||
}
|
||||
}
|
||||
})
|
||||
// A key that is not X25519 PEM is refused, not misread.
|
||||
t.Run("a key of another kind is refused", func(t *testing.T) {
|
||||
if _, err := Seal("x", "not a key"); err == nil {
|
||||
t.Fatal("sealed to text")
|
||||
}
|
||||
f := readFixture(t)
|
||||
if _, err := Open(f.Sealed[0].Box, f.Recipient.PublicKey); err == nil {
|
||||
t.Fatal("opened with a public key")
|
||||
}
|
||||
if _, err := Seal("x", f.Recipient.PrivateKey); err == nil {
|
||||
t.Fatal("sealed to a private key")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -6,7 +6,9 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -355,4 +357,256 @@ func TestABundleKeepsAndWatchesStateThroughTheRuntime(t *testing.T) {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -50,6 +50,28 @@ func TestBusAnswersPublishAndKeyValueCreate(t *testing.T) {
|
||||
if string(entry.Value()) != "v" {
|
||||
t.Errorf("read back %q, want %q", entry.Value(), "v")
|
||||
}
|
||||
// Design 47 H2 §800, H-A-368: the setup bound is half of the level's bound, 30 s when the level has none, and a starved
|
||||
// setup fails with the one line a build module reads.
|
||||
t.Run("the setup bound is half the level's and a starved setup says so", func(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
level string
|
||||
want time.Duration
|
||||
fails bool
|
||||
}{{"", 30 * time.Second, false}, {"10m", 5 * time.Minute, false}, {"ten", 0, true}, {"-1s", 0, true}} {
|
||||
t.Setenv(levelBoundWord, c.level)
|
||||
f := &failed{TB: t}
|
||||
var got time.Duration
|
||||
catch(f, func() { got = SetupBound(f) })
|
||||
if c.fails != (f.words != "") || (!c.fails && got != c.want) {
|
||||
t.Errorf("level %q: bound %s, failed %q", c.level, got, f.words)
|
||||
}
|
||||
}
|
||||
f := &failed{TB: t}
|
||||
catch(f, func() { Starved(f, "the stream did not answer within 5s") })
|
||||
if f.words != "mesh: setup starved: the stream did not answer within 5s" {
|
||||
t.Errorf("a starved setup said %q", f.words)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 1175/AC3, 1171/AC1: the bus is gone when the test that started it ends.
|
||||
|
||||
@@ -3,7 +3,6 @@ package testbus
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// failed is a testing.TB that keeps what a helper failed with, so the helper's own failure is read, not suffered.
|
||||
@@ -26,26 +25,3 @@ func catch(f *failed, run func()) {
|
||||
}()
|
||||
run()
|
||||
}
|
||||
|
||||
// Design 47 H2 §800, H-A-368: the setup bound is half of the level's bound, 30 s when the level has none, and a starved
|
||||
// setup fails with the one line a build module reads.
|
||||
func TestTheSetupBoundIsHalfTheLevelsAndAStarvedSetupSaysSo(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
level string
|
||||
want time.Duration
|
||||
fails bool
|
||||
}{{"", 30 * time.Second, false}, {"10m", 5 * time.Minute, false}, {"ten", 0, true}, {"-1s", 0, true}} {
|
||||
t.Setenv(levelBoundWord, c.level)
|
||||
f := &failed{TB: t}
|
||||
var got time.Duration
|
||||
catch(f, func() { got = SetupBound(f) })
|
||||
if c.fails != (f.words != "") || (!c.fails && got != c.want) {
|
||||
t.Errorf("level %q: bound %s, failed %q", c.level, got, f.words)
|
||||
}
|
||||
}
|
||||
f := &failed{TB: t}
|
||||
catch(f, func() { Starved(f, "the stream did not answer within 5s") })
|
||||
if f.words != "mesh: setup starved: the stream did not answer within 5s" {
|
||||
t.Errorf("a starved setup said %q", f.words)
|
||||
}
|
||||
}
|
||||
|
||||
+46
-47
@@ -1441,6 +1441,52 @@ func (s *pagedSource) Page(after string, _ int, _ time.Duration) ([]KeyedEntry,
|
||||
func TestViews(t *testing.T) {
|
||||
t.Run("Views", subViews)
|
||||
t.Run("StdioWriterPublishesAndListensThroughTheModulesOwnPlumbing", subStdioWriterPublishesAndListensThroughTheModulesOwnPlumbing)
|
||||
// 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.
|
||||
t.Run("one process reader serves concurrent reads", func(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()
|
||||
})
|
||||
}
|
||||
|
||||
type putRecorder struct{ puts map[string][]byte }
|
||||
@@ -1488,50 +1534,3 @@ 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()
|
||||
}
|
||||
|
||||
+3
-6
@@ -154,7 +154,9 @@ test("asked for again, a retired consumer is created again and announced re-enab
|
||||
}
|
||||
});
|
||||
|
||||
test("five passes are not enough: the same set must also hold for stableForMs", async () => {
|
||||
test("five passes are not enough: the same set must also hold for stableForMs, ten minutes by default", async () => {
|
||||
const { STABLE_FOR_MS } = await import("../dist/provider-loop/index.js");
|
||||
assert.equal(STABLE_FOR_MS, 10 * 60_000);
|
||||
const HOLD = 400;
|
||||
const w = await world(["a", "b", "c"], { stableForMs: HOLD });
|
||||
try {
|
||||
@@ -170,11 +172,6 @@ test("five passes are not enough: the same set must also hold for stableForMs",
|
||||
}
|
||||
});
|
||||
|
||||
test("the default hold is ten minutes", async () => {
|
||||
const { STABLE_FOR_MS } = await import("../dist/provider-loop/index.js");
|
||||
assert.equal(STABLE_FOR_MS, 10 * 60_000);
|
||||
});
|
||||
|
||||
test("an adapter without retire is MARK-ONLY: nothing changes in the backend, remove only on delete", async () => {
|
||||
const w = await world(["a", "b", "c"], { retire: false, del: false });
|
||||
try {
|
||||
|
||||
+10
-12
@@ -13,7 +13,7 @@ const fixture = (name: string) => JSON.parse(readFileSync(join(testdata, name),
|
||||
// ADR 0206's box: every box Go's seal opens (the licence manager's, the agent module's TypeScript) opens here; given
|
||||
// the same one-time key and iv, seal makes Go's box byte for byte, so go/seal's Open opens it; another key and a
|
||||
// changed field are refused.
|
||||
test("seal and open make and open go/seal's box byte for byte", () => {
|
||||
test("seal and open make and open go/seal's box, and sealAnonymous Go's anonymous box, byte for byte", () => {
|
||||
const f = fixture("sealed-by-licence-manager.json");
|
||||
for (const s of f.sealed as { plaintext: string; box: Box }[]) {
|
||||
assert.equal(open(s.box, f.recipient.privateKey), s.plaintext);
|
||||
@@ -33,19 +33,17 @@ test("seal and open make and open go/seal's box byte for byte", () => {
|
||||
assert.throws(() => open(box, b.privateKey), /does not open with this key/);
|
||||
assert.throws(() => open({ ...box, ct: Buffer.from("changed").toString("base64") }, a.privateKey), /does not open/);
|
||||
assert.throws(() => open({ ...box, v: 2 }, a.privateKey), /version 2/);
|
||||
});
|
||||
|
||||
// The NaCl anonymous box the vault's intake opens: given the same one-time key, sealAnonymous makes what
|
||||
// golang.org/x/crypto/nacl/box's SealAnonymous made, byte for byte; a fresh one differs and is as long.
|
||||
test("sealAnonymous makes Go's anonymous box byte for byte", () => {
|
||||
const f = fixture("sealed-anonymous-by-go.json");
|
||||
assert.ok(f.vectors.length > 0);
|
||||
for (const v of f.vectors as { plaintext: string; ephSecret: string; sealed: string }[]) {
|
||||
assert.equal(sealAnonymousWith(v.plaintext, f.recipient.public, Buffer.from(v.ephSecret, "base64")), v.sealed);
|
||||
assert.equal(sealAnonymousWith(Buffer.from(v.plaintext, "utf8"), f.recipient.public, Buffer.from(v.ephSecret, "base64")), v.sealed);
|
||||
// The NaCl anonymous box the vault's intake opens: given the same one-time key, sealAnonymous makes what
|
||||
// golang.org/x/crypto/nacl/box's SealAnonymous made, byte for byte; a fresh one differs and is as long.
|
||||
const an = fixture("sealed-anonymous-by-go.json");
|
||||
assert.ok(an.vectors.length > 0);
|
||||
for (const v of an.vectors as { plaintext: string; ephSecret: string; sealed: string }[]) {
|
||||
assert.equal(sealAnonymousWith(v.plaintext, an.recipient.public, Buffer.from(v.ephSecret, "base64")), v.sealed);
|
||||
assert.equal(sealAnonymousWith(Buffer.from(v.plaintext, "utf8"), an.recipient.public, Buffer.from(v.ephSecret, "base64")), v.sealed);
|
||||
}
|
||||
const fresh = sealAnonymous("a token", f.recipient.public);
|
||||
assert.notEqual(fresh, sealAnonymous("a token", f.recipient.public));
|
||||
const fresh = sealAnonymous("a token", an.recipient.public);
|
||||
assert.notEqual(fresh, sealAnonymous("a token", an.recipient.public));
|
||||
assert.equal(Buffer.from(fresh, "base64").length, 32 + 16 + "a token".length);
|
||||
assert.throws(() => sealAnonymous("x", "c2hvcnQ="), /not a raw X25519 public key/);
|
||||
});
|
||||
|
||||
@@ -224,7 +224,7 @@ test("a failing consumer the mesh stopped asking for is said recovered when it i
|
||||
}
|
||||
});
|
||||
|
||||
test("an error is classed by its words", () => {
|
||||
test("an error is classed by its words, and an adapter that classes its own errors is believed", async () => {
|
||||
const table: Record<string, string> = {
|
||||
'Keycloak token request failed: 401 {"error":"invalid_grant"}': CLASS_CREDENTIALS,
|
||||
'FATAL: password authentication failed for user "postgres"': CLASS_CREDENTIALS,
|
||||
@@ -235,9 +235,7 @@ test("an error is classed by its words", () => {
|
||||
'extension "nope" is not available': CLASS_REFUSED,
|
||||
};
|
||||
for (const [text, want] of Object.entries(table)) assert.equal(classOf(text), want, text);
|
||||
});
|
||||
|
||||
test("an adapter that classes its own errors is believed", async () => {
|
||||
const w = await world({ classify: true });
|
||||
try {
|
||||
w.failing = new Error("anything");
|
||||
|
||||
Reference in New Issue
Block a user