carlos / platform Public

test(edge): keep the held WebSocket backend listener owned

Replace the free-port/rebind gap with an owned backend and three injected
connection refusals. Keep the real retry path, 101 handshake and echo,
and require exactly four dial attempts.

Validation: 150 repeated runs across cpu=1,2,4 and 30 race runs passed.
The old test passed 30 baseline runs; the port ownership bug was identified
from the closed listener and delayed rebind.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 16f29ed0dcb1be1f670d8f215bea068c72d94f68 parent 74757d1
1 file changed, +21 −3
  • internal/edge/localhold_test.go +21 −3

Intent

Human

<task-notification> <task-id>baqtx4fr1</task-id> <tool-use-id>toolu_01KK3JJUnn5tV3EGNp3Zw99r</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/9fdb19f6-7964-4108-a723-1323c2914a76/tasks/baqtx4fr1.output</output-file> <status>completed</status> <summary>Background command "Wait for CI then merge if green and main unmoved" completed (exit code 0)</summary> </task-notification>

diff --git a/internal/edge/localhold_test.go b/internal/edge/localhold_test.go
index 314a4b996..44705dc66 100644
--- a/internal/edge/localhold_test.go
+++ b/internal/edge/localhold_test.go
@@ -316,9 +316,24 @@ func TestHeldDialPOSTBodyArrives(t *testing.T) {
}
func TestHeldDialWebSocketUpgrade(t *testing.T) {
- a := deadAddr(t)
- listenLater(t, a, 300*time.Millisecond, upgradeEchoHandler("ws"))
- _, srv := holdEdge(t, a, localHold{dial: 2 * time.Second, probe: probeTimeout})
+ // The backend is owned for the whole test and the first dials are refused
+ // by an injected dialer, so the hold's retry path runs against this exact
+ // server. A closed-then-rebound port (deadAddr + listenLater) can be taken
+ // by another process in the gap, and the test would then upgrade against
+ // a stranger.
+ backend := httptest.NewServer(upgradeEchoHandler("ws"))
+ t.Cleanup(backend.Close)
+ backendAddr := backend.Listener.Addr().String()
+ const refusals = 3
+ var attempts atomic.Int32
+ hold := localHold{dial: 2 * time.Second, probe: probeTimeout,
+ dialer: func(ctx context.Context, network, addr string) (net.Conn, error) {
+ if attempts.Add(1) <= refusals {
+ return nil, refusedErr()
+ }
+ return (&net.Dialer{}).DialContext(ctx, network, backendAddr)
+ }}
+ _, srv := holdEdge(t, backendAddr, hold)
c, err := net.Dial("tcp", strings.TrimPrefix(srv.URL, "http://"))
if err != nil {
@@ -351,6 +366,9 @@ func TestHeldDialWebSocketUpgrade(t *testing.T) {
if rest != "ws:hello\n" {
t.Fatalf("echo = %q, want %q", rest, "ws:hello\n")
}
+ if n := attempts.Load(); n != refusals+1 {
+ t.Fatalf("dial attempts = %d, want %d (%d refused, then the backend)", n, refusals+1, refusals)
+ }
}
func TestHeldDialsReleaseAfterHold(t *testing.T) {