carlos / platform Public

edge: guard usage tests wait for the home to finish serving

The far-fill and hop tests read a home edge's recorder and meter once the
client had its response, but the client's last byte is no barrier for the
server's own defers. The far-fill home is now wrapped with a channel
signalled after ServeHTTP returns, and the hop test waits for both edges'
served counters to catch up with their calls before draining.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev b07c1373f4314feebe1f1624115b48286d2ded7e parent 1cd4f0a
1 file changed, +21 −1
  • internal/edge/guard_usage_test.go +21 −1

CI passed — run details

diff --git a/internal/edge/guard_usage_test.go b/internal/edge/guard_usage_test.go
index a5a37f7db..41e1d2301 100644
--- a/internal/edge/guard_usage_test.go
+++ b/internal/edge/guard_usage_test.go
@@ -6,6 +6,7 @@ import (
"net/http"
"net/http/httptest"
"testing"
+ "time"
"github.com/carlosframework/platform/internal/edgeguard"
"github.com/carlosframework/platform/internal/registry"
@@ -122,6 +123,11 @@ func farReachedHomeGuardUsage(t *testing.T) (far, home guardUsage) {
t.Helper()
t.Setenv("CARLOS_EDGE_PROTECT", "")
homeMeter, homeRec := NewUsageMeter(), &memRecorder{}
+ // homeDone is signalled once the home's ServeHTTP has returned, so its
+ // observer defer has recorded and counted the fill. The far edge's
+ // client can have the last byte before that, so the response alone is
+ // no barrier.
+ homeDone := make(chan struct{}, 1)
s := newFarEdge(t, func(key []byte, m *homeMap) http.Handler {
h := newLAScene(t, assetOK(laBody))
hm := newHomeMap(m.s, "betty")
@@ -129,12 +135,20 @@ func farReachedHomeGuardUsage(t *testing.T) (far, home guardUsage) {
h.e.homes.Store(hm)
h.e.EnableUsage(homeMeter)
h.e.EnableRequestRecords(homeRec)
- return h.e
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ defer func() { homeDone <- struct{}{} }()
+ h.e.ServeHTTP(w, r)
+ })
})
farRec := &memRecorder{}
s.e.EnableRequestRecords(farRec)
w := s.get(t, "/assets/app.php")
wantCache(t, w, "miss")
+ select {
+ case <-homeDone:
+ case <-time.After(5 * time.Second):
+ t.Fatal("timed out waiting for the home to finish serving the fill")
+ }
far = guardUsage{w: w, recorded: true, matches: farRec.match, flush: s.meter.Drain()}
home = guardUsage{recorded: true, matches: homeRec.match, flush: homeMeter.Drain()}
return far, home
@@ -289,6 +303,12 @@ func TestGuardUsageHopCountsOnce(t *testing.T) {
if resp.StatusCode != http.StatusOK {
t.Fatalf("status = %d, want the home app's 200", resp.StatusCode)
}
+ // The client's last byte is no barrier for the servers' own defers,
+ // where the meters count: wait until every request each edge took has
+ // returned from ServeHTTP.
+ laWaitFor(t, "both edges to finish serving", func() bool {
+ return s.a.served.Load() == s.a.calls.Load() && s.b.served.Load() == s.b.calls.Load()
+ })
var checked, caught int64
for _, f := range []usage.EdgeFlush{home.Drain(), sender.Drain()} {
for _, d := range f.Guard {