Merge pull request 'forge bridge: adopt only an added branch; its integration tests wait on what they assert' (#268) from fix/47-H1-forge-bridge-integration into main

This commit was merged in pull request #268.
This commit is contained in:
2026-11-20 14:46:50 +00:00
4 changed files with 150 additions and 20 deletions
@@ -162,6 +162,8 @@ type bed struct {
answer func(verb string, answer string)
// calls are the verbs the stand-in was called with, in order, as `<seat>.<verb> <input>`.
calls func() []string
// called is signalled after each call the stand-in answered, its answer chosen.
called <-chan struct{}
// restart stops the bridge, runs between, and starts it again on the same bus.
restart func(between func())
}
@@ -237,6 +239,7 @@ func startBed(t *testing.T, head string) *bed {
answers["git."+verb] = string(gitfixture.Given(t, verb+" answers", "verb:"+verb+".answer"))
}
var called []string
signal := make(chan struct{}, 1)
for _, subject := range []string{"mesh.seat.*.call.*.*", "mesh.seat.*.tool.*"} {
sub, err := ctl.Subscribe(subject, func(m *nats.Msg) {
parts := strings.Split(m.Subject, ".")
@@ -249,6 +252,10 @@ func startBed(t *testing.T, head string) *bed {
answer = answers[verb+" "+in.Repository]
}
mu.Unlock()
select {
case signal <- struct{}{}:
default:
}
if answer == "" {
answer = `{"error":"the stand-in holds no answer"}`
}
@@ -279,7 +286,7 @@ func startBed(t *testing.T, head string) *bed {
start()
t.Cleanup(func() { stop() })
set := func(verb, answer string) { mu.Lock(); answers[verb] = answer; mu.Unlock() }
return &bed{srv: srv, ctl: ctl, js: js, ctx: ctx, done: done, answer: set,
return &bed{srv: srv, ctl: ctl, js: js, ctx: ctx, done: done, answer: set, called: signal,
calls: func() []string { mu.Lock(); defer mu.Unlock(); return append([]string(nil), called...) },
restart: func(between func()) {
stop()
@@ -289,6 +296,30 @@ func startBed(t *testing.T, head string) *bed {
}}
}
// callsOf is how many calls of the stand-in start with prefix.
func callsOf(b *bed, prefix string) int {
n := 0
for _, c := range b.calls() {
if strings.HasPrefix(c, prefix) {
n++
}
}
return n
}
// calledTimes waits until the stand-in answered n calls starting with prefix: a call's answer is chosen when it is counted.
func calledTimes(t *testing.T, b *bed, prefix string, n int) {
t.Helper()
bound := time.After(20 * time.Second)
for callsOf(b, prefix) < n {
select {
case <-b.called:
case <-bound:
t.Fatalf("%d calls %s within 20 s, want %d", callsOf(b, prefix), prefix, n)
}
}
}
// headAt sets the stand-in seat git so main of the catalog is at head, protected or not.
func headAt(b *bed, head string, protected bool) {
p := "true"
@@ -363,7 +394,10 @@ func TestBridgeSaysOneHeadMovedPerMove(t *testing.T) {
b.restart(func() { headAt(b, h4, true) })
said("a restart after a move", forgemsg.HeadMoved{Head: h4, Before: h3, Start: true})
held(t, b, h4)
branchReads := callsOf(b, "git.branch ")
b.restart(func() {}) // at the head held: nothing
// The head moves only once the start has read the head held, so that read is not the move's (§739: start true).
calledTimes(t, b, "git.branch ", branchReads+1)
headAt(b, h5, true)
move(t, b, "main")
said("the next move, the first message after the restart at the head held", forgemsg.HeadMoved{Head: h5, Before: h4})
@@ -372,13 +406,11 @@ func TestBridgeSaysOneHeadMovedPerMove(t *testing.T) {
t.Errorf("the bridge asked %s; it asks the seat git nothing but branch", c)
}
}
heads, err := b.js.KeyValue(b.ctx, headsBucket(forgeBridgeModule()))
if err != nil {
t.Fatal(err)
}
if e, err := heads.Get(b.ctx, "branch.novox.mesh-catalog.main"); err != nil || string(e.Value()) != h5 {
t.Errorf("the state heads holds %v (%v), want %s", e, err, h5)
// One read per start and per move of main: the first view's registrations, said after a start, adopt no branch (§532).
if n := callsOf(b, "git.branch "); n != 3+4 {
t.Errorf("the bridge read the head %d times, want 7: once at each of 3 starts and once per move of main (4)", n)
}
held(t, b, h5) // kept after the message is written, so watched for, not read at once
}
func heard(t *testing.T, b *bed) <-chan *nats.Msg {
@@ -395,10 +427,16 @@ func heard(t *testing.T, b *bed) <-chan *nats.Msg {
return ch
}
// next is the next message heard on subject within a bound; the others are passed by.
// next is the next message heard on subject within 20 s; the others are passed by.
func next(t *testing.T, ch <-chan *nats.Msg, subject string) []byte {
t.Helper()
bound := time.After(20 * time.Second)
return nextWithin(t, ch, subject, 20*time.Second)
}
// nextWithin is next within bound.
func nextWithin(t *testing.T, ch <-chan *nats.Msg, subject string, within time.Duration) []byte {
t.Helper()
bound := time.After(within)
for {
select {
case m := <-ch:
@@ -406,7 +444,7 @@ func next(t *testing.T, ch <-chan *nats.Msg, subject string) []byte {
return m.Data
}
case <-bound:
t.Fatalf("nothing on %s within 20 s", subject)
t.Fatalf("nothing on %s within %s", subject, within)
return nil
}
}
@@ -455,6 +493,30 @@ func TestBridgePerformsWhatIsOwedOnce(t *testing.T) {
}
return n
}
// The bridge's acknowledgements on its durable of the stream owed, heard as the bus hears them.
acks := make(chan *nats.Msg, 16)
ackSub, err := b.ctl.ChanSubscribe("$JS.ACK."+seat.StreamOwed+".n1_mesh-delivery-forge-bridge.>", acks)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = ackSub.Unsubscribe() })
if err := b.ctl.Flush(); err != nil {
t.Fatal(err)
}
acked := func(n int) {
t.Helper()
bound := time.After(20 * time.Second)
for n > 0 {
select {
case m := <-acks:
if string(m.Data) == "+ACK" {
n--
}
case <-bound:
t.Fatalf("the bridge did not acknowledge what it heard on %s within 20 s", seat.StreamOwed)
}
}
}
gateOwed := gitfixture.Seat(t, seat.Seat, "events", "owed is said, a status", "event:owed")[0]
subject := seat.SubjectOwed("novox~mesh-sdk", "178")
for range 2 {
@@ -479,12 +541,26 @@ func TestBridgePerformsWhatIsOwedOnce(t *testing.T) {
_ = json.Unmarshal(gateOwed, &other)
other["id"] = "status.novox/mesh-sdk#178.aaaaaaaaaaaa.mesh/other"
otherOwed, _ := json.Marshal(other)
// The three acts heard are acknowledged before the stop: one stopped while its outcome is on its way is heard again
// after the start, by design, which is not what this part of the test is about.
acked(3)
// An act said while the bridge stops may be handed to the stopped bridge's pull before the bus has seen it go: the bus
// hands it again after its consumer's acknowledgement wait, its redelivery the only clock (H-A-48).
owedConsumer, err := b.js.Consumer(b.ctx, seat.StreamOwed, "n1_mesh-delivery-forge-bridge")
if err != nil {
t.Fatal(err)
}
ackWait := owedConsumer.CachedInfo().Config.AckWait
b.restart(func() {
if _, err := b.js.Publish(b.ctx, subject, otherOwed); err != nil {
t.Fatal(err)
}
})
if e := ended(); e.Of != seat.Seat || e.Outcome != forgemsg.OwedPerformed {
afterRestart, err := forgemsg.DecodeOwedEnded(nextWithin(t, ch, forgeSubject(forgemsg.OwedEndedEvent), ackWait+20*time.Second))
if err != nil {
t.Fatal(err)
}
if e := afterRestart; e.Of != seat.Seat || e.Outcome != forgemsg.OwedPerformed {
t.Errorf("the gate's status after the restart: %+v, want performed", e)
}
if n := count("git.status"); n != 2 {
@@ -115,24 +115,31 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro
var regs *meshcatalogclient.Registrations
turn := &sync.Mutex{}
mv := &mover{call: call, pub: js, heads: heads, watched: watched, turn: turn, logger: logger}
recompute := func() {
// begun says the start's read of the heads covers the watched branches from now on; it and what is watched are set
// under setting, so a branch added before begun is read by the start and one added after it is adopted.
var setting sync.Mutex
begun := false
recompute := func() (added bool) {
if regs != nil {
watched.Set(registeredOf(regs.All()), unescaped(src.Branches()))
added = watched.Set(registeredOf(regs.All()), unescaped(src.Branches()))
}
return added
}
started := make(chan struct{}) // closed once the watched branches are read
regs, _, err = meshcatalogclient.WireRegistrationsProcess(reader, keptCall, func(mcseat.Registration, bool) {
recompute()
select {
case <-started: // a branch registered while the bridge runs is adopted: its head read (§532)
setting.Lock()
adopt := recompute() && begun
setting.Unlock()
// Only a registration adding a watched branch while the bridge runs is adopted, its head read (§532); the first
// view's registrations, said after the start, add none, so the start reads their heads once, with start true (§739).
if adopt {
go mv.readHeads(ctx)
default:
}
})
if err != nil {
return err
}
src.Follower.Changed = func(string, json.RawMessage, bool, time.Time) { recompute() }
src.Follower.Changed = func(string, json.RawMessage, bool, time.Time) { setting.Lock(); recompute(); setting.Unlock() }
if _, err := reader.Adopt(src.Follower, sourcebranches.Seat, sourcebranches.EventSourceMoved, src.Hear); err != nil {
return err
}
@@ -167,7 +174,10 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro
return nil
}
}
setting.Lock()
recompute()
begun = true
setting.Unlock()
close(started)
// What the gate says it owes, on its seat's stream owed through this module's durable there (made from its consumes).
// A stream not there yet is said, and the bridge goes on: its other work does not wait on the gate.
@@ -27,6 +27,23 @@ func TestAMoveOfAWatchedBranchIsOneHeadMoved(t *testing.T) {
if !watchedOf("main").Watches("novox/other", "main") || watchedOf("main").Watches("novox/other", "beta") {
t.Errorf("before the first registration the gate's source's branches are watched, of every repository")
}
// §532: only a registration adding a watched branch is adopted; the same registrations read again (a first view's
// values said after the start) add none.
regs := []Registered{{Origin: "novox/mesh-sdk", Branches: []string{"main", "beta"}}}
for _, c := range []struct {
name string
regs []Registered
want bool
}{
{"the same registrations again", regs, false},
{"a branch fewer", []Registered{{Origin: "novox/mesh-sdk", Branches: []string{"main"}}}, false},
{"a branch registered", regs, true},
{"a repository registered", append(regs, Registered{Origin: "novox/other", Branches: []string{"main"}}), true},
} {
if got := w.Set(c.regs, []string{"main"}); got != c.want {
t.Errorf("%s: added %v, want %v", c.name, got, c.want)
}
}
const head, before = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", "cccccccccccccccccccccccccccccccccccccccc"
moved := Moved{Owner: "novox", Repo: "mesh-sdk", Branch: "feat/47-H"}
@@ -21,8 +21,10 @@ type Registered struct {
Branches []string
}
// Set takes what the bridge read: the registrations (none before the first) and the gate's source's branches by name.
func (w *Watched) Set(regs []Registered, source []string) {
// Set takes what the bridge read: the registrations (none before the first) and the gate's source's branches by name. It
// says whether a branch is watched now that was not (§532: a registration adding a watched branch is adopted); the same
// registrations read again add none.
func (w *Watched) Set(regs []Registered, source []string) (added bool) {
by := map[string][]string{}
for _, r := range regs {
for _, b := range r.Branches {
@@ -33,8 +35,33 @@ func (w *Watched) Set(regs []Registered, source []string) {
}
w.mu.Lock()
defer w.mu.Unlock()
before := w.pairsLocked()
w.byRepo, w.registered = by, len(regs) > 0
w.source = slices.Clone(source)
for p := range w.pairsLocked() {
if !before[p] {
added = true
}
}
return added
}
// pairsLocked is every watched branch as `<repository> <branch>`, the repository empty before the first registration
// (the gate's source's branch, of every repository). The caller holds mu.
func (w *Watched) pairsLocked() map[string]bool {
out := map[string]bool{}
if !w.registered {
for _, b := range w.source {
out[" "+b] = true
}
return out
}
for repo, branches := range w.byRepo {
for _, b := range branches {
out[repo+" "+b] = true
}
}
return out
}
// Watches says whether a move of branch in repository is one the bridge writes.