aboutsummaryrefslogtreecommitdiff
path: root/client_test.go
diff options
context:
space:
mode:
authorGravatar Igor Menshenin <38577106+iv-menshenin@users.noreply.github.com> 2023-03-03 23:25:39 +0300
committerGravatar GitHub <noreply@github.com> 2023-03-03 21:25:39 +0100
commit74a050705b5a66541add862084aacf292bf6e7ce (patch)
treeade4ad821b452fcda70691580922a1e494cab54a /client_test.go
parentBump golang.org/x/crypto from 0.0.0-20220214200702-86341886e292 to 0.1.0 (#1508) (diff)
downloadfasthttp-74a050705b5a66541add862084aacf292bf6e7ce.tar.gz
fasthttp-74a050705b5a66541add862084aacf292bf6e7ce.tar.bz2
fasthttp-74a050705b5a66541add862084aacf292bf6e7ce.zip
Immediately return ErrTimeout if deadline is already reached. (#1497)
* fix: Immediately return ErrTimeout if deadline is already reached. * test: Added tests for negative timeout --------- Co-authored-by: Igor Menshenin <igor@native.rent>
Diffstat (limited to 'client_test.go')
-rw-r--r--client_test.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/client_test.go b/client_test.go
index aa53461..cf98531 100644
--- a/client_test.go
+++ b/client_test.go
@@ -120,6 +120,32 @@ func testPipelineClientSetUserAgent(t *testing.T, timeout time.Duration) {
}
}
+func TestHostClientNegativeTimeout(t *testing.T) {
+ t.Parallel()
+
+ ln := fasthttputil.NewInmemoryListener()
+ s := &Server{
+ Handler: func(ctx *RequestCtx) {
+ },
+ }
+ go s.Serve(ln) //nolint:errcheck
+ c := &HostClient{
+ Dial: func(addr string) (net.Conn, error) {
+ return ln.Dial()
+ },
+ }
+ req := AcquireRequest()
+ req.Header.SetMethod(MethodGet)
+ req.SetRequestURI("http://example.com")
+ if err := c.DoTimeout(req, nil, -time.Second); err != ErrTimeout {
+ t.Fatalf("expected ErrTimeout error got: %+v", err)
+ }
+ if err := c.DoDeadline(req, nil, time.Now().Add(-time.Second)); err != ErrTimeout {
+ t.Fatalf("expected ErrTimeout error got: %+v", err)
+ }
+ ln.Close()
+}
+
func TestPipelineClientIssue832(t *testing.T) {
t.Parallel()
@@ -306,6 +332,32 @@ func TestClientNilResp(t *testing.T) {
ln.Close()
}
+func TestClientNegativeTimeout(t *testing.T) {
+ t.Parallel()
+
+ ln := fasthttputil.NewInmemoryListener()
+ s := &Server{
+ Handler: func(ctx *RequestCtx) {
+ },
+ }
+ go s.Serve(ln) //nolint:errcheck
+ c := &Client{
+ Dial: func(addr string) (net.Conn, error) {
+ return ln.Dial()
+ },
+ }
+ req := AcquireRequest()
+ req.Header.SetMethod(MethodGet)
+ req.SetRequestURI("http://example.com")
+ if err := c.DoTimeout(req, nil, -time.Second); err != ErrTimeout {
+ t.Fatalf("expected ErrTimeout error got: %+v", err)
+ }
+ if err := c.DoDeadline(req, nil, time.Now().Add(-time.Second)); err != ErrTimeout {
+ t.Fatalf("expected ErrTimeout error got: %+v", err)
+ }
+ ln.Close()
+}
+
func TestPipelineClientNilResp(t *testing.T) {
t.Parallel()