Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Adding timeout to Snap REST client
Browse files Browse the repository at this point in the history
  • Loading branch information
iwankgb committed Jan 25, 2017
1 parent f36d1aa commit 13f1b6d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions mgmt/rest/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ func Username(u string) metaOp {
}
}

//Timeout is an option that can be provided to the func client.New in order to set HTTP connection timeout.
func Timeout(t time.Duration) metaOp {
return func(c *Client) {
c.http.Timeout = t
}
}

var (
secureTransport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: false},
Expand Down
17 changes: 17 additions & 0 deletions mgmt/rest/client/client_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package client
import (
"fmt"
"io/ioutil"
"net/http"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -627,4 +628,20 @@ func TestSnapClient(t *testing.T) {
So(err, ShouldNotBeNil)
So(c, ShouldBeNil)
})

go http.ListenAndServe("127.0.0.1:65000", timeoutHandler{})
c, err = New("http://127.0.0.1:65000", "", true, Timeout(time.Second))
Convey("Client should timeout", t, func() {
So(err, ShouldBeNil)
r := c.GetTasks()
So(r.Err, ShouldNotBeNil)
})
}

type timeoutHandler struct{}

//ServeHTTP implements http.Handler interface
func (th timeoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
time.Sleep(3 * time.Second)
w.Write([]byte("Hello!"))
}

0 comments on commit 13f1b6d

Please sign in to comment.