Merge pull request 'Forge bridge whole test: read performed once the bridge acknowledged the act (design 47 H2 §807, §812)' (#350) from fix/forge-bridge-owed-once into main

This commit was merged in pull request #350.
This commit is contained in:
2026-11-23 09:09:54 +00:00
@@ -10,6 +10,7 @@ import (
"io"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"testing"
@@ -495,6 +496,49 @@ func refusedRead(t *testing.T, b *bed, bucket string) {
}
}
// ackedOf listens, from now on, to the bridge's acknowledgements on its durable of the stream owed; the function it
// answers waits until the bridge acknowledged the act data. The bridge acknowledges an act only once heard returned:
// its outcome said, kept in performed, and the prune that write started ended (bound.go), so what the act changed is
// read whole after it, and the bridge stopped after it, never while the act is still being written.
func ackedOf(t *testing.T, b *bed) func(data []byte) {
t.Helper()
sub, err := b.ctl.SubscribeSync("$JS.ACK." + seat.StreamOwed + ".n1_mesh-delivery-forge-bridge.>")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = sub.Unsubscribe() })
if err := b.ctl.Flush(); err != nil {
t.Fatal(err)
}
owed, err := b.js.Stream(b.ctx, seat.StreamOwed)
if err != nil {
t.Fatal(err)
}
seen := map[string]bool{} // the acts acknowledged while another was waited for
return func(data []byte) {
t.Helper()
bound := time.Now().Add(20 * time.Second)
for !seen[string(data)] {
m, err := sub.NextMsg(time.Until(bound))
if err != nil {
t.Fatalf("the bridge did not acknowledge %s on %s within 20 s: %v", data, seat.StreamOwed, err)
}
// $JS.ACK.<stream>.<consumer>.<delivered>.<stream sequence>.<consumer sequence>.<time>.<pending>
parts := strings.Split(m.Subject, ".")
if string(m.Data) != "+ACK" || len(parts) < 6 {
continue
}
seq, err := strconv.ParseUint(parts[5], 10, 64)
if err != nil {
continue
}
if heard, err := owed.GetMsg(b.ctx, seq); err == nil {
seen[string(heard.Data)] = true
}
}
}
}
// Acceptance 49: the forge bridge sets the group on each member's merged message and says the group's end once, after the
// last member's, from the seat mesh-delivery's verb groups; its account reads no state of the gate's.
func TestBridgePerformsWhatIsOwedOnce(t *testing.T) {
@@ -658,8 +702,13 @@ func TestBridgePerformsWhatIsOwedOnce(t *testing.T) {
"number": 178, "head": strings.Repeat("a", 40), "line": "built while the forge was away"}
comment := map[string]any{"id": "comment.novox/mesh-catalog#9.aaaaaaaaaaaa.away", "act": "comment", "repository": catalog,
"number": 9, "head": strings.Repeat("a", 40), "text": "closed meanwhile"}
// Both acts are acknowledged before the bridge stops below: one stopped between its outcome and its keeping is heard
// again after the start, by design, and would write performed while it is filled.
acknowledgedAway := ackedOf(t, b)
var awayActs [][]byte
for _, act := range []map[string]any{note, comment} {
data, _ := json.Marshal(act)
awayActs = append(awayActs, data)
if _, err := b.js.Publish(b.ctx, seat.SubjectOwed(keys.EscapeName(act["repository"].(string)), fmt.Sprint(act["number"])), data); err != nil {
t.Fatal(err)
}
@@ -686,6 +735,9 @@ func TestBridgePerformsWhatIsOwedOnce(t *testing.T) {
if n := callsOf(b, "mesh-hand-acts.hand-act"); n != handActs {
t.Errorf("%d hand acts while the forge was away, want none", n-handActs)
}
for _, data := range awayActs {
acknowledgedAway(data)
}
// Design 47 H2 §812 (F2, the bridge's whole test): a write that takes performed past prune-mark (70 % of its cap)
// prunes it, oldest acts first, down to prune-target (50 %); a key kept under _keep/ survives the prune. performed is
@@ -737,31 +789,25 @@ func TestBridgePerformsWhatIsOwedOnce(t *testing.T) {
if f := fillOf(); f >= mark || f+400 < mark {
t.Fatalf("performed is filled to %d bytes, want just below its mark %d", f, mark)
}
pruned, err := kept.Watch(b.ctx, fillKey(1), jetstream.UpdatesOnly())
if err != nil {
t.Fatal(err)
}
defer pruned.Stop()
other["id"] = "status.novox/mesh-sdk#178.aaaaaaaaaaaa.mesh/prune"
pruning, _ := json.Marshal(other)
// The prune runs in the write that keeps the act's outcome, after the outcome is said: performed is read once the
// bridge acknowledged the act, when the prune ended, not at its first purge while the others are still on their way.
acknowledged := ackedOf(t, b)
if _, err := b.js.Publish(b.ctx, subject, pruning); err != nil {
t.Fatal(err)
}
if e := ended(); e.Outcome != forgemsg.OwedPerformed {
t.Errorf("the act that takes performed past its mark: %+v, want performed", e)
}
for gone, bound := false, time.After(20*time.Second); !gone; {
select {
case e := <-pruned.Updates():
gone = e != nil && e.Operation() == jetstream.KeyValuePurge
case <-bound:
t.Fatalf("performed past its mark (%d of %d bytes) was not pruned within 20 s", fillOf(), uint64(performedCap))
}
}
acknowledged(pruning)
held := func(key string) bool {
_, err := kept.Get(b.ctx, key)
return err == nil
}
if held(fillKey(1)) {
t.Fatalf("performed past its mark (%d of %d bytes) was not pruned: its oldest act not kept is still held", fillOf(), uint64(performedCap))
}
if !held(fillKey(0)) || !held("_keep/"+fillKey(0)) {
t.Errorf("the act kept under _keep/ was pruned")
}