Skip to content

Commit 728f65f

Browse files
committed
testing timeout from samalba#77
1 parent d797a34 commit 728f65f

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

dockerclient_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package dockerclient
33
import (
44
"bytes"
55
"fmt"
6+
"net/url"
67
"reflect"
78
"strings"
89
"testing"
10+
"time"
911

1012
"github.com/docker/docker/pkg/stdcopy"
1113
)
@@ -28,6 +30,22 @@ func testDockerClient(t *testing.T) *DockerClient {
2830
return client
2931
}
3032

33+
func TestDockerClientTimeout(t *testing.T) {
34+
client, err := NewDockerClientTimeout(testHTTPServer.URL, nil, 3*time.Second)
35+
if err != nil {
36+
t.Fatal("Cannot init the docker client")
37+
}
38+
hangingURL := fmt.Sprintf("%s/%s/hangFor", testHTTPServer.URL, APIVersion)
39+
_, err = client.HTTPClient.PostForm(hangingURL, url.Values{"numSeconds": {"4"}})
40+
if err == nil {
41+
t.Fatal("Expected failure from POST request")
42+
}
43+
_, err = client.HTTPClient.PostForm(hangingURL, url.Values{"numSeconds": {"1"}})
44+
if err != nil {
45+
t.Fatalf("Got error from POST request: %q", err)
46+
}
47+
}
48+
3149
func TestInfo(t *testing.T) {
3250
client := testDockerClient(t)
3351
info, err := client.Info()

engine_mock_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ func init() {
2929
r.HandleFunc(baseURL+"/containers/{id}/logs", handleContainerLogs).Methods("GET")
3030
r.HandleFunc(baseURL+"/containers/{id}/kill", handleContainerKill).Methods("POST")
3131
r.HandleFunc(baseURL+"/images/create", handleImagePull).Methods("POST")
32+
// this is used to test timeout functionality
33+
r.HandleFunc(baseURL+"/hangFor", handleHang).Methods("POST")
34+
3235
testHTTPServer = httptest.NewServer(handlerAccessLog(r))
3336
}
3437

@@ -208,3 +211,17 @@ func handlerGetContainers(w http.ResponseWriter, r *http.Request) {
208211
}
209212
w.Write([]byte(body))
210213
}
214+
215+
func handleHang(w http.ResponseWriter, r *http.Request) {
216+
if err := r.ParseForm(); err != nil {
217+
panic(err)
218+
}
219+
numSecondsString := r.FormValue("numSeconds")
220+
numSeconds, err := strconv.Atoi(numSecondsString)
221+
if err != nil {
222+
panic(err)
223+
}
224+
225+
time.Sleep(time.Duration(numSeconds) * time.Second)
226+
fmt.Printf("done sleeping! %d", numSeconds)
227+
}

0 commit comments

Comments
 (0)