mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #25794 from chenchun/timeout
Add default timeout to pkg/plugins/client
This commit is contained in:
commit
a4d1365bce
2 changed files with 24 additions and 1 deletions
|
@ -16,7 +16,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultTimeOut = 30
|
defaultTimeOut = 30
|
||||||
|
defaultHTTPTimeOut = 32 * time.Second
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewClient creates a new plugin client (http).
|
// NewClient creates a new plugin client (http).
|
||||||
|
@ -53,6 +54,7 @@ func NewClient(addr string, tlsConfig *tlsconfig.Options) (*Client, error) {
|
||||||
func NewClientWithTransport(tr transport.Transport) *Client {
|
func NewClientWithTransport(tr transport.Transport) *Client {
|
||||||
return &Client{
|
return &Client{
|
||||||
http: &http.Client{
|
http: &http.Client{
|
||||||
|
Timeout: defaultHTTPTimeOut,
|
||||||
Transport: tr,
|
Transport: tr,
|
||||||
},
|
},
|
||||||
requestFactory: tr,
|
requestFactory: tr,
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -30,6 +31,26 @@ func teardownRemotePluginServer() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHttpTimeout(t *testing.T) {
|
||||||
|
addr := setupRemotePluginServer()
|
||||||
|
defer teardownRemotePluginServer()
|
||||||
|
stop := false // we need this variable to stop the http server
|
||||||
|
mux.HandleFunc("/hang", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
for {
|
||||||
|
if stop {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
c, _ := NewClient(addr, &tlsconfig.Options{InsecureSkipVerify: true})
|
||||||
|
_, err := c.callWithRetry("hang", nil, false)
|
||||||
|
stop = true
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "request canceled") {
|
||||||
|
t.Fatalf("The request should be canceled %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestFailedConnection(t *testing.T) {
|
func TestFailedConnection(t *testing.T) {
|
||||||
c, _ := NewClient("tcp://127.0.0.1:1", &tlsconfig.Options{InsecureSkipVerify: true})
|
c, _ := NewClient("tcp://127.0.0.1:1", &tlsconfig.Options{InsecureSkipVerify: true})
|
||||||
_, err := c.callWithRetry("Service.Method", nil, false)
|
_, err := c.callWithRetry("Service.Method", nil, false)
|
||||||
|
|
Loading…
Add table
Reference in a new issue