carlos / platform Public

edge tests: hand the hub's buffered bytes to the box server (fixes #471 test race)

The hub writes the h2 preface and SETTINGS straight after the 101. Three
tests that register a box by hand read the 101 through a bufio.Reader and
then served h2 on the raw conn, so when the preface landed in the same
read the box's server saw the SETTINGS ACK first ("bogus greeting") and
the hub answered 502. They now serve readerConn over what br over-read,
as tunnelclient.Dial does with bufferedConn. TestRegistrationRecordsCaps
no longer asserts br is empty after the 101: that only held by timing.
Failed 20/20 at -cpu=1 before (on main too), passes 20/20 after.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 6e7f78ef722edb93928da10716dfc55bd628f1eb parent f5d2955
3 files changed, +13 −7
  • internal/edge/tunnel_register_race_test.go +6 −2
  • internal/edge/tunnel_upgrade_test.go +3 −1
  • internal/edge/upgrades_test.go +4 −4

CI passed — run details

diff --git a/internal/edge/tunnel_register_race_test.go b/internal/edge/tunnel_register_race_test.go
index 771e4fbd6..08e4e03ff 100644
--- a/internal/edge/tunnel_register_race_test.go
+++ b/internal/edge/tunnel_register_race_test.go
@@ -9,6 +9,7 @@ package edge
import (
"bufio"
+ "io"
"net"
"net/http"
"net/url"
@@ -96,13 +97,16 @@ func TestRegistrationRacingARevocationIsNotPublished(t *testing.T) {
if err := req.Write(conn); err != nil {
t.Fatal(err)
}
- resp, err := http.ReadResponse(bufio.NewReader(conn), req)
+ br := bufio.NewReader(conn)
+ resp, err := http.ReadResponse(br, req)
if err != nil || resp.StatusCode != http.StatusSwitchingProtocols {
t.Fatalf("registration = %v, %v; want 101", resp, err)
}
served := make(chan struct{})
go func() {
- tunnelclient.ServeWith(conn, namedHandler("box-2"), tunnelclient.DefaultLimits)
+ // The hub writes the h2 preface straight after the 101, so br may
+ // already hold it: the box serves what br over-read first.
+ tunnelclient.ServeWith(&readerConn{Conn: conn, r: io.MultiReader(br, conn)}, namedHandler("box-2"), tunnelclient.DefaultLimits)
close(served)
}()
select {
diff --git a/internal/edge/tunnel_upgrade_test.go b/internal/edge/tunnel_upgrade_test.go
index e741595a5..51343a347 100644
--- a/internal/edge/tunnel_upgrade_test.go
+++ b/internal/edge/tunnel_upgrade_test.go
@@ -246,7 +246,9 @@ func TestOldBoxKeepsThe502(t *testing.T) {
if err != nil || resp.StatusCode != http.StatusSwitchingProtocols {
t.Fatalf("registration = %v, %v; want 101", resp, err)
}
- go tunnelclient.ServeWith(conn, box, tunnelclient.DefaultLimits)
+ // The hub writes the h2 preface straight after the 101, so br may
+ // already hold it: the box serves what br over-read first.
+ go tunnelclient.ServeWith(&readerConn{Conn: conn, r: io.MultiReader(br, conn)}, box, tunnelclient.DefaultLimits)
waitForTunnelState(t, hub.tunnels, "away/box-2", "connected", e2eReconnectBound)
if tc := hub.tunnels.channel("away/box-2"); tc == nil || tc.caps != "" {
t.Fatalf("away/box-2 = %+v, want a channel with no caps", tc)
diff --git a/internal/edge/upgrades_test.go b/internal/edge/upgrades_test.go
index 1e9b2da67..085780d78 100644
--- a/internal/edge/upgrades_test.go
+++ b/internal/edge/upgrades_test.go
@@ -451,10 +451,10 @@ func TestRegistrationRecordsCaps(t *testing.T) {
if err != nil || resp.StatusCode != http.StatusSwitchingProtocols {
t.Fatalf("registration = %v, %v; want 101", resp, err)
}
- if br.Buffered() != 0 {
- t.Fatalf("%d bytes after the 101", br.Buffered())
- }
- go tunnelclient.ServeWith(conn, namedHandler("old"), tunnelclient.DefaultLimits)
+ // No check that br is empty here: the hub writes the h2 preface straight
+ // after the 101, so the read above may already hold it, depending only on
+ // timing. The box serves what br over-read first, as tunnelclient.Dial does.
+ go tunnelclient.ServeWith(&readerConn{Conn: conn, r: io.MultiReader(br, conn)}, namedHandler("old"), tunnelclient.DefaultLimits)
waitForTunnelState(t, hub.tunnels, "t1", "connected", e2eReconnectBound)
tc := hub.tunnels.channel("t1")
if tc == nil || tc.caps != "" {