Files
mesh-tools/node-tools/internal/runtime/state_history_test.go
T
jschoubben cab76319b8 R1-1: a page hands a kept delete, so a deleted key never comes back alive
The page watch ignored delete markers, so a reader following pages of a bucket with history kept
a deleted key's last value. A kept delete (a purge reads as one) is now an entry deleted: true with
no value at its sequence, seen-again like any message (review round 1, finding 1, state.go:375).

Builds: design 47 H2 §779, H-A-451 (1), H-A-382 (2)
2026-11-22 02:19:43 +01:00

252 lines
11 KiB
Go

package runtime
import (
"encoding/json"
"errors"
"strings"
"testing"
"time"
"git.novox.be/novox/mesh-sdk/go/buscalls"
"github.com/novox/mesh-tools/node-tools/internal/bus"
"github.com/novox/mesh-tools/node-tools/internal/launch"
mt "github.com/novox/mesh-tools/node-tools/internal/meshtest"
)
// historyBuses are three modules on one runtime, each reached as a bundle reaches it (novox/hq ADR
// 0307): issues keeps a state `issues` in a bucket keeping 8 values a key, triage reads it as
// `issues.issues`, and other was issued no state.
func historyBuses(t *testing.T) (owner, reader, other launch.Bus) {
t.Helper()
mesh := mt.New(t)
mesh.BucketKeeping(t, "issues_issues", 8)
m := mt.MembershipOf("issues", "anchor", false, nil)
m.State = []bus.StateIssued{{Name: "issues", Bucket: "issues_issues", Writes: true}}
mesh.Issue(t, m)
r := mt.MembershipOf("triage", "anchor", false, nil)
r.State = []bus.StateIssued{{Name: "issues.issues", Bucket: "issues_issues"}}
mesh.Issue(t, r)
mesh.Issue(t, mt.MembershipOf("other", "anchor", false, nil))
conn := connect(t, "node-tools", "anchor")
for _, module := range []string{"issues", "triage", "other"} {
conn.Follow(module)
}
mt.Until(t, func() error {
for _, module := range []string{"issues", "triage", "other"} {
if conn.Membership(module) == nil {
return errors.New(module + " not issued yet")
}
}
return nil
})
all := &consumers{conn: conn, logf: t.Logf, of: map[string]*moduleEvents{}}
return all.forModule("issues"), all.forModule("triage"), all.forModule("other")
}
// kept is one value of a history answer, by the field names on the wire.
type kept struct {
Key string `json:"key"`
Value json.RawMessage `json:"value"`
Revision uint64 `json:"revision"`
Op string `json:"op"`
At string `json:"at"`
}
func historyOf(t *testing.T, b launch.Bus, state, key string) []kept {
t.Helper()
raw, err := ask(b, "history", map[string]any{"state": state, "key": key})
if err != nil {
t.Fatalf("history of %s %s: %v", state, key, err)
}
var h []kept
if err := json.Unmarshal(raw, &h); err != nil || h == nil {
t.Fatalf("history answered %s, not an array", raw)
}
return h
}
// A key's history is every value the bucket keeps, oldest first, each with its revision, op and time
// in RFC 3339 — a delete with no value; an unknown key's is []. The owner and a reader are served it,
// and reading it writes nothing.
func TestAKeysHistoryIsEveryValueKeptOldestFirst(t *testing.T) {
owner, reader, _ := historyBuses(t)
before := time.Now().Add(-time.Second)
var revisions []uint64
// each put answers the bucket's fill with its revision, buscalls.Fill's form (design 47 H2 DMC-14, H-A-386): the messages
// kept (every value of the key, the bucket keeping 8), their bytes, and no cap (0)
var bytes uint64
for i, v := range []string{`{"state":"open"}`, `{"state":"fixing"}`, `{"state":"resolved"}`} {
raw, err := ask(owner, "put", map[string]any{"state": "issues", "key": "502", "value": json.RawMessage(v)})
if err != nil {
t.Fatal(err)
}
var r struct {
Revision uint64 `json:"revision"`
Fill *buscalls.Fill `json:"fill"`
}
_ = json.Unmarshal(raw, &r)
revisions = append(revisions, r.Revision)
if r.Fill == nil || r.Fill.Count != uint64(i+1) || r.Fill.Bytes <= bytes || r.Fill.MaxBytes != 0 {
t.Errorf("put %d answered %s, want its revision and the fill of %d messages, more bytes than %d, no cap", i, raw, i+1, bytes)
}
if r.Fill != nil {
bytes = r.Fill.Bytes
}
}
if _, err := ask(owner, "put", map[string]any{"state": "issues", "key": "503", "value": 1}); err != nil {
t.Fatal(err)
}
if _, err := ask(owner, "delete", map[string]any{"state": "issues", "key": "502"}); err != nil {
t.Fatal(err)
}
for who, b := range map[string]launch.Bus{"the owner": owner, "a reader": reader} {
name := "issues"
if b == reader {
name = "issues.issues"
}
h := historyOf(t, b, name, "502")
if len(h) != 4 {
t.Fatalf("%s read %d values of 502, not 4: %+v", who, len(h), h)
}
for i, v := range []string{`{"state":"open"}`, `{"state":"fixing"}`, `{"state":"resolved"}`} {
if h[i].Op != "put" || string(h[i].Value) != v || h[i].Revision != revisions[i] || h[i].Key != "502" {
t.Errorf("%s: value %d is %+v", who, i, h[i])
}
}
if h[3].Op != "delete" || len(h[3].Value) != 0 && string(h[3].Value) != "null" || h[3].Revision <= revisions[2] {
t.Errorf("%s: the delete reads %+v", who, h[3])
}
for i, k := range h {
at, err := time.Parse(time.RFC3339, k.At)
if err != nil || at.Before(before) || at.After(time.Now().Add(time.Second)) {
t.Errorf("%s: value %d's time %q is not RFC 3339 and now: %v", who, i, k.At, err)
}
if i > 0 && k.Revision <= h[i-1].Revision {
t.Errorf("%s: not oldest first: %+v", who, h)
}
}
if h := historyOf(t, b, name, "999"); len(h) != 0 {
t.Errorf("%s: an unknown key's history is %+v, not []", who, h)
}
}
// The history is read, never written: the key is still deleted and keeps the same four values.
if raw, err := ask(owner, "get", map[string]any{"state": "issues", "key": "502"}); err != nil || string(raw) != "null" {
t.Fatalf("after history, 502 reads %s %v", raw, err)
}
if h := historyOf(t, owner, "issues", "502"); len(h) != 4 {
t.Fatalf("reading the history changed it: %+v", h)
}
// A key the bucket holds nothing for answers [] on the wire, not null.
if raw, _ := ask(owner, "history", map[string]any{"state": "issues", "key": "999"}); string(raw) != "[]" {
t.Fatalf("an unknown key's history is %s on the wire", raw)
}
}
// A module the bucket was not issued to is refused its history, and a page, with the reason, as it is a get; the
// owner and a reader are served a page.
func TestAKeysHistoryAndAPageOfAStateAreServedToOwnerAndReaderAndRefusedToAModuleNotIssuedIt(t *testing.T) {
owner, reader, other := historyBuses(t)
if _, err := ask(owner, "put", map[string]any{"state": "issues", "key": "1", "value": 1}); err != nil {
t.Fatal(err)
}
for _, name := range []string{"issues", "issues.issues"} {
_, err := ask(other, "history", map[string]any{"state": name, "key": "1"})
if err == nil || !strings.Contains(err.Error(), "keeps and reads no state called") ||
!strings.Contains(err.Error(), "issued: none") {
t.Errorf("history of %s to a module not issued it: %v", name, err)
}
}
if _, err := ask(owner, "history", map[string]any{"state": "issues", "key": "a b"}); err == nil ||
!strings.Contains(err.Error(), "not a key") {
t.Errorf("history of a key the bus cannot hold: %v", err)
}
// A page is the values in the order the bus wrote them after the cursor, in one request, to the owner and to a reader,
// never to a module not issued the state; a page too small for two values still gives one and says where the read goes
// on; a value written after the read began comes marked seen-again, and a fresh read hands each kept message, a key's
// older kept revision included (design 47 H2 §779, H-A-382).
for _, kv := range [][2]string{{"2", `"bb"`}, {"3", `"cc"`}} {
if _, err := ask(owner, "put", map[string]any{"state": "issues", "key": kv[0], "value": json.RawMessage(kv[1])}); err != nil {
t.Fatal(err)
}
}
type entry struct {
Key string `json:"key"`
Value json.RawMessage `json:"value"`
SeenAgain bool `json:"seen-again"`
Deleted bool `json:"deleted"`
}
type page struct {
Entries []entry `json:"entries"`
Next string `json:"next"`
}
read := func(who launch.Bus, state, cursor string, most int) page {
t.Helper()
raw, err := ask(who, "page", map[string]any{"state": state, "cursor": cursor, "max_bytes": most})
var p page
if err != nil || json.Unmarshal(raw, &p) != nil {
t.Fatalf("page of %s after %q = %s, %v", state, cursor, raw, err)
}
return p
}
said := func(p page) string {
var out []string
for _, e := range p.Entries {
out = append(out, e.Key+map[bool]string{true: "+again"}[e.SeenAgain]+map[bool]string{true: "+deleted"}[e.Deleted])
if e.Deleted == (e.Value != nil) {
out = append(out, "(value "+string(e.Value)+")")
}
}
return strings.Join(out, ",") + map[bool]string{true: " (more)"}[p.Next != ""]
}
for who, b := range map[string]launch.Bus{"issues": owner, "issues.issues": reader} {
if got := said(read(b, who, "", 1<<20)); got != "1,2,3" {
t.Errorf("a whole page of %s is %s, want 1,2,3", who, got)
}
}
first := read(owner, "issues", "", 1)
second := read(owner, "issues", first.Next, 1)
if said(first) != "1 (more)" || said(second) != "2 (more)" {
t.Fatalf("pages of one value are %s and %s, want 1 (more) and 2 (more)", said(first), said(second))
}
if _, err := ask(owner, "put", map[string]any{"state": "issues", "key": "1", "value": 11}); err != nil {
t.Fatal(err)
}
if got := said(read(owner, "issues", second.Next, 1<<20)); got != "3,1+again" {
t.Errorf("the rest of the read is %s, want 3 and 1 written again since the read began, marked: 3,1+again", got)
}
if got := said(read(owner, "issues", "", 1<<20)); got != "1,2,3,1" {
t.Errorf("a fresh read is %s, want each kept message once, 1's older revision first: 1,2,3,1", got)
}
// A kept delete is handed as an entry deleted, with no value, at its sequence, so a deleted key never comes back alive
// from a bucket with history (H-A-451 (1)).
for _, v := range []int{1, 2} {
if _, err := ask(owner, "put", map[string]any{"state": "issues", "key": "gone", "value": v}); err != nil {
t.Fatal(err)
}
}
if _, err := ask(owner, "delete", map[string]any{"state": "issues", "key": "gone"}); err != nil {
t.Fatal(err)
}
if got := said(read(owner, "issues", "", 1<<20)); got != "1,2,3,1,gone,gone,gone+deleted" {
t.Errorf("a read past a delete is %s, want its two values and then the delete: 1,2,3,1,gone,gone,gone+deleted", got)
}
if _, err := ask(owner, "page", map[string]any{"state": "issues", "cursor": "2"}); err == nil ||
!strings.Contains(err.Error(), "not a cursor") {
t.Errorf("a page after a key, not a cursor, answered %v; want it refused", err)
}
// A page stops when its time budget is spent, after one value at least, and says where it goes on.
step := time.Time{}
bus.PageClock = func() time.Time { step = step.Add(11 * time.Second); return step }
defer func() { bus.PageClock = time.Now }()
raw, err := ask(owner, "page", map[string]any{"state": "issues", "max_bytes": 1 << 20, "budget_ms": 10000})
var cut page
if err != nil || json.Unmarshal(raw, &cut) != nil || said(cut) != "1 (more)" {
t.Fatalf("a page past its budget = %s, %v", raw, err)
}
if _, err := ask(other, "page", map[string]any{"state": "issues.issues"}); err == nil {
t.Errorf("a page given to a module not issued the state")
}
}