Merge pull request #26512 from AkihiroSuda/fix-pkg-plugins-racy-test

[test] fix racy test in pkg/plugins
This commit is contained in:
Aaron Lehmann 2016-09-15 20:11:03 -07:00 committed by GitHub
commit a1fc3852da
1 changed files with 15 additions and 9 deletions

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