Review of 083: finishing an enrolment whose token was spent takes proof of the key's private half, a live lease and a first delivery — a public key alone cannot replay a spent token; shutdown leaves held messages for the broker; identical builds supersede; what is held leaves room in the prefetch

This commit is contained in:
2026-09-22 14:33:13 +02:00
parent a3b7e830c8
commit 4567fa666c
7 changed files with 214 additions and 25 deletions
+47
View File
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"testing"
@@ -118,3 +119,49 @@ func TestACatchUpWaitsOutARestartingStore(t *testing.T) {
}
}
}
// Shutting down is not an answer about a message: one handled with a cancelled context is left
// unsettled, for the broker to hand to whatever consumes next (issue 083, review).
func TestAMessageHandledDuringShutdownIsLeftForTheBroker(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
s := quietServer()
s.recorder = recordsWith{err: context.Canceled}
to := &settledAs{}
s.handleBuilt(ctx, a(t, to, KeyBuilt, BuildResult{On: "anchor", Repository: "/r", Commit: "abc"}))
if !to.held() {
t.Fatalf("a build result handled during shutdown was settled, and so lost: %+v", *to)
}
}
// Two identical build results: the newer sets the older aside rather than leaving it unsettled
// for ever, holding a place in the prefetch.
func TestAnIdenticalBuildResultSetsTheHeldOneAside(t *testing.T) {
s := quietServer()
s.recorder = recordsWith{err: restarting}
built := BuildResult{On: "anchor", Repository: "/r", Commit: "abc"}
first, second := &settledAs{}, &settledAs{}
s.handleBuilt(context.Background(), a(t, first, KeyBuilt, built))
s.handleBuilt(context.Background(), a(t, second, KeyBuilt, built))
if !first.acked || !second.held() || len(s.parked) != 1 {
t.Fatalf("an identical build result did not set the held one aside: first %+v second %+v, %d held",
*first, *second, len(s.parked))
}
}
// What is held stops short of the prefetch, so the loop always has room to answer an enrolment.
func TestWhatIsHeldLeavesRoomInThePrefetch(t *testing.T) {
s := quietServer()
s.recorder = recordsWith{err: restarting}
var last *settledAs
for i := 0; i < Prefetch; i++ {
last = &settledAs{}
s.handleBuilt(context.Background(), a(t, last, KeyBuilt, BuildResult{On: "anchor", Commit: fmt.Sprint(i)}))
}
if len(s.parked) != Prefetch-PrefetchHeadroom {
t.Fatalf("%d messages were held; the ceiling is %d", len(s.parked), Prefetch-PrefetchHeadroom)
}
if last.held() {
t.Fatalf("a message past the ceiling was held: %+v", *last)
}
}