fix racy test in pkg/plugins

Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
This commit is contained in:
Akihiro Suda 2016-09-13 02:38:41 +00:00
parent 6fafd07282
commit 4bc908ec89

View file

@ -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) {