| 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 |
| }) |