Skip to content

Commit 7ca971f

Browse files
committed
Adds a unit test for plugin request timeout
Signed-off-by: Brian Goff <[email protected]>
1 parent 8830ef8 commit 7ca971f

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

pkg/plugins/client_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package plugins // import "github.com/docker/docker/pkg/plugins"
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"io"
78
"net/http"
@@ -13,7 +14,9 @@ import (
1314

1415
"github.com/docker/docker/pkg/plugins/transport"
1516
"github.com/docker/go-connections/tlsconfig"
17+
"github.com/pkg/errors"
1618
"github.com/stretchr/testify/assert"
19+
"github.com/stretchr/testify/require"
1720
)
1821

1922
var (
@@ -232,3 +235,43 @@ func TestClientSendFile(t *testing.T) {
232235
}
233236
assert.Equal(t, m, output)
234237
}
238+
239+
func TestClientWithRequestTimeout(t *testing.T) {
240+
timeout := 1 * time.Millisecond
241+
testHandler := func(w http.ResponseWriter, r *http.Request) {
242+
time.Sleep(timeout + 1*time.Millisecond)
243+
w.WriteHeader(http.StatusOK)
244+
}
245+
246+
srv := httptest.NewServer(http.HandlerFunc(testHandler))
247+
defer srv.Close()
248+
249+
client := &Client{http: srv.Client(), requestFactory: &testRequestWrapper{srv}}
250+
_, err := client.callWithRetry("/Plugin.Hello", nil, false, WithRequestTimeout(timeout))
251+
require.Error(t, err, "expected error")
252+
253+
err = errors.Cause(err)
254+
255+
switch e := err.(type) {
256+
case *url.Error:
257+
err = e.Err
258+
}
259+
require.Equal(t, context.DeadlineExceeded, err)
260+
}
261+
262+
type testRequestWrapper struct {
263+
*httptest.Server
264+
}
265+
266+
func (w *testRequestWrapper) NewRequest(path string, data io.Reader) (*http.Request, error) {
267+
req, err := http.NewRequest("POST", path, data)
268+
if err != nil {
269+
return nil, err
270+
}
271+
u, err := url.Parse(w.Server.URL)
272+
if err != nil {
273+
return nil, err
274+
}
275+
req.URL = u
276+
return req, nil
277+
}

0 commit comments

Comments
 (0)