diff options
author | Erik Dubbelboer <erik@dubbelboer.com> | 2022-07-29 19:03:15 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-29 19:03:15 +0200 |
commit | a5f448fc970972ab47113971d898a22fb28fef52 (patch) | |
tree | 826fe1e91cdca5002f7db0bef4b8dfdcf55efddc /client_test.go | |
parent | Prevent overflow and panic on large HTTP responses (#1351) (diff) | |
download | fasthttp-a5f448fc970972ab47113971d898a22fb28fef52.tar.gz fasthttp-a5f448fc970972ab47113971d898a22fb28fef52.tar.bz2 fasthttp-a5f448fc970972ab47113971d898a22fb28fef52.zip |
Improve Client timeout (#1346)
Don't run requests in a separate Goroutine anymore. Instead use proper
conn deadlines to enforce timeouts.
- Also contains some linting fixes.
Diffstat (limited to 'client_test.go')
-rw-r--r-- | client_test.go | 44 |
1 files changed, 33 insertions, 11 deletions
diff --git a/client_test.go b/client_test.go index faf29d4..90092f4 100644 --- a/client_test.go +++ b/client_test.go @@ -181,7 +181,7 @@ func TestClientInvalidURI(t *testing.T) { ln := fasthttputil.NewInmemoryListener() requests := int64(0) s := &Server{ - Handler: func(ctx *RequestCtx) { + Handler: func(_ *RequestCtx) { atomic.AddInt64(&requests, 1) }, } @@ -636,7 +636,7 @@ func TestClientReadTimeout(t *testing.T) { timeout := false s := &Server{ - Handler: func(ctx *RequestCtx) { + Handler: func(_ *RequestCtx) { if timeout { time.Sleep(time.Second) } else { @@ -1191,7 +1191,7 @@ func TestHostClientPendingRequests(t *testing.T) { doneCh := make(chan struct{}) readyCh := make(chan struct{}, concurrency) s := &Server{ - Handler: func(ctx *RequestCtx) { + Handler: func(_ *RequestCtx) { readyCh <- struct{}{} <-doneCh }, @@ -1750,16 +1750,19 @@ func testClientGetTimeoutError(t *testing.T, c *Client, n int) { type readTimeoutConn struct { net.Conn - t time.Duration + t time.Duration + wc chan struct{} + rc chan struct{} } func (r *readTimeoutConn) Read(p []byte) (int, error) { - time.Sleep(r.t) - return 0, io.EOF + <-r.rc + return 0, os.ErrDeadlineExceeded } func (r *readTimeoutConn) Write(p []byte) (int, error) { - return len(p), nil + <-r.wc + return 0, os.ErrDeadlineExceeded } func (r *readTimeoutConn) Close() error { @@ -1774,12 +1777,30 @@ func (r *readTimeoutConn) RemoteAddr() net.Addr { return nil } +func (r *readTimeoutConn) SetReadDeadline(d time.Time) error { + r.rc = make(chan struct{}, 1) + go func() { + time.Sleep(time.Until(d)) + r.rc <- struct{}{} + }() + return nil +} + +func (r *readTimeoutConn) SetWriteDeadline(d time.Time) error { + r.wc = make(chan struct{}, 1) + go func() { + time.Sleep(time.Until(d)) + r.wc <- struct{}{} + }() + return nil +} + func TestClientNonIdempotentRetry(t *testing.T) { t.Parallel() dialsCount := 0 c := &Client{ - Dial: func(addr string) (net.Conn, error) { + Dial: func(_ string) (net.Conn, error) { dialsCount++ switch dialsCount { case 1, 2: @@ -1829,7 +1850,7 @@ func TestClientNonIdempotentRetry_BodyStream(t *testing.T) { dialsCount := 0 c := &Client{ - Dial: func(addr string) (net.Conn, error) { + Dial: func(_ string) (net.Conn, error) { dialsCount++ switch dialsCount { case 1, 2: @@ -1866,7 +1887,7 @@ func TestClientIdempotentRequest(t *testing.T) { dialsCount := 0 c := &Client{ - Dial: func(addr string) (net.Conn, error) { + Dial: func(_ string) (net.Conn, error) { dialsCount++ switch dialsCount { case 1: @@ -1922,7 +1943,7 @@ func TestClientRetryRequestWithCustomDecider(t *testing.T) { dialsCount := 0 c := &Client{ - Dial: func(addr string) (net.Conn, error) { + Dial: func(_ string) (net.Conn, error) { dialsCount++ switch dialsCount { case 1: @@ -2758,6 +2779,7 @@ func TestHostClientMaxConnWaitTimeoutWithEarlierDeadline(t *testing.T) { time.Sleep(sleep) ctx.WriteString("foo") //nolint:errcheck }, + Logger: &testLogger{}, // Don't print connection closed errors. } serverStopCh := make(chan struct{}) go func() { |