Skip to content

Commit

Permalink
Fixed timeout issues in http request workflow function
Browse files Browse the repository at this point in the history
  • Loading branch information
petergrlica committed Mar 28, 2022
1 parent 655ed4c commit 6620b6e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
13 changes: 7 additions & 6 deletions automation/automation/http_request_handler.go
Expand Up @@ -41,6 +41,13 @@ func (h httpRequestHandler) send(ctx context.Context, args *httpRequestSendArgs)
return nil, err
}

if args.Timeout > 0 {
var tfn context.CancelFunc
ctx, tfn = context.WithTimeout(ctx, args.Timeout)
req = req.WithContext(ctx)
defer tfn()
}

rsp, err = http.DefaultClient.Do(req)
if err != nil {
return
Expand Down Expand Up @@ -113,12 +120,6 @@ func (h httpRequestHandler) makeRequest(ctx context.Context, args *httpRequestSe
return nil, err
}

if args.Timeout > 0 {
var tfn context.CancelFunc
ctx, tfn = context.WithTimeout(ctx, args.Timeout)
defer tfn()
}

if args.hasParams {
purl, err := url.Parse(args.Url)
if err != nil {
Expand Down
21 changes: 20 additions & 1 deletion automation/automation/http_request_handler_test.go
Expand Up @@ -2,11 +2,14 @@ package automation

import (
"context"
"github.com/stretchr/testify/require"
"errors"
"io/ioutil"
"net/http"
"net/url"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestHttpRequestMaker(t *testing.T) {
Expand Down Expand Up @@ -35,6 +38,22 @@ func TestHttpRequestMaker(t *testing.T) {
r.Equal("http://localhost/test", req.URL.String())
})

t.Run("basic timeout", func(t *testing.T) {
var (
r = require.New(t)
ctx = context.Background()

_, err = httpRequestHandler{}.send(ctx, &httpRequestSendArgs{
Timeout: time.Nanosecond,
HeaderAuthBearer: "foo",
Url: "http://localhost/test",
Method: "GET",
})
)

r.True(errors.Is(err, context.DeadlineExceeded))
})

t.Run("post form", func(t *testing.T) {
var (
r = require.New(t)
Expand Down

0 comments on commit 6620b6e

Please sign in to comment.