Builder: test the reserve-done act, its reasks dispatch and its tool (A-455 review round 1)

Builds: design 47 G A-455, §369, §588
This commit is contained in:
2026-11-17 21:22:09 +01:00
parent bec4d43da6
commit 22f252251c
4 changed files with 153 additions and 12 deletions
@@ -2,6 +2,7 @@ package main
import (
"encoding/json"
"errors"
"strings"
"testing"
"time"
@@ -44,3 +45,56 @@ func TestWaitsToolsListTheWaitsAndReleaseAfterTheHandAct(t *testing.T) {
t.Fatalf("release: %v, %d recorded, asked %+v", err, recorded, asked)
}
}
// Design 47 G A-455: builder_reserve_done records the hand act first and only then asks the builder through reasks; the builder's
// answer (the version and its queued publish) is the tool's answer, its refusal the tool's error, and an act that was not
// recorded asks nothing.
func TestReserveDoneRecordsTheHandActFirstAndAnswersTheBuilders(t *testing.T) {
var order []string
var asked []build.ReAsk
reply := func(refuse bool) func(string, []byte) (json.RawMessage, error) {
return func(key string, value []byte) (json.RawMessage, error) {
order = append(order, "asked")
a, _ := build.DecodeReAsk(value)
asked = append(asked, a)
a.Answered, a.Answer, a.Refused = time.Now(), "reserved 0.1.13 for node-tools and queued 1 versions to publish: node-tools@0.1.13", refuse
if refuse {
a.Answer = "the build key is not done"
}
return json.Marshal(a)
}
}
record := func(fail bool) meshhandactsclient.Recorder {
return meshhandactsclient.Recorder{Ask: func(string, any) (json.RawMessage, error) {
order = append(order, "recorded")
if fail {
return nil, errors.New("no responders")
}
return json.RawMessage(`{}`), nil
}}
}
run := func(ask func(string, []byte) (json.RawMessage, error), acts meshhandactsclient.Recorder, args map[string]any) (any, error) {
for _, tl := range reservedTools(versions.Reader{}, ask, time.Now, acts) {
if tl.Name == versions.ReserveDoneTool {
return tl.Run(args)
}
}
t.Fatal("the bundle serves no builder_reserve_done")
return nil, nil
}
args := map[string]any{"module": "node-tools", "fp": "src2:7df8", "why": "its build ended done with no version"}
out, err := run(reply(false), record(false), args)
if err != nil || len(order) != 2 || order[0] != "recorded" || order[1] != "asked" || asked[0].Mode != build.ModeReserveDone || asked[0].Name != "node-tools" || asked[0].FP != "src2:7df8" {
t.Fatalf("answer %v, %v; order %v, asked %+v", out, err, order, asked)
}
if ans := out.(map[string]any)["answer"].(string); !strings.Contains(ans, "reserved 0.1.13") || !strings.Contains(ans, "queued 1 versions") {
t.Errorf("the answer %q names no version and queued publish", ans)
}
if _, err := run(reply(true), record(false), args); err == nil || !strings.Contains(err.Error(), "not done") {
t.Errorf("the builder's refusal is not the tool's error: %v", err)
}
order = nil
if _, err := run(reply(false), record(true), args); err == nil || len(order) != 1 || order[0] != "recorded" {
t.Errorf("an unrecorded act was asked of the builder: %v, %v", err, order)
}
}
@@ -247,18 +247,7 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro
acts.Republish = republish.Republish
reserved.publishAtEnd = republish.Ended
// builder_reserve_done: a done trunk build that reserved none is given a version, and its publish queued as publish again does.
acts.ReserveDone = func(ctx context.Context, module, fp, by, why string, at time.Time) (string, error) {
res, err := b.ReserveDone(ctx, module, fp)
if err != nil {
return "", err
}
logger.Printf("%s at %s: version %s is reserved by hand (%s, %s) for the done build", module, fp, res.Version, by, why)
queued, err := republish.Republish(ctx, module, res.Version)
if err != nil {
return "", fmt.Errorf("%s is reserved for %s at %s, but its publish is not queued (builder_publish_again queues it): %w", res.Version, module, fp, err)
}
return fmt.Sprintf("reserved %s for %s at %s and queued %d versions to publish: %s; builder_publish_queue shows them", res.Version, module, fp, len(queued), strings.Join(queued, ", ")), nil
}
acts.ReserveDone = reserveDoneAct(b.ReserveDone, republish.Republish, logger.Printf)
again := &republishing{rp: republish}
// The operator is asked once a key's event builds are spent (design 47 G §568): the builder's asker on the seat operator-channel.
decided := &decisions{subject: oc.SubjectDecided(cred.Module)}
@@ -341,3 +341,22 @@ func (rv *reservations) noMark(ctx context.Context, o versions.Outcome) string {
}
return strings.Join(out, "; ")
}
// reserveDoneAct is builder_reserve_done's act (design 47 G A-455): the next version of the series is reserved for a done trunk build
// that reserved none, and its publish is queued as builder_publish_again queues it. Its answer says the version and the queued
// publishes; a reservation whose publish could not be queued is said so, with the tool that queues it.
func reserveDoneAct(reserve func(ctx context.Context, module, fp string) (versions.ReservedVersion, error),
republish func(ctx context.Context, module, version string) ([]string, error), logf func(string, ...any)) func(ctx context.Context, module, fp, by, why string, at time.Time) (string, error) {
return func(ctx context.Context, module, fp, by, why string, at time.Time) (string, error) {
res, err := reserve(ctx, module, fp)
if err != nil {
return "", err
}
logf("%s at %s: version %s is reserved by hand (%s, %s) for the done build", module, fp, res.Version, by, why)
queued, err := republish(ctx, module, res.Version)
if err != nil {
return "", fmt.Errorf("%s is reserved for %s at %s, but its publish is not queued (builder_publish_again queues it): %w", res.Version, module, fp, err)
}
return fmt.Sprintf("reserved %s for %s at %s and queued %d versions to publish: %s; builder_publish_queue shows them", res.Version, module, fp, len(queued), strings.Join(queued, ", ")), nil
}
}
@@ -17,6 +17,7 @@ import (
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"mesh-delivery-builder/internal/build"
"mesh-delivery-builder/internal/versions"
)
@@ -293,3 +294,81 @@ func TestABuildTakingOverAnOlderReservationDropsItsBoundAndSaysSo(t *testing.T)
t.Errorf("reserved for fp-new: %+v %v", got, ok)
}
}
// ansKV records the value versionActed writes back to a reasks key.
type ansKV struct {
jetstream.KeyValue
wrote chan []byte
}
func (a *ansKV) Update(_ context.Context, _ string, v []byte, _ uint64) (uint64, error) {
a.wrote <- v
return 2, nil
}
type ansEntry struct {
jetstream.KeyValueEntry
key string
value []byte
}
func (e ansEntry) Key() string { return e.key }
func (e ansEntry) Revision() uint64 { return 1 }
func (e ansEntry) Value() []byte { return e.value }
func (e ansEntry) Operation() jetstream.KeyValueOp { return jetstream.KeyValuePut }
// TestAReserveDoneIsAnsweredWithTheVersionAndItsQueuedPublish: design 47 G A-455 — the reasks value of builder_reserve_done reaches
// the act, which reserves for the done build and queues the publish as publish again does, and its answer names both; a
// refusal of the reserving queues nothing and is the answer; a publish that cannot be queued says the version is reserved.
func TestAReserveDoneIsAnsweredWithTheVersionAndItsQueuedPublish(t *testing.T) {
ctx := context.Background()
var queued []string
var logged []string
logf := func(f string, a ...any) { logged = append(logged, fmt.Sprintf(f, a...)) }
reserve := func(_ context.Context, module, fp string) (versions.ReservedVersion, error) {
if fp == "refused" {
return versions.ReservedVersion{}, fmt.Errorf("the build key of %s at %s is not done", module, fp)
}
return versions.ReservedVersion{Module: module, Version: "0.1.13", Fingerprint: fp}, nil
}
republish := func(_ context.Context, module, version string) ([]string, error) {
if module == "nope" {
return nil, fmt.Errorf("the queue is full")
}
queued = append(queued, module+"@"+version)
return []string{module + "@" + version}, nil
}
acts := versions.Acts{ReserveDone: reserveDoneAct(reserve, republish, logf)}
for _, c := range []struct {
module, fp, want string
refused bool
}{
{"node-tools", "src2:7df8", "reserved 0.1.13 for node-tools at src2:7df8 and queued 1 versions to publish: node-tools@0.1.13", false},
{"node-tools", "refused", "not done", true},
{"nope", "src2:1", "0.1.13 is reserved for nope at src2:1, but its publish is not queued", true},
} {
value, err := json.Marshal(build.ReAsk{V: 1, Why: "w", By: "me", Mode: build.ModeReserveDone, Name: c.module, FP: c.fp})
if err != nil {
t.Fatal(err)
}
kv := &ansKV{wrote: make(chan []byte, 1)}
// reAsked sends a reserve-done to the version acts (b is not asked), and the answer is written back to the key.
reAsked(ctx, nil, acts, kv, ansEntry{key: "reserve-done." + c.module, value: value}, logf)
var wrote []byte
select {
case wrote = <-kv.wrote:
case <-time.After(10 * time.Second):
t.Fatalf("%s at %s: no answer was written to the key", c.module, c.fp)
}
var got build.ReAsk
if err := json.Unmarshal(wrote, &got); err != nil || got.Refused != c.refused || !strings.Contains(got.Answer, c.want) || got.Answered.IsZero() {
t.Errorf("%s at %s: written %s (%v), want refused=%v containing %q", c.module, c.fp, wrote, err, c.refused, c.want)
}
}
if !slices.Equal(queued, []string{"node-tools@0.1.13"}) {
t.Errorf("queued %v, want only node-tools@0.1.13", queued)
}
if !slices.ContainsFunc(logged, func(l string) bool { return strings.Contains(l, "0.1.13 is reserved by hand") }) {
t.Errorf("the reserving by hand is not said in the log: %q", logged)
}
}