From 4bc908ec8910468711960a4bcd4f3ec8dce909fb Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Tue, 13 Sep 2016 02:38:41 +0000 Subject: [PATCH] fix racy test in pkg/plugins Signed-off-by: Akihiro Suda --- pkg/plugins/client_test.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/pkg/plugins/client_test.go b/pkg/plugins/client_test.go index 952d086984..be71a371da 100644 --- a/pkg/plugins/client_test.go +++ b/pkg/plugins/client_test.go @@ -31,24 +31,30 @@ func teardownRemotePluginServer() { } } -func TestHttpTimeout(t *testing.T) { +func testHTTPTimeout(t *testing.T, timeout, epsilon time.Duration) { addr := setupRemotePluginServer() defer teardownRemotePluginServer() - stop := false // we need this variable to stop the http server + stop := make(chan struct{}) // 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) - } + <-stop }) c, _ := NewClient(addr, &tlsconfig.Options{InsecureSkipVerify: true}) + c.http.Timeout = timeout + begin := time.Now() _, err := c.callWithRetry("hang", nil, false) - stop = true + close(stop) if err == nil || !strings.Contains(err.Error(), "request canceled") { t.Fatalf("The request should be canceled %v", err) } + elapsed := time.Now().Sub(begin) + if elapsed < timeout || elapsed > timeout+epsilon { + t.Fatalf("elapsed time: got %v, expected %v (epsilon=%v)", + elapsed, timeout, epsilon) + } +} + +func TestHTTPTimeout(t *testing.T) { + testHTTPTimeout(t, 5*time.Second, 1*time.Second) } func TestFailedConnection(t *testing.T) {