Quellcode durchsuchen

fix racy test in pkg/plugins

Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
Akihiro Suda vor 8 Jahren
Ursprung
Commit
4bc908ec89
1 geänderte Dateien mit 15 neuen und 9 gelöschten Zeilen
  1. 15 9
      pkg/plugins/client_test.go

+ 15 - 9
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) {