mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 18:47:20 +01:00
* util: keep a request's context while its handler works quietly past the idle timeout A request that the server works on for longer than the listener's idle timeout, without reading or writing, lets that timeout end net/http's background read: the read the server keeps waiting while a handler runs, to see a client hang up. net/http takes the failed read for the client's connection failing and cancels the connection's context, so the request in progress, and every later request on the kept-alive connection, run with a canceled context. #10212 covered a long write-only response, whose writes now extend the read deadline; an answer that has not started yet has nothing to extend it. On the S3 gateway (-idleTimeout 120 s) such requests are the ones that answer only when their work is done, e.g. a large CopyObject into a versioned bucket, which the copy keep-alive of #12862 does not cover. After one, the next requests on that connection fail within a millisecond: a ListObjectsV2 answers 500, a GetObject and a PutObject 499 ClientDisconnected while the client is still connected. The filer's HTTP listener has a 10 s idle timeout by default. PauseIdleTimeoutWhileHandling, which newHttpServer now installs next to the ConnContext hook it already sets (S3, filer, IAM and master HTTP), takes reading out of the activity timeout from the moment a request's body has been read to its end (at once when it has none) until its handler returns: the client is waiting for the answer, so a connection that carries nothing means the server is at work. Reading a body, writing an answer and waiting for the next request stay under the timeout, and a client that closes its connection still cancels the context. Over HTTP/2 the frame reader keeps no deadline while such a handler runs and gets it back when the last one returns. Tests: weed/util/net_timeout_test.go (a quiet handler over HTTP/1.1, TLS and HTTP/2, and the next request on the same connection; the same after a 1 MiB body; a stalled body, an idle connection, an idle HTTP/2 connection and a client hang-up still end as before; concurrent requests; a waiting read held by the pause and released at its end; the context loss without the wrapper) and weed/command/server_http_test.go (newHttpServer). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * webdav: serve through newHttpServer so quiet handlers keep their context weed webdav serves a util.Listener with an idle timeout (10 s by default) but built a bare http.Server, so it had neither ConnContext nor the idle timeout pause. A COPY or MOVE of a large file answers only once it is done, and like the S3 and filer handlers it then lost its context, and so did every later request on the connection. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * util: pause the idle timeout for HTTP/1.x requests only HTTP/2 has no background read to protect: when its connection carries nothing for the timeout, the frame reader's deadline closes the connection, and no later request runs on it. That deadline is also what ends a stream whose client stopped reading, since the answer waits for flow-control credit, not on the socket, where the write deadline would end it. With the pause on HTTP/2 requests, a download whose client stopped reading held its handler and connection until the client acted, and a stream that declared content-length 0 but never ended could hold its handler the same way. Pause only HTTP/1.x requests; HTTP/2 keeps the timeout it had. Tests: TestStalledDownloadStillTimesOut over HTTP/1.1, TLS and HTTP/2 (the HTTP/2 case was still waiting after 8 s with the pause on HTTP/2) and TestOnlyHTTP1RequestsArePaused; the HTTP/2 quiet-handler cases are gone, with them the HTTP/2 subtest that flaked under -race. TestPauseUnderConcurrentRequests gets a 1 s idle timeout, so that a request waiting for its turn on a slow run does not find its connection closed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
534 lines
16 KiB
Go
534 lines
16 KiB
Go
package util
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/stats"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
// Conn reports every read and write to the stats channels; weed/server
|
|
// drains them in a weed process, a test binary of this package has to.
|
|
go stats.NewServerStats().Start()
|
|
os.Exit(m.Run())
|
|
}
|
|
|
|
const (
|
|
testIdleTimeout = 200 * time.Millisecond
|
|
// testQuietFor is how long the quiet handlers work without reading or
|
|
// writing: well past the idle timeout.
|
|
testQuietFor = 3 * testIdleTimeout
|
|
)
|
|
|
|
// countingListener counts the connections it accepts.
|
|
type countingListener struct {
|
|
net.Listener
|
|
accepted atomic.Int32
|
|
}
|
|
|
|
func (l *countingListener) Accept() (net.Conn, error) {
|
|
c, err := l.Listener.Accept()
|
|
if err == nil {
|
|
l.accepted.Add(1)
|
|
}
|
|
return c, err
|
|
}
|
|
|
|
// ctxState is what the test handlers answer: "ok" while the request context
|
|
// is live, else its error.
|
|
func ctxState(ctx context.Context) string {
|
|
if err := ctx.Err(); err != nil {
|
|
return err.Error()
|
|
}
|
|
return "ok"
|
|
}
|
|
|
|
// idleTimeoutTestServer serves the test handlers through a Listener whose
|
|
// activity timeout is testIdleTimeout, and /quiet and /upload work quietly
|
|
// for testQuietFor. mode is "http", "https" or "h2"; pause wraps the
|
|
// handlers in PauseIdleTimeoutWhileHandling.
|
|
type idleTimeoutTestServer struct {
|
|
*httptest.Server
|
|
listener *countingListener
|
|
quietFor time.Duration
|
|
// bodyErr gets the error of the body read on /upload when it fails.
|
|
bodyErr chan error
|
|
// hungUp gets the context state /wait saw once it stopped waiting.
|
|
hungUp chan string
|
|
// downloadErr gets the outcome of /download's writes.
|
|
downloadErr chan error
|
|
}
|
|
|
|
func newIdleTimeoutTestServer(t *testing.T, mode string, pause bool) *idleTimeoutTestServer {
|
|
t.Helper()
|
|
return newIdleTimeoutTestServerWith(t, mode, pause, testIdleTimeout, testQuietFor)
|
|
}
|
|
|
|
// newIdleTimeoutTestServerWith is newIdleTimeoutTestServer with its own idle
|
|
// timeout and quiet time.
|
|
func newIdleTimeoutTestServerWith(t *testing.T, mode string, pause bool, idle, quietFor time.Duration) *idleTimeoutTestServer {
|
|
t.Helper()
|
|
s := &idleTimeoutTestServer{
|
|
quietFor: quietFor,
|
|
bodyErr: make(chan error, 1),
|
|
hungUp: make(chan string, 1),
|
|
|
|
downloadErr: make(chan error, 1),
|
|
}
|
|
mux := http.NewServeMux()
|
|
quiet := func(ctx context.Context) {
|
|
select {
|
|
case <-time.After(s.quietFor):
|
|
case <-ctx.Done():
|
|
}
|
|
}
|
|
mux.HandleFunc("/quiet", func(w http.ResponseWriter, r *http.Request) {
|
|
quiet(r.Context())
|
|
fmt.Fprint(w, ctxState(r.Context()))
|
|
})
|
|
mux.HandleFunc("/ctx", func(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprint(w, ctxState(r.Context()))
|
|
})
|
|
mux.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) {
|
|
n, err := io.Copy(io.Discard, r.Body)
|
|
if err != nil {
|
|
s.bodyErr <- err
|
|
return
|
|
}
|
|
quiet(r.Context())
|
|
fmt.Fprintf(w, "%s %d", ctxState(r.Context()), n)
|
|
})
|
|
mux.HandleFunc("/handling", func(w http.ResponseWriter, r *http.Request) {
|
|
io.Copy(io.Discard, r.Body)
|
|
tc, _ := r.Context().Value(connContextKey{}).(*Conn)
|
|
if tc == nil {
|
|
fmt.Fprint(w, "no conn")
|
|
return
|
|
}
|
|
tc.mu.Lock()
|
|
defer tc.mu.Unlock()
|
|
fmt.Fprint(w, tc.handling)
|
|
})
|
|
mux.HandleFunc("/download", func(w http.ResponseWriter, r *http.Request) {
|
|
chunk := bytes.Repeat([]byte("z"), 64<<10)
|
|
var err error
|
|
for i := 0; i < 1024 && err == nil; i++ { // 64 MiB
|
|
_, err = w.Write(chunk)
|
|
}
|
|
s.downloadErr <- err
|
|
})
|
|
mux.HandleFunc("/wait", func(w http.ResponseWriter, r *http.Request) {
|
|
select {
|
|
case <-time.After(10 * time.Second):
|
|
case <-r.Context().Done():
|
|
}
|
|
s.hungUp <- ctxState(r.Context())
|
|
})
|
|
var h http.Handler = mux
|
|
if pause {
|
|
h = PauseIdleTimeoutWhileHandling(mux)
|
|
}
|
|
|
|
s.Server = httptest.NewUnstartedServer(h)
|
|
s.listener = &countingListener{Listener: s.Server.Listener}
|
|
s.Server.Listener = &Listener{Listener: s.listener, Timeout: idle}
|
|
s.Server.Config.ConnContext = ConnContext
|
|
switch mode {
|
|
case "http":
|
|
s.Server.Start()
|
|
case "https":
|
|
s.Server.StartTLS()
|
|
case "h2":
|
|
s.Server.EnableHTTP2 = true
|
|
s.Server.StartTLS()
|
|
default:
|
|
t.Fatalf("unknown mode %q", mode)
|
|
}
|
|
t.Cleanup(s.Server.Close)
|
|
return s
|
|
}
|
|
|
|
// client returns a client that sends every request over one connection.
|
|
func (s *idleTimeoutTestServer) client() *http.Client {
|
|
tr := s.Server.Client().Transport.(*http.Transport).Clone()
|
|
tr.MaxConnsPerHost = 1
|
|
tr.MaxIdleConnsPerHost = 1
|
|
return &http.Client{Transport: tr, Timeout: 10 * time.Second}
|
|
}
|
|
|
|
func (s *idleTimeoutTestServer) send(c *http.Client, method, path string, body io.Reader) (string, error) {
|
|
req, err := http.NewRequest(method, s.Server.URL+path, body)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
resp, err := c.Do(req)
|
|
if err != nil {
|
|
return "", fmt.Errorf("%s %s: %w", method, path, err)
|
|
}
|
|
defer resp.Body.Close()
|
|
b, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return "", fmt.Errorf("%s %s: read answer: %w", method, path, err)
|
|
}
|
|
return string(b), nil
|
|
}
|
|
|
|
func (s *idleTimeoutTestServer) do(t *testing.T, c *http.Client, method, path string, body io.Reader) string {
|
|
t.Helper()
|
|
got, err := s.send(c, method, path, body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return got
|
|
}
|
|
|
|
func (s *idleTimeoutTestServer) rawConn(t *testing.T) net.Conn {
|
|
t.Helper()
|
|
c, err := net.Dial("tcp", s.listener.Addr().String())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { c.Close() })
|
|
return c
|
|
}
|
|
|
|
func TestQuietHandlerKeepsItsContext(t *testing.T) {
|
|
for _, mode := range []string{"http", "https"} {
|
|
t.Run(mode, func(t *testing.T) {
|
|
s := newIdleTimeoutTestServer(t, mode, true)
|
|
c := s.client()
|
|
if got := s.do(t, c, http.MethodGet, "/quiet", nil); got != "ok" {
|
|
t.Fatalf("quiet request: context %s", got)
|
|
}
|
|
if got := s.do(t, c, http.MethodGet, "/ctx", nil); got != "ok" {
|
|
t.Fatalf("next request on the connection: context %s", got)
|
|
}
|
|
if n := s.listener.accepted.Load(); n != 1 {
|
|
t.Fatalf("requests used %d connections, want 1", n)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestQuietHandlerWithoutPauseLosesItsContext shows what the pause prevents:
|
|
// the idle timeout ends net/http's background read, which cancels the
|
|
// context of the quiet request and of the next request on the connection.
|
|
func TestQuietHandlerWithoutPauseLosesItsContext(t *testing.T) {
|
|
s := newIdleTimeoutTestServer(t, "http", false)
|
|
c := s.client()
|
|
if got := s.do(t, c, http.MethodGet, "/quiet", nil); got != context.Canceled.Error() {
|
|
t.Fatalf("quiet request: context %s, want %v", got, context.Canceled)
|
|
}
|
|
if got := s.do(t, c, http.MethodGet, "/ctx", nil); got != context.Canceled.Error() {
|
|
t.Fatalf("next request on the connection: context %s, want %v", got, context.Canceled)
|
|
}
|
|
if n := s.listener.accepted.Load(); n != 1 {
|
|
t.Fatalf("requests used %d connections, want 1", n)
|
|
}
|
|
}
|
|
|
|
func TestQuietHandlerAfterItsBodyKeepsItsContext(t *testing.T) {
|
|
for _, mode := range []string{"http", "https"} {
|
|
t.Run(mode, func(t *testing.T) {
|
|
s := newIdleTimeoutTestServer(t, mode, true)
|
|
c := s.client()
|
|
body := bytes.Repeat([]byte("x"), 1<<20)
|
|
if got, want := s.do(t, c, http.MethodPost, "/upload", bytes.NewReader(body)), fmt.Sprintf("ok %d", len(body)); got != want {
|
|
t.Fatalf("upload: %q, want %q", got, want)
|
|
}
|
|
if got := s.do(t, c, http.MethodGet, "/ctx", nil); got != "ok" {
|
|
t.Fatalf("next request on the connection: context %s", got)
|
|
}
|
|
if n := s.listener.accepted.Load(); n != 1 {
|
|
t.Fatalf("requests used %d connections, want 1", n)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestStalledBodyStillTimesOut checks that a client that stops sending its
|
|
// body is still cut off by the idle timeout.
|
|
func TestStalledBodyStillTimesOut(t *testing.T) {
|
|
s := newIdleTimeoutTestServer(t, "http", true)
|
|
c := s.rawConn(t)
|
|
if _, err := io.WriteString(c, "POST /upload HTTP/1.1\r\nHost: x\r\nContent-Length: 100\r\n\r\n0123456789"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
select {
|
|
case err := <-s.bodyErr:
|
|
if !errors.Is(err, os.ErrDeadlineExceeded) {
|
|
t.Fatalf("body read failed with %v, want %v", err, os.ErrDeadlineExceeded)
|
|
}
|
|
case <-time.After(10 * testIdleTimeout):
|
|
t.Fatal("body read still waiting on a stalled client")
|
|
}
|
|
}
|
|
|
|
// TestIdleConnectionStillTimesOut checks that a kept-alive connection with
|
|
// no request on it is still closed after the idle timeout.
|
|
func TestIdleConnectionStillTimesOut(t *testing.T) {
|
|
s := newIdleTimeoutTestServer(t, "http", true)
|
|
c := s.rawConn(t)
|
|
if _, err := io.WriteString(c, "GET /ctx HTTP/1.1\r\nHost: x\r\n\r\n"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
br := bufio.NewReader(c)
|
|
resp, err := http.ReadResponse(br, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, _ := io.ReadAll(resp.Body)
|
|
resp.Body.Close()
|
|
if string(b) != "ok" {
|
|
t.Fatalf("answer %q", b)
|
|
}
|
|
start := time.Now()
|
|
c.SetReadDeadline(time.Now().Add(10 * testIdleTimeout))
|
|
if _, err := br.ReadByte(); err != io.EOF {
|
|
t.Fatalf("idle connection: read got %v, want the server to close it", err)
|
|
}
|
|
if waited := time.Since(start); waited < testIdleTimeout/2 {
|
|
t.Fatalf("server closed the idle connection after %v, before the idle timeout", waited)
|
|
}
|
|
}
|
|
|
|
// TestOnlyHTTP1RequestsArePaused checks the scope of the pause: an HTTP/1.1
|
|
// request holds it while its handler runs, with or without a body, and an
|
|
// HTTP/2 request leaves the connection's timeout alone.
|
|
func TestOnlyHTTP1RequestsArePaused(t *testing.T) {
|
|
for mode, want := range map[string]string{"http": "1", "https": "1", "h2": "0"} {
|
|
t.Run(mode, func(t *testing.T) {
|
|
s := newIdleTimeoutTestServer(t, mode, true)
|
|
c := s.client()
|
|
for _, body := range []string{"", "x"} {
|
|
method := http.MethodGet
|
|
if body != "" {
|
|
method = http.MethodPost
|
|
}
|
|
if got := s.do(t, c, method, "/handling", strings.NewReader(body)); got != want {
|
|
t.Fatalf("%s with body %q: %s requests paused, want %s", method, body, got, want)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestStalledDownloadStillTimesOut checks that a client that stops reading
|
|
// an answer is still cut off by the idle timeout: over HTTP/1.1 the write
|
|
// waits on the socket, over HTTP/2 on flow-control credit, which only the
|
|
// frame reader's deadline ends. Over TLS, closing the connection after the
|
|
// failed write can take up to the 5 s crypto/tls gives its close_notify.
|
|
func TestStalledDownloadStillTimesOut(t *testing.T) {
|
|
for _, mode := range []string{"http", "https", "h2"} {
|
|
t.Run(mode, func(t *testing.T) {
|
|
s := newIdleTimeoutTestServer(t, mode, true)
|
|
c := s.client()
|
|
c.Timeout = 0 // the client must not give up first
|
|
resp, err := c.Get(s.Server.URL + "/download")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
select {
|
|
case err := <-s.downloadErr:
|
|
if err == nil {
|
|
t.Fatal("download finished although the client read none of it")
|
|
}
|
|
case <-time.After(8 * time.Second):
|
|
t.Fatal("download still waiting on a client that stopped reading")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestClientHangUpStillCancelsTheContext checks that the background read
|
|
// still sees a client close its connection while the read is paused.
|
|
func TestClientHangUpStillCancelsTheContext(t *testing.T) {
|
|
s := newIdleTimeoutTestServer(t, "http", true)
|
|
c := s.rawConn(t)
|
|
if _, err := io.WriteString(c, "GET /wait HTTP/1.1\r\nHost: x\r\n\r\n"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
time.Sleep(2 * testIdleTimeout)
|
|
c.Close()
|
|
select {
|
|
case got := <-s.hungUp:
|
|
if got != context.Canceled.Error() {
|
|
t.Fatalf("context %s after the client hung up, want %v", got, context.Canceled)
|
|
}
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("handler did not see the client hang up")
|
|
}
|
|
}
|
|
|
|
// TestPauseUnderConcurrentRequests runs quiet requests, uploads and quick
|
|
// requests at once over several connections; run it with -race. A request
|
|
// can wait for its turn on a connection, so the idle timeout here is 1 s:
|
|
// a slow run must not find the connection closed between two requests.
|
|
func TestPauseUnderConcurrentRequests(t *testing.T) {
|
|
for _, mode := range []string{"http", "https"} {
|
|
t.Run(mode, func(t *testing.T) {
|
|
s := newIdleTimeoutTestServerWith(t, mode, true, time.Second, 1500*time.Millisecond)
|
|
var clients []*http.Client
|
|
for i := 0; i < 4; i++ {
|
|
clients = append(clients, s.client())
|
|
}
|
|
body := bytes.Repeat([]byte("y"), 64<<10)
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < 8; i++ {
|
|
wg.Add(1)
|
|
go func(i int) {
|
|
defer wg.Done()
|
|
c := clients[i%len(clients)]
|
|
for j := 0; j < 2; j++ {
|
|
var got, want string
|
|
var err error
|
|
switch (i + j) % 3 {
|
|
case 0:
|
|
want = "ok"
|
|
got, err = s.send(c, http.MethodGet, "/quiet", nil)
|
|
case 1:
|
|
want = fmt.Sprintf("ok %d", len(body))
|
|
got, err = s.send(c, http.MethodPost, "/upload", bytes.NewReader(body))
|
|
default:
|
|
want = "ok"
|
|
got, err = s.send(c, http.MethodGet, "/ctx", nil)
|
|
}
|
|
if err != nil || got != want {
|
|
t.Errorf("request %d.%d: %q (%v), want %q", i, j, got, err, want)
|
|
}
|
|
}
|
|
}(i)
|
|
}
|
|
wg.Wait()
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestPauseHoldsAWaitingRead checks a read that is already waiting with the
|
|
// activity deadline, as net/http's background read is when the handler
|
|
// begins: the pause lifts its deadline, and its end puts the read back
|
|
// under the timeout.
|
|
func TestPauseHoldsAWaitingRead(t *testing.T) {
|
|
a, b := net.Pipe()
|
|
defer a.Close()
|
|
defer b.Close()
|
|
c := &Conn{Conn: a, Timeout: testIdleTimeout}
|
|
readErr := make(chan error, 1)
|
|
go func() {
|
|
_, err := c.Read(make([]byte, 1))
|
|
readErr <- err
|
|
}()
|
|
time.Sleep(testIdleTimeout / 4)
|
|
p := &idleTimeoutPause{conn: c}
|
|
p.begin()
|
|
select {
|
|
case err := <-readErr:
|
|
t.Fatalf("read ended while the pause held it: %v", err)
|
|
case <-time.After(3 * testIdleTimeout):
|
|
}
|
|
p.end()
|
|
select {
|
|
case err := <-readErr:
|
|
if !errors.Is(err, os.ErrDeadlineExceeded) {
|
|
t.Fatalf("read ended with %v, want %v", err, os.ErrDeadlineExceeded)
|
|
}
|
|
case <-time.After(10 * testIdleTimeout):
|
|
t.Fatal("read still waiting after the pause ended")
|
|
}
|
|
}
|
|
|
|
// TestConnPauseRacesWithReadsAndWrites exercises pauseIdleTimeout and
|
|
// resumeIdleTimeout against concurrent reads and writes of one Conn; run it
|
|
// with -race.
|
|
func TestConnPauseRacesWithReadsAndWrites(t *testing.T) {
|
|
a, b := net.Pipe()
|
|
defer a.Close()
|
|
defer b.Close()
|
|
c := &Conn{Conn: a, Timeout: time.Second}
|
|
go io.Copy(io.Discard, b)
|
|
go func() {
|
|
for i := 0; i < 2000; i++ {
|
|
if _, err := b.Write([]byte{1}); err != nil {
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
var wg sync.WaitGroup
|
|
wg.Add(3)
|
|
go func() {
|
|
defer wg.Done()
|
|
buf := make([]byte, 16)
|
|
for read := 0; read < 2000; {
|
|
n, err := c.Read(buf)
|
|
if err != nil {
|
|
t.Errorf("read: %v", err)
|
|
return
|
|
}
|
|
read += n
|
|
}
|
|
}()
|
|
go func() {
|
|
defer wg.Done()
|
|
for i := 0; i < 2000; i++ {
|
|
if _, err := c.Write([]byte{2}); err != nil {
|
|
t.Errorf("write: %v", err)
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
go func() {
|
|
defer wg.Done()
|
|
for i := 0; i < 2000; i++ {
|
|
p := &idleTimeoutPause{conn: c}
|
|
p.begin()
|
|
p.end()
|
|
}
|
|
}()
|
|
wg.Wait()
|
|
c.mu.Lock()
|
|
handling := c.handling
|
|
c.mu.Unlock()
|
|
if handling != 0 {
|
|
t.Fatalf("handling = %d after every pause ended", handling)
|
|
}
|
|
}
|
|
|
|
// TestPauseEndsOnceAndNotAfterTheHandler checks the pause bookkeeping: a
|
|
// body that ends after its handler returned (read by a goroutine the
|
|
// handler left behind) does not leave the connection paused.
|
|
func TestPauseEndsOnceAndNotAfterTheHandler(t *testing.T) {
|
|
a, b := net.Pipe()
|
|
defer a.Close()
|
|
defer b.Close()
|
|
c := &Conn{Conn: a, Timeout: time.Second}
|
|
p := &idleTimeoutPause{conn: c}
|
|
body := &pauseAtBodyEnd{ReadCloser: io.NopCloser(strings.NewReader("")), pause: p}
|
|
p.begin()
|
|
p.begin()
|
|
p.end()
|
|
p.end()
|
|
if _, err := body.Read(make([]byte, 1)); err != io.EOF {
|
|
t.Fatalf("read %v, want EOF", err)
|
|
}
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
if c.handling != 0 {
|
|
t.Fatalf("handling = %d, want 0", c.handling)
|
|
}
|
|
}
|