The review found §794's two bounds fixed in code: the builder's 100 ms for saying a slow event's handling, and builder_health's five minutes for naming a consumer behind. Both become settings read at run time, slow-event (100ms) and events-behind-bound (5m), declared in the manifest's config with those defaults; a value that is no duration refuses the builder's start, and makes the health say it. Builds: design 47 H2 §794, H-A-461 (2)
152 lines
8.6 KiB
Go
152 lines
8.6 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds"
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
"git.novox.be/novox/mesh-sdk/go/health"
|
|
|
|
"mesh-delivery-builder/internal/build"
|
|
|
|
"mesh-delivery-builder/internal/versions"
|
|
)
|
|
|
|
// TestAStateIsReadInOnePage: design 47 H H-A-292, H-A-293 — builder_publish_queue reads each of its own states in one page and
|
|
// never asks a second: a state past the page answers that page with More and the state's size from its info; one the page holds
|
|
// answers whole without reading the info.
|
|
func TestAStateIsReadInOnePage(t *testing.T) {
|
|
for _, c := range []struct {
|
|
name string
|
|
next string
|
|
}{{"past one page", "k1"}, {"held by one page", ""}} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
var asked []string
|
|
infos := 0
|
|
page := func(after string, maxBytes int, budget time.Duration) (stdio.StatePage, error) {
|
|
asked = append(asked, after)
|
|
if after != "" {
|
|
return stdio.StatePage{Entries: []stdio.StateEntry{{Key: "k2", Value: json.RawMessage(`2`)}}}, nil
|
|
}
|
|
return stdio.StatePage{Entries: []stdio.StateEntry{{Key: "k1", Value: json.RawMessage(`1`)}}, Next: c.next}, nil
|
|
}
|
|
info := func() (*stdio.StateInfo, error) { infos++; return &stdio.StateInfo{Keys: 2, Bytes: 640}, nil }
|
|
pg, err := onePage(page, info)
|
|
more := c.next != ""
|
|
if err != nil || len(asked) != 1 || len(pg.Values) != 1 || pg.More != more || (more && (pg.Keys != 2 || pg.Bytes != 640 || infos != 1)) || (!more && infos != 0) {
|
|
t.Errorf("asked the pages after %q, read %v, more %v, size %d keys %d bytes, info read %d times (%v): want one page only, its rest said with the state's size", asked, pg.Values, pg.More, pg.Keys, pg.Bytes, infos, err)
|
|
}
|
|
})
|
|
}
|
|
unsupported := func(string, int, time.Duration) (stdio.StatePage, error) {
|
|
return stdio.StatePage{}, errors.ErrUnsupported
|
|
}
|
|
if _, err := onePage(unsupported, nil); !errors.Is(err, errors.ErrUnsupported) {
|
|
t.Errorf("a runtime with no pages answered %v, want ErrUnsupported so the reader reads key by key", err)
|
|
}
|
|
// Design 47 H2 §794, H-A-435: builder_health reads the state consumers; a consumer whose oldest unacknowledged message waits
|
|
// six minutes is named, one within five minutes is not.
|
|
t.Run("builder_health names a consumer behind by five minutes", func(t *testing.T) {
|
|
now := time.Unix(10000, 0)
|
|
facts := map[string]json.RawMessage{
|
|
"builder-events": json.RawMessage(`{"stream":"EVENTS","name":"builder-events","pending":40,"oldest-unacknowledged":"` + now.Add(-6*time.Minute).UTC().Format(time.RFC3339) + `"}`),
|
|
"builder-kept": json.RawMessage(`{"stream":"K","name":"builder-kept","oldest-unacknowledged":"` + now.Add(-time.Minute).UTC().Format(time.RFC3339) + `"}`)}
|
|
r := versions.Reader{Keys: func() ([]string, error) { return []string{"builder-kept", "builder-events"}, nil },
|
|
Get: func(k string) (json.RawMessage, bool, error) { v, ok := facts[k]; return v, ok, nil }}
|
|
a := versions.BuilderHealth(r, now, defaultEventsBehindBound)
|
|
if a.Healthy || a.Why != "The builder's consumers behind by 5m0s or more: builder-events (its oldest unacknowledged message waits 6m0s, 40 more not yet given)." {
|
|
t.Errorf("health = %+v, want unhealthy naming builder-events alone", a)
|
|
}
|
|
if a := versions.BuilderHealth(r, now.Add(-2*time.Minute), defaultEventsBehindBound); !a.Healthy {
|
|
t.Errorf("health four minutes in = %+v, want healthy", a)
|
|
}
|
|
// H-A-461 (2): the bound is the setting events-behind-bound, five minutes when none is set.
|
|
tool := healthTool(r, func() time.Time { return now })
|
|
for _, c := range []struct{ set, want string }{{"", "unhealthy"}, {"10m", "healthy"}, {"soon", "events-behind-bound"}} {
|
|
t.Setenv(stdio.SettingWord(eventsBehindBoundSetting), c.set)
|
|
got, _ := tool.Run(nil)
|
|
a := got.(health.Answer)
|
|
if (c.want == "healthy") != a.Healthy || (c.want == "events-behind-bound" && !strings.Contains(a.Why, c.want)) {
|
|
t.Errorf("with events-behind-bound %q the health = %+v, want %s", c.set, a, c.want)
|
|
}
|
|
}
|
|
})
|
|
// Design 47 H2 §793, H-A-454: builder_publish_queue asks the seat mesh-builds' verb publish-queue, which the builder serves from
|
|
// its own stream, and answers its counts, list and cap with the states beside them; builder_publish_drop and
|
|
// builder_publish_first record the hand act first, then write their reasks value.
|
|
t.Run("the queue is the builder's verb publish-queue", func(t *testing.T) {
|
|
var asked []string
|
|
ask := func(key string, body any) (json.RawMessage, error) {
|
|
in := body.(meshbuilds.PublishQueueInput)
|
|
asked = append(asked, key+" "+in.Module)
|
|
return json.Marshal(meshbuilds.PublishQueueAnswer{Total: 9000, Listed: 1, Capped: true, ItemsRead: 4000, QueueItems: 9000, QueueBytes: 4096,
|
|
Counts: []meshbuilds.PublishQueueAnswerCountsItem{{Holder: "oci", Class: versions.PublishClassStart, Count: 4000}},
|
|
Queue: []meshbuilds.PublishQueueAnswerQueueItem{{Holder: "oci", Module: "m", Version: "0.1.0", Class: versions.PublishClassStart}}})
|
|
}
|
|
none := versions.Reader{Keys: func() ([]string, error) { return nil, nil }}
|
|
tools := republishTools(publishQueueVia(ask), none, none, nil, time.Now, meshhandactsclient.Recorder{})
|
|
raw, err := tools[1].Run(map[string]any{"module": "m"})
|
|
a, _ := raw.(versions.Queued)
|
|
if err != nil || fmt.Sprint(asked) != "[seat:mesh-builds.publish-queue m]" || a.Total != 9000 || len(a.Queue) != 1 || a.Queue[0].Module != "m" ||
|
|
len(a.Counts) != 1 || !a.Capped || len(a.Read) != 1 || a.Read[0].KeysRead != 4000 || a.Read[0].StateKeys != 9000 {
|
|
t.Errorf("%s asked %v, answered %+v (%v): want the verb asked with the module, its answer carried with its cap", tools[1].Name, asked, a, err)
|
|
}
|
|
})
|
|
// Design 47 H2 §801, H-A-446 (1): build_current names the holders from the seat-holders view and asks each holder's current in
|
|
// turn at its node, answering them together; a holder that does not answer is listed with why.
|
|
t.Run("build_current asks every build seat's holder in turn", func(t *testing.T) {
|
|
view := map[string]json.RawMessage{
|
|
"node-build-go.g14": json.RawMessage(`{"holders":[{"module":"mesh-build-go"}]}`),
|
|
"node-build-go.ace": json.RawMessage(`{"holders":[{"module":"mesh-build-go"}]}`),
|
|
"node-build-node.ace": json.RawMessage(`{"holders":[{"module":"mesh-build-node"}]}`),
|
|
"mesh-builds": json.RawMessage(`{"holders":[{"module":"mesh-delivery-builder"}]}`)}
|
|
var asked []string
|
|
ask := func(key string, body any) (json.RawMessage, error) {
|
|
asked = append(asked, key)
|
|
if strings.HasSuffix(key, "@g14") {
|
|
return nil, errors.New("no answer")
|
|
}
|
|
return json.RawMessage(`{"takes":true,"asks":[{"id":"a1","asker":"mesh-delivery-builder"}]}`), nil
|
|
}
|
|
tool := buildCurrentTool(func() []string { return []string{"node-build-go", "node-build-node"} }, func() (map[string]json.RawMessage, error) { return view, nil }, ask)
|
|
raw, err := tool.Run(map[string]any{})
|
|
a, _ := raw.(map[string]any)
|
|
hs, _ := a["holders"].([]heldBuildSeat)
|
|
if err != nil || fmt.Sprint(asked) != "[seat:node-build-go.current@ace seat:node-build-go.current@g14 seat:node-build-node.current@ace]" ||
|
|
len(hs) != 3 || hs[1].Error != "no answer" || hs[0].Module != "mesh-build-go" || !strings.Contains(string(hs[2].Current), `"a1"`) {
|
|
t.Errorf("asked %v, answered %+v (%v): want each holder asked in turn at its node, answered together, the silent one with why", asked, hs, err)
|
|
}
|
|
})
|
|
t.Run("drop and first record the hand act, then ask the builder", func(t *testing.T) {
|
|
var order []string
|
|
ask := func(key string, value []byte) (json.RawMessage, error) {
|
|
order = append(order, "ask "+key)
|
|
r, _ := build.DecodeReAsk(value)
|
|
r.Answer = "done"
|
|
return json.Marshal(r)
|
|
}
|
|
acts := meshhandactsclient.Recorder{Ask: func(key string, body any) (json.RawMessage, error) {
|
|
order = append(order, "record")
|
|
return json.RawMessage(`{"id":"h1"}`), nil
|
|
}}
|
|
tools := publishActTools(ask, func() time.Time { return time.Unix(5, 0) }, acts)
|
|
for _, tool := range tools {
|
|
if _, err := tool.Run(map[string]any{"module": "m", "version": "0.1.2", "why": "a test"}); err != nil {
|
|
t.Fatalf("%s: %v", tool.Name, err)
|
|
}
|
|
}
|
|
if len(tools) != 2 || fmt.Sprint(order) != "[record ask publish-drop.5000000000 record ask publish-first.5000000000]" {
|
|
t.Errorf("tools %d, order %v: want each hand act recorded before its reasks value", len(tools), order)
|
|
}
|
|
if _, err := tools[0].Run(map[string]any{"module": "m", "why": "no version"}); err == nil {
|
|
t.Error("a drop naming no version is not refused")
|
|
}
|
|
})
|
|
}
|