A process judges a running sum of what its writes put against the figures read at the bind and after each prune; a bundle judges the fill the runtime's put answer carries. Past prune-mark (70 %) the oldest keys by revision go down to prune-target (50 %), in one journal line; a key kept under _keep/<key> stays, and no reader counts the keep. The generic tools <module>_state_prune_now and <module>_state_keep act through the hand-act log. Builds: design 47 H2 §812, H-A-380, H-A-386
178 lines
6.1 KiB
Go
178 lines
6.1 KiB
Go
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
|
|
}
|