Add configurable timeout to fission fn test (#1091)

Currently the implementation of the CLI (or any Fission component) specifies any timeout on
the fission fn test functionality. If a user function or environment does not specify any
timeout the command will stall. Nor is there any parameter to set a timeout if the user
wanted to.

This change sets the sensible default for the timeout to 30 seconds, to avoid new
users having to figure this out the hard way. The alternative could be to set the timeout to
0 (no timeout).

Future work: extend this timeout parameter to a global flag, and support
timeouts on all CLI commands.
This commit is contained in:
Erwin van Eyk
2019-04-24 13:08:28 +02:00
committed by GitHub
parent d6c89d5449
commit 2de8e63596
2 changed files with 16 additions and 7 deletions
+11 -6
View File
@@ -790,9 +790,16 @@ func fnTest(c *cli.Context) error {
functionUrl.RawQuery = query.Encode()
}
ctx := context.Background()
if deadline := c.Duration("timeout"); deadline > 0 {
var closeCtx func()
ctx, closeCtx = context.WithTimeout(ctx, deadline)
defer closeCtx()
}
headers := c.StringSlice("header")
resp := httpRequest(c.String("method"), functionUrl.String(), c.String("body"), headers)
resp := doHTTPRequest(ctx, c.String("method"), functionUrl.String(), c.String("body"), headers)
if resp.StatusCode < 400 {
body, err := ioutil.ReadAll(resp.Body)
util.CheckErr(err, "Function test")
@@ -813,9 +820,9 @@ func fnTest(c *cli.Context) error {
return nil
}
func httpRequest(method, url, body string, headers []string) *http.Response {
func doHTTPRequest(ctx context.Context, method, url, body string, headers []string) *http.Response {
if method == "" {
method = "GET"
method = http.MethodGet
}
if method != http.MethodGet &&
@@ -836,9 +843,7 @@ func httpRequest(method, url, body string, headers []string) *http.Response {
}
req.Header.Set(headerKeyValue[0], headerKeyValue[1])
}
client := &http.Client{}
resp, err := client.Do(req)
resp, err := http.DefaultClient.Do(req.WithContext(ctx))
util.CheckErr(err, "execute HTTP request")
return resp