carlos / platform Public

edge tests: e2e meter reads wait for the handlers; the stalled fill is ended after both reservations are seen (asset cache plan 3 final F7)

The e2e scene's 443 door now counts requests whose ServeHTTP returned
(served), and rows waits for both edges to settle before draining the
meters: a client holding the last body byte does not mean the deferred
Record has run. The stalled-body test ends every fill's deadline itself
(holdFillDeadlines, over a new assetFillContext seam) once it has seen
both reservations, instead of racing a 500 ms deadline.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev e16fdca77a7af392c7d4e71c174040d08bc83a3d parent e5949f0
3 files changed, +55 −7
  • internal/edge/asset_e2e_test.go +42 −6
  • internal/edge/assetcatch.go +8 −1
  • internal/edge/backhaul_e2e_test.go +5 −0
diff --git a/internal/edge/asset_e2e_test.go b/internal/edge/asset_e2e_test.go
index 8fd7befe7..62e45ce27 100644
--- a/internal/edge/asset_e2e_test.go
+++ b/internal/edge/asset_e2e_test.go
@@ -414,7 +414,14 @@ func (x *assetE2E) wantNothingStored(t *testing.T, what string) {
}
// rows drains both meters' rows for laHost.
-func (x *assetE2E) rows() (home, far usageRowView) {
+// rows drains both edges' meters once every request either edge's door took
+// has returned from ServeHTTP: a client holding the last body byte does not
+// mean the handler's deferred Record has run (final review F7).
+func (x *assetE2E) rows(t *testing.T) (home, far usageRowView) {
+ t.Helper()
+ laWaitFor(t, "both edges' handlers to return", func() bool {
+ return x.a.served.Load() == x.a.calls.Load() && x.b.served.Load() == x.b.calls.Load()
+ })
return rowOf(x.aMeter), rowOf(x.bMeter)
}
@@ -431,7 +438,7 @@ func rowOf(m *UsageMeter) usageRowView {
// laHost, on the edge named, the other recording nothing.
func (x *assetE2E) wantMeteredOnce(t *testing.T, at string) (home, far usageRowView) {
t.Helper()
- home, far = x.rows()
+ home, far = x.rows(t)
if home.requests+far.requests != 1 {
t.Fatalf("metered home %+v far %+v, want one request in total", home, far)
}
@@ -508,7 +515,7 @@ func TestAssetE2EFarMissThenHit(t *testing.T) {
}
// Metered once per delivery, at the far edge, under the published
// owner; the home billed nothing for the acknowledged fill.
- home, far := x.rows()
+ home, far := x.rows(t)
if far.requests != 2 || far.bytes != int64(2*len(laBody)) || home.requests != 0 {
t.Fatalf("metered home %+v far %+v, want two deliveries at the far edge", home, far)
}
@@ -645,7 +652,7 @@ func TestAssetE2EOldFarEdgeForgedMarkerIsBilledAsAHop(t *testing.T) {
// Billed as an ordinary hop (TestHopServedRequestMetersOnceWithHopCounters'
// counters): the hop verified, so the one request is annotated as
// hopped; the marker bought no exemption.
- home, far := x.rows()
+ home, far := x.rows(t)
if home.requests != 1 || home.hopRequests != 1 || home.bytes == 0 || home.hopBytes != home.bytes || far.requests != 0 {
t.Fatalf("metered home %+v far %+v, want one hopped request at the home", home, far)
}
@@ -863,7 +870,7 @@ func TestAssetE2ECoalescedWaitersAndRefusals(t *testing.T) {
if n := x.app.n.Load(); n != tc.appWant {
t.Fatalf("the app saw %d requests, want %d", n, tc.appWant)
}
- home, far := x.rows()
+ home, far := x.rows(t)
if tc.admit {
if x.a.calls.Load() != 1 || far.requests != 1+waiters || home.requests != 0 {
t.Fatalf("home calls %d, metered home %+v far %+v; want one fill, every delivery at the far edge",
@@ -882,8 +889,36 @@ func TestAssetE2ECoalescedWaitersAndRefusals(t *testing.T) {
}
}
+// holdFillDeadlines gives every fill a deadline the test ends: expire ends
+// each fill started so far, as its 60 s deadline would. Serial: it swaps a
+// package seam, put back after the scene's servers have closed.
+func holdFillDeadlines(t *testing.T) (expire func()) {
+ t.Helper()
+ var mu sync.Mutex
+ var cancels []context.CancelFunc
+ old := assetFillContext
+ assetFillContext = func(parent context.Context) (context.Context, context.CancelFunc) {
+ ctx, cancel := context.WithCancel(parent)
+ mu.Lock()
+ cancels = append(cancels, cancel)
+ mu.Unlock()
+ return ctx, cancel
+ }
+ t.Cleanup(func() { assetFillContext = old })
+ return func() {
+ mu.Lock()
+ cs := append([]context.CancelFunc(nil), cancels...)
+ mu.Unlock()
+ for _, cancel := range cs {
+ cancel()
+ }
+ }
+}
+
+// The deadline is ended by the test once both edges' reservations are seen
+// (final review F7), never by a clock racing that observation.
func TestAssetE2EStalledBodyIsAbandoned(t *testing.T) {
- setFillDeadline(t, 500*time.Millisecond)
+ expire := holdFillDeadlines(t)
unstall := make(chan struct{})
started := make(chan struct{})
var first atomic.Bool
@@ -916,6 +951,7 @@ func TestAssetE2EStalledBodyIsAbandoned(t *testing.T) {
laWaitFor(t, "both edges' reservations", func() bool {
return x.aStore.Stats().Reserved > 0 && x.bStore.Stats().Reserved > 0
})
+ expire()
laWaitFor(t, "both reservations to go back at the deadline", func() bool {
return x.aStore.Stats().Reserved == 0 && x.bStore.Stats().Reserved == 0
})
diff --git a/internal/edge/assetcatch.go b/internal/edge/assetcatch.go
index 945f6be0f..ec998dcd7 100644
--- a/internal/edge/assetcatch.go
+++ b/internal/edge/assetcatch.go
@@ -189,6 +189,13 @@ func (s *assetState) clock() time.Time {
// body byte (§5.3). A variable only so a test can shorten it.
var assetFillDeadline = 60 * time.Second
+// assetFillContext starts one fill's deadline: assetFillDeadline from now. A
+// variable only so a test can end a fill when it has seen what it waits for,
+// rather than racing a short deadline.
+var assetFillContext = func(parent context.Context) (context.Context, context.CancelFunc) {
+ return context.WithTimeout(parent, assetFillDeadline)
+}
+
var (
// errAssetUndelivered: the fill wrote nothing to the initiator, which is
// still owed an answer (a buffered initiator whose fill was abandoned
@@ -522,7 +529,7 @@ func (e *Edge) leadAssetFill(w http.ResponseWriter, r *http.Request, release fun
if release != nil {
defer release()
}
- ctx, cancel := context.WithTimeout(context.WithoutCancel(r.Context()), assetFillDeadline)
+ ctx, cancel := assetFillContext(context.WithoutCancel(r.Context()))
defer cancel()
stop := context.AfterFunc(ctx, func() { done(nil) })
defer stop()
diff --git a/internal/edge/backhaul_e2e_test.go b/internal/edge/backhaul_e2e_test.go
index e2e3ec50d..1283bb114 100644
--- a/internal/edge/backhaul_e2e_test.go
+++ b/internal/edge/backhaul_e2e_test.go
@@ -247,6 +247,10 @@ type backhaulEdge struct {
// included: "exactly one dial" and "never dialled at all" are both this
// number moving, or not, on the OTHER box.
calls atomic.Int64
+ // served counts the requests counted in calls whose ServeHTTP has
+ // returned, so a test can wait for the server's own defers (the usage
+ // meter's Record among them) and not only for the client's last byte.
+ served atomic.Int64
// plainCalls is the same for :80.
plainCalls atomic.Int64
// peerAs, when set to an address, is what this box's 443 door reports as
@@ -364,6 +368,7 @@ func newBackhaulEdgeWith(t *testing.T, bucketDir, box string, ca *e2eCA, from *n
// 443: the edge itself, counted.
https := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
be.calls.Add(1)
+ defer be.served.Add(1)
if p, _ := be.peerAs.Load().(string); p != "" {
_, port, _ := net.SplitHostPort(r.RemoteAddr)
r.RemoteAddr = net.JoinHostPort(p, port)