Files
mesh-delivery/modules/mesh-delivery-builder/cmd/mesh-delivery-builder/step_integration_test.go
T
jschoubben 0f65240139 builder: bind the seat's worker again after a deletion
The builder bound the seat mesh-builds' worker once, under a sync.Once, and stopped hearing the bus's advisories after
that, so a worker deleted and made again by the grants part was never pulled again and the asks piled up. The
advisories CREATED and DELETED are now heard for as long as the builder runs: a deletion lets the worker go and says
so, and a creation binds it again. The step test deletes the worker, makes it again and has an ask ended through it.

Builds: design 47 H2 H-A-468 (2)
2026-11-22 11:38:43 +01:00

142 lines
5.6 KiB
Go

//go:build integration
package main
import (
"encoding/json"
"runtime"
"strings"
"testing"
"time"
"github.com/nats-io/nats.go"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds"
"git.novox.be/novox/mesh-sdk/go/buscompose"
)
// The runner's build step ask on the seat mesh-builds' accept subject, naming an entry by its fingerprint, is built through the
// module's one claim and answered as the seat's step-ended.<node> under the ask's id (design 47 G A-97, A-99).
func TestTheRunnersBuildStepAskIsAnsweredAsStepEnded(t *testing.T) {
b := startBed(t)
b.module(t, "netcheck", "catalogue-a67f0196-netcheck.module.json", "modules/netcheck")
sub, err := nats.Connect(b.srvURL, nats.UserInfo("ctl", "pw"))
if err != nil {
t.Fatal(err)
}
defer sub.Close()
ended := make(chan *nats.Msg, 4)
s, err := sub.ChanSubscribe(meshbuilds.SubjectStepEndedAll, ended)
if err != nil {
t.Fatal(err)
}
defer s.Unsubscribe()
taken := make(chan *nats.Msg, 4)
st, err := sub.ChanSubscribe(meshbuilds.SubjectAskTakenAll, taken)
if err != nil {
t.Fatal(err)
}
defer st.Unsubscribe()
// Design 47 H2 §777: an ask put on the seat's work queue while the builder is stopped waits there, and is taken (ask-taken)
// and ended once the builder starts.
run := meshbuilds.StepRun{Pipeline: "main.1.netcheck", Step: meshbuilds.StepRunStepBuild, Branch: "main", Revision: 1,
Entries: []meshbuilds.BuildEntry{{Module: "netcheck", Fingerprint: fp}}}
data, _ := json.Marshal(run)
msg := nats.NewMsg(meshbuilds.AcceptSubjectStep)
msg.Data = data
msg.Header.Set("x-event-id", "ask-1")
msg.Header.Set(nats.MsgIdHdr, "ask-1")
if _, err := b.js.PublishMsg(b.ctx, msg); err != nil {
t.Fatalf("the ask is not put on the seat's work queue: %v", err)
}
b.start(t, "n1", nil)
select {
case m := <-taken:
var at meshbuilds.AskTakenEvent
if err := json.Unmarshal(m.Data, &at); err != nil || at.Ask != "ask-1" || at.Node != "n1" || at.At.IsZero() || m.Subject != meshbuilds.SubjectAskTaken("ask-1") {
t.Fatalf("ask-taken on %s = %+v, %v; want ask-1 taken on n1", m.Subject, at, err)
}
case <-time.After(20 * time.Second):
t.Fatal("no ask-taken within 20 s of the start")
}
select {
case m := <-ended:
var end meshbuilds.StepEnded
if err := json.Unmarshal(m.Data, &end); err != nil || end.ID != "ask-1" || end.Outcome != meshbuilds.StepEndedOutcomePassed || end.Pipeline != "main.1.netcheck" || m.Subject != meshbuilds.SubjectStepEnded("n1") {
t.Fatalf("step-ended on %s = %+v, %v; want the ask ask-1 passed on n1", m.Subject, end, err)
}
case <-time.After(20 * time.Second):
t.Fatal("no step-ended within 20 s")
}
// H-A-155: an ask naming a source revision the builder's picture of main does not hold yet waits for it, said in the
// journal, and is answered once a revision taken in from it arrives.
run.Pipeline, run.Revision, run.Source = "main.2.netcheck", 2, 2
data, _ = json.Marshal(run)
msg = nats.NewMsg(meshbuilds.AcceptSubjectStep)
msg.Data = data
msg.Header.Set("x-event-id", "ask-2")
if err := sub.PublishMsg(msg); err != nil {
t.Fatal(err)
}
b.awaitSaid(t, "main.2.netcheck: revision 2 of main not in the builder's picture yet")
b.catalog.mu.Lock()
b.catalog.source = 2
b.catalog.mu.Unlock()
// Revision 2, taken in from source 2, moves another module: netcheck's entry is the one the picture holds already.
b.moduleFP(t, "ca-trust", "catalogue-a67f0196-ca-trust.module.json", "modules/ca-trust", nil, commit, "src2:"+strings.Repeat("4", 64), 0)
select {
case m := <-ended:
var end meshbuilds.StepEnded
if err := json.Unmarshal(m.Data, &end); err != nil || end.ID != "ask-2" || end.Outcome != meshbuilds.StepEndedOutcomePassed {
t.Fatalf("step-ended = %+v, %v; want the ask ask-2 passed once the picture held source revision 2", end, err)
}
case <-time.After(20 * time.Second):
t.Fatal("no step-ended for ask-2 within 20 s")
}
// Each ask is acknowledged once its end was said: the seat's worker holds none pending.
stream, worker := buscompose.SeatQueueStreamName(meshbuilds.Seat), buscompose.SeatQueueWorkerName(meshbuilds.Seat)
deadline := time.Now().Add(10 * time.Second)
for {
c, err := b.js.Consumer(b.ctx, stream, worker)
if err != nil {
t.Fatal(err)
}
info, err := c.Info(b.ctx)
if err != nil {
t.Fatal(err)
}
if info.NumPending == 0 && info.NumAckPending == 0 {
break
}
if time.Now().After(deadline) {
t.Fatalf("the seat's worker holds %d waiting and %d taken after both asks ended, want none", info.NumPending, info.NumAckPending)
}
runtime.Gosched()
}
// H-A-468 (2): the worker deleted and made again by the broker's part is bound again, and an ask put after it is ended.
if err := b.js.DeleteConsumer(b.ctx, stream, worker); err != nil {
t.Fatal(err)
}
b.awaitSaid(t, "work queue "+worker+" on "+stream+" was deleted")
b.seatQueue(t)
b.awaitSaid(t, "pulling the seat "+meshbuilds.Seat+"'s asks from "+worker)
run.Pipeline = "main.3.netcheck"
data, _ = json.Marshal(run)
msg = nats.NewMsg(meshbuilds.AcceptSubjectStep)
msg.Data = data
msg.Header.Set("x-event-id", "ask-3")
msg.Header.Set(nats.MsgIdHdr, "ask-3")
if _, err := b.js.PublishMsg(b.ctx, msg); err != nil {
t.Fatal(err)
}
select {
case m := <-ended:
var end meshbuilds.StepEnded
if err := json.Unmarshal(m.Data, &end); err != nil || end.ID != "ask-3" || end.Outcome != meshbuilds.StepEndedOutcomePassed {
t.Fatalf("step-ended = %+v, %v; want the ask ask-3 passed through the worker made again", end, err)
}
case <-time.After(20 * time.Second):
t.Fatal("no step-ended for ask-3 within 20 s of the worker made again: the builder did not bind it again")
}
}