edge: hold the stalled demo upload test to its bound (#1248)
Astra round 1, P2-2. The stalled-upload test accepted any close within a ten second connection deadline, so slow cleanup still passed. It now measures from the moment the whole answer arrives and allows exitLingerBound plus two seconds for a loaded runner, with a separate generous deadline for setup. It covers declared-length and chunked uploads on both the local and backhaul paths.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+57
−14
internal/edge/demo_api_duplex_test.go+57 −14
CI cancelled — superseded by a newer push — run details
diff --git a/internal/edge/demo_api_duplex_test.go b/internal/edge/demo_api_duplex_test.go| index b2aad974e..a3b6c5da4 100644 |
| --- a/internal/edge/demo_api_duplex_test.go |
| +++ b/internal/edge/demo_api_duplex_test.go |
| @@ -392,20 +392,19 @@ func demoDuplexLocalFront(t *testing.T, s *demoDuplexScene) *httptest.Server { |
| return front |
| } |
| -// A client that stops sending after the early answer does not hold the |
| -// connection: the edge closes it at the linger bound. |
| -func TestDemoAPIAliasEarlyAnswerStalledUploadCloses(t *testing.T) { |
| - s := &demoDuplexScene{} |
| - front := demoDuplexLocalFront(t, s) |
| - conn, err := net.Dial("tcp", front.Listener.Addr().String()) |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - defer conn.Close() |
| - if err := conn.SetDeadline(time.Now().Add(10 * time.Second)); err != nil { |
| +// demoStalledCloseTolerance is what a loaded runner may add to |
| +// exitLingerBound between the edge's deadline and the client seeing the close. |
| +const demoStalledCloseTolerance = 2 * time.Second |
| + |
| +// checkDemoStalledUploadCloses sends the start of an upload, reads the early |
| +// answer and sends nothing more. The clock starts when the whole answer has |
| +// arrived, so a slow setup takes nothing from the cleanup's allowance. |
| +func checkDemoStalledUploadCloses(t *testing.T, conn net.Conn, framing string) { |
| + t.Helper() |
| + if err := conn.SetDeadline(time.Now().Add(time.Minute)); err != nil { |
| t.Fatal(err) |
| } |
| - if _, err := io.WriteString(conn, "POST /api/demo/apps HTTP/1.1\r\nHost: "+demoAPISite+"\r\nContent-Length: 100\r\n\r\nstart"); err != nil { |
| + if _, err := io.WriteString(conn, "POST /api/demo/apps HTTP/1.1\r\nHost: "+demoAPISite+"\r\n"+framing); err != nil { |
| t.Fatal(err) |
| } |
| br := bufio.NewReader(conn) |
| @@ -413,14 +412,58 @@ func TestDemoAPIAliasEarlyAnswerStalledUploadCloses(t *testing.T) { |
| if err != nil { |
| t.Fatal(err) |
| } |
| - if body, err := io.ReadAll(resp.Body); err != nil || resp.StatusCode != http.StatusForbidden || string(body) != demoDuplexRefusal || !resp.Close { |
| + body, err := io.ReadAll(resp.Body) |
| + answered := time.Now() |
| + if err != nil || resp.StatusCode != http.StatusForbidden || string(body) != demoDuplexRefusal || !resp.Close { |
| t.Fatalf("the early answer: %d %q close=%v, %v", resp.StatusCode, body, resp.Close, err) |
| } |
| + // Long enough to report how late a slow close was. |
| + if err := conn.SetReadDeadline(answered.Add(30 * time.Second)); err != nil { |
| + t.Fatal(err) |
| + } |
| if _, err := br.ReadByte(); !demoDuplexGone(err) { |
| - t.Fatalf("the stalled connection: %v, want it closed", err) |
| + t.Fatalf("the stalled connection after %s: %v, want it closed", time.Since(answered), err) |
| + } |
| + if took, limit := time.Since(answered), exitLingerBound+demoStalledCloseTolerance; took > limit { |
| + t.Fatalf("the stalled connection closed %s after the answer, want at most %s", took, limit) |
| } |
| } |
| +// A client that stops sending after the early answer does not hold the |
| +// connection: the edge closes it at the linger bound. |
| +func TestDemoAPIAliasEarlyAnswerStalledUploadCloses(t *testing.T) { |
| + framings := []struct{ name, start string }{ |
| + {"content-length", "Content-Length: 100\r\n\r\nstart"}, |
| + {"chunked", "Transfer-Encoding: chunked\r\n\r\n5\r\nstart\r\n"}, |
| + } |
| + t.Run("local", func(t *testing.T) { |
| + front := demoDuplexLocalFront(t, &demoDuplexScene{}) |
| + for _, f := range framings { |
| + t.Run(f.name, func(t *testing.T) { |
| + conn, err := net.Dial("tcp", front.Listener.Addr().String()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer conn.Close() |
| + checkDemoStalledUploadCloses(t, conn, f.start) |
| + }) |
| + } |
| + }) |
| + t.Run("backhaul", func(t *testing.T) { |
| + scene := NewDemoAPITestIngress(t, &demoDuplexScene{}, release.NewDirStore(t.TempDir())) |
| + for _, f := range framings { |
| + t.Run(f.name, func(t *testing.T) { |
| + conn, err := tls.Dial("tcp", scene.servers[1].Listener.Addr().String(), &tls.Config{RootCAs: scene.ca.pool, ServerName: demoAPISite, MinVersion: tls.VersionTLS12, NextProtos: []string{"http/1.1"}}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer conn.Close() |
| + checkDemoStalledUploadCloses(t, conn, f.start) |
| + }) |
| + } |
| + }) |
| +} |
| + |
| // An upload the console read to its end before answering keeps its |
| // connection, as every ordinary request does. |
| func TestDemoAPIAliasWholeUploadKeepsConnection(t *testing.T) { |