carlos / platform Public

edge: a -tunnel row's login passes through to the box; witness fixes (#471 Task 6 fix round 1)

Controller ruling on Astra's Task 6 finding 1: a login POST to an
ungated static -tunnel row (ArmTunnel) is passed down the tunnel to the
box, as a forward row's already is (loginPassesThrough). Before, the
hub 404'd it, so a -tunnel row gated on its box could not be logged
into at all. A -tunnel row gated at the hub still judges the login
itself; every other ungated row still 404s. Spec §4.5 says so; §7 has
Task 6's card (sites Edge.ServeHTTP -whole:Route, new
loginPassesThrough; seal unchanged).

Test-first: case 2b is restored as the brief states (gate only on the
box's t.example.com row) and was shown to 404 before the change. A
hub-gated -tunnel row and an ungated socket row's 404 are extra cases.

Witness fixes (findings 2-4): each app socket is matched to its
handshake by a unique tws query id, not a drained queue; both endpoints
must close before one deadline taken at the trigger (the registry write
under Watch, Reload's return, the close call); the trusted child runs
under exec.CommandContext bounded by the test's deadline, gets its own
-test.timeout, is killed and reaped on every abnormal path, and scanner
errors fail the test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 021c7307bbe1286358bb630746d0d2c006638578 parent eef20b7
4 files changed, +236 −70
  • docs/superpowers/specs/2026-10-30-tunnel-websockets-design.md +19 −0
  • internal/edge/edge.go +16 −1
  • internal/edge/trusted_child_test.go +41 −5
  • internal/edge/tunnel_ws_e2e_test.go +160 −64

Intent

Human

<task-notification> <task-id>b47jdwx7c</task-id> <tool-use-id>toolu_01Vysi3LAnTFAzvnd5djnmir</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/ba9fa944-a429-4788-9b3c-f2b2f42617f3/tasks/b47jdwx7c.output</output-file> <status>completed</status> <summary>Background command "Run Astra review of #471 Task 6" completed (exit code 0)</summary> </task-notification>

Attributed to this commit: 2.68 g CO₂e (282,849 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on design-tunnel-websockets-471, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.

diff --git a/docs/superpowers/specs/2026-10-30-tunnel-websockets-design.md b/docs/superpowers/specs/2026-10-30-tunnel-websockets-design.md
index fd3f43ebf..8d2c8f3ff 100644
--- a/docs/superpowers/specs/2026-10-30-tunnel-websockets-design.md
+++ b/docs/superpowers/specs/2026-10-30-tunnel-websockets-design.md
@@ -526,6 +526,7 @@ passes it; the box's `gate.Admit` runs before the box's upgrade branch
(`edge.go:2083-2104`) and strips it. A handshake without it gets the
gate's answer as an ordinary response (§4.3, last paragraph), never a
101. The witness logs in through the hub and then upgrades.
+The tunnel arm now passes the login through as well: a `POST /.carlos/gate` to an ungated `-tunnel` row goes down the tunnel to the box as a forward row's does (`loginPassesThrough`, Task 6 fix round 1), and a `-tunnel` row gated at the hub still judges the login itself.
### 4.6 One enforcement point, and `CloseAccountUpgrades` on a real socket
@@ -1072,6 +1073,24 @@ changed, and why the card has no row for it:
The seal is unchanged.
+Task 6's card (fix round 1, the controller's ruling that a `-tunnel`
+row's login passes through, §4.5) changes two rows from Task 5's:
+
+- `sites` `Edge.ServeHTTP`, `-whole:Route`: the `loginPost` expression
+ no longer calls `up.route.IsForward()` itself; it calls
+ `loginPassesThrough(up)`.
+- `new` `loginPassesThrough` (edge.go): `+registry:Gate`,
+ `+whole:Route` x2. It decides, from the route row and the tenancy
+ `Reload` already put on the entry, that a login POST to a forward row
+ (as before) or to an ungated `ArmTunnel` row is not this hub's to
+ judge, so it is proxied down the tunnel unparsed. Serving only: no
+ file, no root-owned state, nothing opened or followed; a row gated at
+ the hub still judges the login here, and every other ungated row still
+ answers 404.
+
+The other rows (`reads` `Edge.Reload`; the two `new` tunnelclient `Read`
+units) and the seal are unchanged. No overlay is proposed here (Task 9).
+
The reviewer is Astra (Claude author), recorded on #280.
Copy. Two new cause descriptions in `internal/apperr/availability.go`
diff --git a/internal/edge/edge.go b/internal/edge/edge.go
index db7a67936..d245243e0 100644
--- a/internal/edge/edge.go
+++ b/internal/edge/edge.go
@@ -2019,8 +2019,14 @@ func (e *Edge) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// upstream is not re-resolved by `next` (a forward host has no path map
// here anyway), and the ungated-route 404 below does not fire: the POST
// reaches the fleet edge with its body and Content-Length intact.
+ //
+ // The same holds for an UNGATED static -tunnel row (#471 spec §4.5):
+ // its gate, if it has one, is the box's, on the box's own row, so the
+ // hub passes the login down the tunnel like any other request and the
+ // box judges it. A -tunnel row gated here is unchanged: this hub renders
+ // the form and judges the password itself (loginPassesThrough).
loginPost := r.Method == http.MethodPost && r.URL.Path == gate.LoginPath &&
- !up.route.IsForward()
+ !loginPassesThrough(up)
if loginPost {
r.Body = http.MaxBytesReader(w, r.Body, gate.MaxFormBody)
if err := r.ParseForm(); err != nil {
@@ -3318,6 +3324,15 @@ func tunnelProxy(name string, hub *TunnelHub) *httputil.ReverseProxy {
}
}
+// loginPassesThrough reports whether a login POST to up is not this edge's
+// to judge and goes on to the box behind it, body intact: a forward row
+// (the fleet edge holds the gate; fleet public routes spec §4.8), or a
+// static -tunnel row with no gate here (#471 spec §4.5: the box holds it).
+// The arm is the tenancy Reload gave the entry, the one shared decision.
+func loginPassesThrough(up upstream) bool {
+ return up.route.IsForward() || (up.tenancy.Arm == ArmTunnel && up.route.Gate == "")
+}
+
// tunnelUpgradeError answers the tunnel transport's upgrade refusals for
// tunnelProxy and fleetProxy (#471 spec §4.8, §4.9), and reports whether
// err was one:
diff --git a/internal/edge/trusted_child_test.go b/internal/edge/trusted_child_test.go
index 05c84c77f..7d4e8dfa1 100644
--- a/internal/edge/trusted_child_test.go
+++ b/internal/edge/trusted_child_test.go
@@ -17,6 +17,7 @@ package edge
import (
"bufio"
+ "context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
@@ -147,7 +148,19 @@ func runChild(t *testing.T, wrongCA bool) (bool, tlsFixture) {
if wrongCA {
trust = newTLSFixture(t, t.TempDir()).CAFile
}
- cmd := exec.Command(os.Args[0], childArgs(t)...)
+ // The child is bounded by this test's deadline: it gets its own
+ // -test.timeout inside it (so a hung child dumps its goroutines and
+ // exits first), and the context kills it shortly before the parent's
+ // own timeout would fire. Wait reaps it on every path.
+ ctx, cancel := context.WithCancel(context.Background())
+ var childTimeout time.Duration
+ if d, ok := t.Deadline(); ok {
+ ctx, cancel = context.WithDeadline(context.Background(), d.Add(-childKillMargin))
+ childTimeout = max(time.Until(d)-childTimeoutMargin, time.Second)
+ }
+ defer cancel()
+ cmd := exec.CommandContext(ctx, os.Args[0], childArgs(t, childTimeout)...)
+ cmd.WaitDelay = 5 * time.Second // pipes and the reap after a kill
cmd.Env = append(os.Environ(),
"SSL_CERT_FILE="+trust,
"SSL_CERT_DIR="+t.TempDir(),
@@ -168,15 +181,35 @@ func runChild(t *testing.T, wrongCA bool) (bool, tlsFixture) {
for sc.Scan() {
t.Log(sc.Text())
}
- if err := cmd.Wait(); err != nil {
- t.Fatalf("the trusted child failed: %v", err)
+ scanErr := sc.Err()
+ if scanErr != nil {
+ // Nothing reads the pipe any more: a child still writing would
+ // block on it, so end it here; Wait below reaps it.
+ cmd.Process.Kill()
+ }
+ waitErr := cmd.Wait()
+ switch {
+ case scanErr != nil:
+ t.Fatalf("reading the trusted child's output: %v (child: %v)", scanErr, waitErr)
+ case ctx.Err() != nil:
+ t.Fatalf("the trusted child was killed at this test's deadline: %v", waitErr)
+ case waitErr != nil:
+ t.Fatalf("the trusted child failed: %v", waitErr)
}
return false, fx
}
+// childKillMargin is how long before the parent test's deadline the child
+// is killed; childTimeoutMargin how much earlier the child's own
+// -test.timeout fires, so it can report a hang itself.
+const (
+ childKillMargin = 10 * time.Second
+ childTimeoutMargin = 20 * time.Second
+)
+
// childArgs runs this test, and the sub-tests the parent's -test.run
-// selects under it, once and verbosely.
-func childArgs(t *testing.T) []string {
+// selects under it, once and verbosely, within timeout (0: the default).
+func childArgs(t *testing.T, timeout time.Duration) []string {
pattern := "^" + t.Name() + "$"
if f := flag.Lookup("test.run"); f != nil {
if _, sub, ok := strings.Cut(f.Value.String(), "/"); ok && sub != "" {
@@ -187,5 +220,8 @@ func childArgs(t *testing.T) []string {
if testing.Short() {
args = append(args, "-test.short")
}
+ if timeout > 0 {
+ args = append(args, "-test.timeout="+timeout.String())
+ }
return args
}
diff --git a/internal/edge/tunnel_ws_e2e_test.go b/internal/edge/tunnel_ws_e2e_test.go
index 89ce1f350..fc89e1ed5 100644
--- a/internal/edge/tunnel_ws_e2e_test.go
+++ b/internal/edge/tunnel_ws_e2e_test.go
@@ -68,7 +68,9 @@ import (
const (
wsHost = "shop.example.com" // forward row on home, gated on the box
- wsTunnelHost = "t.example.com" // the hub's -tunnel row, channel t1
+ wsTunnelHost = "t.example.com" // the hub's -tunnel row, channel t1, gated on the box
+ wsHubGateHost = "tg.example.com" // a -tunnel row on t1 gated on the hub instead
+ wsPlainHost = "plain.example.com" // an ungated socket row the hub serves itself
wsLimitedHost = "limited.example.com" // forward row with the hub's burst 2
wsRawHost = "raw.example.com" // forward row the box answers by hand
wsOldHost = "old.example.com" // forward row on away, for the old box
@@ -98,11 +100,11 @@ type seenReq struct {
// cableApp is the WebSocket app behind the box, Action Cable shaped.
type cableApp struct {
- mu sync.Mutex
- seen []seenReq // every request that reached the app, in order
- conns map[*appConn]struct{}
- opened chan *appConn
- srv *websocket.Server
+ mu sync.Mutex
+ seen []seenReq // every request that reached the app, in order
+ conns map[*appConn]struct{}
+ byID map[string]chan *appConn // each socket, by its handshake's tws id
+ srv *websocket.Server
}
// appConn is the app's end of one accepted socket.
@@ -120,7 +122,7 @@ type appConn struct {
}
func newCableApp() *cableApp {
- a := &cableApp{conns: make(map[*appConn]struct{}), opened: make(chan *appConn, 1024)}
+ a := &cableApp{conns: make(map[*appConn]struct{}), byID: make(map[string]chan *appConn)}
a.srv = &websocket.Server{
// Action Cable's same-origin rule and its one subprotocol.
Handshake: func(cfg *websocket.Config, r *http.Request) error {
@@ -187,9 +189,11 @@ func (a *cableApp) serve(ws *websocket.Conn) {
a.mu.Unlock()
close(c.closed)
}()
- select {
- case a.opened <- c:
- default:
+ if id := r.URL.Query().Get(wsIDParam); id != "" {
+ select {
+ case a.slot(id) <- c:
+ default:
+ }
}
a.send(c, map[string]any{"type": "welcome"})
go func() {
@@ -280,27 +284,40 @@ func (a *cableApp) closeAll() {
}
}
-// drainOpened forgets sockets opened earlier, so the next one read is new.
-func (a *cableApp) drainOpened() {
- for {
- select {
- case <-a.opened:
- default:
- return
- }
+// wsIDParam is the query parameter that names one handshake, so the app's
+// end of a socket is matched to the client's by identity, not by order.
+const wsIDParam = "tws"
+
+// newHandshakeID is a fresh tws value.
+func newHandshakeID() string {
+ b := make([]byte, 8)
+ rand.Read(b)
+ return fmt.Sprintf("%x", b)
+}
+
+// slot is the one-socket channel for handshake id.
+func (a *cableApp) slot(id string) chan *appConn {
+ a.mu.Lock()
+ defer a.mu.Unlock()
+ ch := a.byID[id]
+ if ch == nil {
+ ch = make(chan *appConn, 1)
+ a.byID[id] = ch
}
+ return ch
}
-func (a *cableApp) nextOpened(t *testing.T, host string) *appConn {
+// opened is the app's end of the socket that handshake id opened.
+func (a *cableApp) opened(t *testing.T, id, host string) *appConn {
t.Helper()
select {
- case c := <-a.opened:
+ case c := <-a.slot(id):
if c.host != host {
- t.Fatalf("the app opened a socket for %s, want %s", c.host, host)
+ t.Fatalf("handshake %s opened a socket for %s, want %s", id, c.host, host)
}
return c
case <-time.After(5 * time.Second):
- t.Fatalf("the app opened no socket for %s within 5 s", host)
+ t.Fatalf("the app opened no socket for handshake %s (%s) within 5 s", id, host)
return nil
}
}
@@ -539,12 +556,19 @@ func newWSScene(t *testing.T, fx tlsFixture) *wsScene {
t.Fatalf("PutForward(%s): %v", rt.Host, err)
}
}
- if err := s.hubReg.Upsert(wsTunnelRow("t1")); err != nil {
- t.Fatal(err)
+ for _, rt := range []registry.Route{
+ wsTunnelRow(wsTunnelHost, "t1"),
+ wsTunnelRow(wsHubGateHost, "t1"),
+ {Host: wsPlainHost, Kind: "instance", Status: "ready", Socket: sock, Account: fwdOwner, App: "plain", RateLimit: wsWideRate},
+ } {
+ if err := s.hubReg.Upsert(rt); err != nil {
+ t.Fatal(err)
+ }
}
- // A -tunnel row is a platform row on the hub: its gate is the hub's
- // (the hub 404s a login POST to an ungated non-forward row).
- if err := s.hubReg.SetGate(wsTunnelHost, s.hash); err != nil {
+ // t.example.com is gated on the box only (spec §4.5: the hub passes the
+ // login and the cookie, the box judges them); tg.example.com on the hub
+ // only, whose gate judges and strips before the tunnel.
+ if err := s.hubReg.SetGate(wsHubGateHost, s.hash); err != nil {
t.Fatal(err)
}
s.hub, err = New(s.hubReg, nil)
@@ -572,6 +596,7 @@ func newWSScene(t *testing.T, fx tlsFixture) *wsScene {
for _, rt := range []registry.Route{
wsBoxRow(wsHost, sock, ""),
wsBoxRow(wsTunnelHost, sock, ""),
+ wsBoxRow(wsHubGateHost, sock, ""),
wsBoxRow(wsLimitedHost, sock, wsBoxRate),
wsBoxRow(wsOldHost, sock, ""),
} {
@@ -579,8 +604,10 @@ func newWSScene(t *testing.T, fx tlsFixture) *wsScene {
t.Fatal(err)
}
}
- if err := s.boxReg.SetGate(wsHost, s.hash); err != nil {
- t.Fatal(err)
+ for _, h := range []string{wsHost, wsTunnelHost} {
+ if err := s.boxReg.SetGate(h, s.hash); err != nil {
+ t.Fatal(err)
+ }
}
s.box, err = New(s.boxReg, nil)
if err != nil {
@@ -653,8 +680,8 @@ func wsForward(host, fleet, rate string) registry.Route {
return rt
}
-func wsTunnelRow(channel string) registry.Route {
- return registry.Route{Host: wsTunnelHost, Kind: "instance", Status: "ready", Tunnel: channel,
+func wsTunnelRow(host, channel string) registry.Route {
+ return registry.Route{Host: host, Kind: "instance", Status: "ready", Tunnel: channel,
Account: fwdOwner, App: "shop", RateLimit: wsWideRate}
}
@@ -973,9 +1000,9 @@ type cableClient struct {
// and returns it with the app's end.
func (s *wsScene) openCable(t *testing.T, host, cookie string) (*cableClient, *appConn) {
t.Helper()
- s.app.drainOpened()
+ id := newHandshakeID()
conn := s.dialTLS(t, s.addr, host)
- cfg, err := websocket.NewConfig("wss://"+host+"/cable", "https://"+host)
+ cfg, err := websocket.NewConfig("wss://"+host+"/cable?"+wsIDParam+"="+id, "https://"+host)
if err != nil {
t.Fatal(err)
}
@@ -1012,7 +1039,7 @@ func (s *wsScene) openCable(t *testing.T, host, cookie string) (*cableClient, *a
}
}()
t.Cleanup(func() { ws.Close() })
- return c, s.app.nextOpened(t, host)
+ return c, s.app.opened(t, id, host)
}
func (c *cableClient) next(t *testing.T) string {
@@ -1055,14 +1082,41 @@ func (c *cableClient) echo(t *testing.T, text string) {
}
}
-// waitClosed waits for ch to close within d.
-func waitClosed(t *testing.T, ch <-chan struct{}, d time.Duration, what string) {
+// closedBy requires the client's end c and the app's end ac (either may be
+// nil) to have closed before deadline: one bound, taken at the trigger, for
+// both.
+func closedBy(t *testing.T, deadline time.Time, what string, c *cableClient, ac *appConn) {
t.Helper()
- select {
- case <-ch:
- case <-time.After(d):
- t.Fatalf("%s: not closed within %s", what, d)
+ timer := time.NewTimer(time.Until(deadline))
+ defer timer.Stop()
+ for _, e := range []struct {
+ name string
+ ch <-chan struct{}
+ set bool
+ }{{"the client's socket", chOf(c), c != nil}, {"the app's socket", chOfApp(ac), ac != nil}} {
+ if !e.set {
+ continue
+ }
+ select {
+ case <-e.ch:
+ case <-timer.C:
+ t.Fatalf("%s: %s was not closed by the deadline", what, e.name)
+ }
+ }
+}
+
+func chOf(c *cableClient) <-chan struct{} {
+ if c == nil {
+ return nil
+ }
+ return c.closed
+}
+
+func chOfApp(ac *appConn) <-chan struct{} {
+ if ac == nil {
+ return nil
}
+ return ac.closed
}
// notClosed checks ch has not closed.
@@ -1179,8 +1233,9 @@ func (s *wsScene) fullExchange(t *testing.T, host string) {
if got := ac.hdr.Get("X-Forwarded-Proto"); got != "https" {
t.Fatalf("the app saw X-Forwarded-Proto %q, want https", got)
}
+ closeBy := time.Now().Add(2 * time.Second)
c.ws.Close()
- waitClosed(t, ac.closed, 2*time.Second, "the app's end after the client closed")
+ closedBy(t, closeBy, "after the client closed", nil, ac)
}
// ---- the witness ------------------------------------------------------------
@@ -1240,24 +1295,63 @@ func TestTunnelWebSocketEndToEnd(t *testing.T) {
})
t.Run("case02b_tunnel_arm", func(t *testing.T) {
- ten, ok := tenancyAt(s.hub, wsTunnelHost)
- if !ok || ten.Arm != ArmTunnel || ten.Tunnel != "t1" {
- t.Fatalf("%s is served %v under %+v, want the -tunnel arm on t1", wsTunnelHost, ok, ten)
+ // The brief's shape: the hub's -tunnel row is ungated and the box's
+ // row carries the gate, so the login and the cookie cross the
+ // tunnel and the box judges both.
+ for _, host := range []string{wsTunnelHost, wsHubGateHost} {
+ ten, ok := tenancyAt(s.hub, host)
+ if !ok || ten.Arm != ArmTunnel || ten.Tunnel != "t1" {
+ t.Fatalf("%s is served %v under %+v, want the -tunnel arm on t1", host, ok, ten)
+ }
+ }
+ if g := s.hub.current()[wsTunnelHost].route.Gate; g != "" {
+ t.Fatalf("the hub's %s row has a gate", wsTunnelHost)
+ }
+ if g := s.box.current()[wsTunnelHost].route.Gate; g != s.hash {
+ t.Fatalf("the box's %s row has gate %q, want the scene's", wsTunnelHost, g)
}
s.fullExchange(t, wsTunnelHost)
waitOpen(t, s.hub, s.channel(t, "t1"), 0, "after case 2b")
})
+ t.Run("case02b_hub_gated_tunnel_row", func(t *testing.T) {
+ // Extra coverage: a -tunnel row with its own gate at the hub. The
+ // hub renders, judges and strips; the box's row is ungated.
+ if g := s.box.current()[wsHubGateHost].route.Gate; g != "" {
+ t.Fatalf("the box's %s row has a gate", wsHubGateHost)
+ }
+ s.fullExchange(t, wsHubGateHost)
+ waitOpen(t, s.hub, s.channel(t, "t1"), 0, "after the hub-gated exchange")
+ })
+
+ t.Run("case02b_ungated_row_login_404", func(t *testing.T) {
+ // Unchanged: a login POST to an ungated row that is neither a
+ // forward row nor a -tunnel row is a 404 and reaches no app.
+ before := len(s.app.seenFor(wsPlainHost))
+ resp, _ := s.do(t, http.MethodPost, wsPlainHost, gate.LoginPath, "",
+ http.Header{"Content-Type": {"application/x-www-form-urlencoded"}}, "password=right&next=%2Fcable")
+ if resp.StatusCode != http.StatusNotFound {
+ t.Fatalf("a login POST to the ungated %s: %d, want 404", wsPlainHost, resp.StatusCode)
+ }
+ if n := len(s.app.seenFor(wsPlainHost)); n != before {
+ t.Fatalf("the app saw %d requests for %s, want none", n-before, wsPlainHost)
+ }
+ // Control: the row serves.
+ if resp, body := s.do(t, "GET", wsPlainHost, "/x", "", nil, ""); resp.StatusCode != 200 || body != "plain:"+wsPlainHost+"/x" {
+ t.Fatalf("a GET to %s: %d %q", wsPlainHost, resp.StatusCode, body)
+ }
+ })
+
t.Run("case03_close_account", func(t *testing.T) {
s.hub.SetPlanGovernedFor(func(rt registry.Route) bool { return rt.Account == fwdOwner })
defer s.hub.SetPlanGovernedFor(nil)
c, ac := s.openShop(t)
c.echo(t, "before the close")
+ deadline := time.Now().Add(2 * time.Second)
if n := s.hub.CloseAccountUpgrades(fwdOwner); n != 1 {
t.Fatalf("CloseAccountUpgrades(%s) with one socket open closed %d, want 1", fwdOwner, n)
}
- waitClosed(t, c.closed, 2*time.Second, "the client's socket")
- waitClosed(t, ac.closed, 2*time.Second, "the app's socket")
+ closedBy(t, deadline, "CloseAccountUpgrades", c, ac)
if n := s.hub.CloseAccountUpgrades(fwdOwner); n != 0 {
t.Fatalf("a second CloseAccountUpgrades closed %d, want 0", n)
}
@@ -1314,13 +1408,13 @@ func TestTunnelWebSocketEndToEnd(t *testing.T) {
for _, drop := range []bool{false, true} {
c, ac := s.openShop(t)
c.echo(t, "before the app closes")
+ deadline := time.Now().Add(2 * time.Second)
if drop {
ac.raw.Close()
} else {
ac.ws.Close()
}
- waitClosed(t, ac.closed, 2*time.Second, "the app's socket")
- waitClosed(t, c.closed, 2*time.Second, fmt.Sprintf("the client's socket (dropped %v)", drop))
+ closedBy(t, deadline, fmt.Sprintf("the app closed (dropped %v)", drop), c, ac)
// The hub holds the slot until the client closes its side, as
// a browser does once it reads the close.
c.ws.Close()
@@ -1444,9 +1538,9 @@ func TestTunnelWebSocketEndToEnd(t *testing.T) {
old := s.channel(t, wsBox1)
c, ac := s.openShop(t)
c.echo(t, "before a second loop registers")
+ deadline := time.Now().Add(2 * time.Second)
s.startLoop("second", wsBox1, fwdBox1Tok) // a second loop as home/box-1
- waitClosed(t, c.closed, 2*time.Second, "the client's socket")
- waitClosed(t, ac.closed, 2*time.Second, "the app's socket")
+ closedBy(t, deadline, "re-registration", c, ac)
s.stopLoop("second")
waitOpen(t, s.hub, old, 0, "the replaced connection")
s.waitServing(t, 15*time.Second)
@@ -1457,8 +1551,7 @@ func TestTunnelWebSocketEndToEnd(t *testing.T) {
if err := s.hub.Reload(); err != nil {
t.Fatal(err)
}
- waitClosed(t, c.closed, 2*time.Second, "the client's socket")
- waitClosed(t, ac.closed, 2*time.Second, "the app's socket")
+ closedBy(t, time.Now().Add(2*time.Second), "detach", c, ac)
setBoxStatus(t, s.hubReg, "home", "box-1", "active")
if err := s.hub.Reload(); err != nil {
t.Fatal(err)
@@ -1491,9 +1584,9 @@ func TestTunnelWebSocketEndToEnd(t *testing.T) {
old := s.channel(t, wsBox1)
c, ac := s.openShop(t)
c.echo(t, "before the tunnel goes")
+ deadline := time.Now().Add(10 * time.Second)
wait := s.cancelLoop(wsBox1)
- waitClosed(t, c.closed, 10*time.Second, "the client's socket")
- waitClosed(t, ac.closed, 10*time.Second, "the app's socket")
+ closedBy(t, deadline, "tunnel loss", c, ac)
wait()
waitOpen(t, s.hub, old, 0, "the lost connection")
s.startLoop(wsBox1, wsBox1, fwdBox1Tok)
@@ -1700,18 +1793,18 @@ func (s *wsScene) modifyResponseFails(t *testing.T, cookie, what string) {
func (s *wsScene) openWrapped(t *testing.T, cookie string, mode int32, what string) (hs, chan struct{}, *upgradeReservation, *appConn) {
t.Helper()
s.drainInserted()
- s.app.drainOpened()
+ id := newHandshakeID()
barrier := make(chan struct{})
s.wrapBarrier.Store(&barrier)
s.wrapMode.Store(mode)
- h := s.sendHandshake(t, s.wrapAddr, wsHost, "/cable", cookie, "websocket")
+ h := s.sendHandshake(t, s.wrapAddr, wsHost, "/cable?"+wsIDParam+"="+id, cookie, "websocket")
res := s.nextInserted(t)
select {
case <-s.wrapReturned:
case <-time.After(5 * time.Second):
t.Fatalf("%s: the hub did not return within 5 s", what)
}
- return h, barrier, res, s.app.nextOpened(t, wsHost)
+ return h, barrier, res, s.app.opened(t, id, wsHost)
}
// hijackNotSupported: the hub behind a writer whose Hijack answers
@@ -1728,6 +1821,7 @@ func (s *wsScene) hijackNotSupported(t *testing.T, cookie, what string) {
if got := res.firstReleaser(); got != relNone {
t.Fatalf("%s: with the request live the reservation was released by %d", what, got)
}
+ deadline := time.Now().Add(2 * time.Second)
close(barrier)
h.read(t)
wantStatus(t, h, http.StatusBadGateway, what)
@@ -1735,7 +1829,7 @@ func (s *wsScene) hijackNotSupported(t *testing.T, cookie, what string) {
if got := res.firstReleaser(); got != relBackstop {
t.Fatalf("%s: the first releaser is %d, want relBackstop (%d)", what, got, relBackstop)
}
- waitClosed(t, ac.closed, 2*time.Second, what+": the app's socket")
+ closedBy(t, deadline, what, nil, ac)
}
// noHijack: a writer with no Hijack at all. slotWriter's Hijack then
@@ -1756,11 +1850,12 @@ func (s *wsScene) noHijack(t *testing.T, cookie, what string) {
if got := res.firstReleaser(); got != relBody {
t.Fatalf("%s: the first releaser is %d, want relBody (%d)", what, got, relBody)
}
+ deadline = time.Now().Add(2 * time.Second)
close(barrier)
h.read(t)
wantStatus(t, h, http.StatusBadGateway, what)
waitOpen(t, s.hub, tc, 0, what)
- waitClosed(t, ac.closed, 2*time.Second, what+": the app's socket")
+ closedBy(t, deadline, what, nil, ac)
}
// revocations is spec §8.2 case 15: on an account plans do not govern, a
@@ -1814,16 +1909,17 @@ func (s *wsScene) revocations(t *testing.T) {
}
}()
}
+ // One deadline for both ends, from the trigger: the registry
+ // write under Watch, Reload's return when called directly.
+ deadline := time.Now().Add(5 * time.Second)
rv.revoke(t)
- bound := 5 * time.Second
if !watch {
if err := s.hub.Reload(); err != nil {
t.Fatal(err)
}
- bound = 2 * time.Second
+ deadline = time.Now().Add(2 * time.Second)
}
- waitClosed(t, c.closed, bound, "the client's socket")
- waitClosed(t, ac.closed, bound, "the app's socket")
+ closedBy(t, deadline, name, c, ac)
before := len(s.app.seenFor(rv.host))
h := s.handshake(t, rv.host, "/cable", cookie)
if h.resp.StatusCode == http.StatusSwitchingProtocols {
@@ -1865,7 +1961,7 @@ func (s *wsScene) putForward(t *testing.T, rt registry.Route) {
func (s *wsScene) pointTunnel(t *testing.T, channel string) {
t.Helper()
- if err := s.hubReg.Upsert(wsTunnelRow(channel)); err != nil {
+ if err := s.hubReg.Upsert(wsTunnelRow(wsTunnelHost, channel)); err != nil {
t.Fatal(err)
}
got, err := s.hubReg.Get(wsTunnelHost)