fix some test hangs (#4785)
The default http.Client uses infinite timeouts, so if TestHTTPAPI_MethodNotAllowed_OSS experienced anything going wrong about setup it could hang forever. Switching to hard coding various http.Client timeouts to non-infinite values at least bounds the failure time.
This commit is contained in:
parent
e3b4fafe36
commit
f15bce9f20
|
@ -2,10 +2,12 @@ package agent
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/consul/logger"
|
||||
)
|
||||
|
@ -43,6 +45,18 @@ func includePathInTest(path string) bool {
|
|||
return !ignored
|
||||
}
|
||||
|
||||
func newHttpClient(timeout time.Duration) *http.Client {
|
||||
return &http.Client{
|
||||
Timeout: timeout,
|
||||
Transport: &http.Transport{
|
||||
Dial: (&net.Dialer{
|
||||
Timeout: timeout,
|
||||
}).Dial,
|
||||
TLSHandshakeTimeout: timeout,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTTPAPI_MethodNotAllowed_OSS(t *testing.T) {
|
||||
|
||||
a := NewTestAgent(t.Name(), `acl_datacenter = "dc1"`)
|
||||
|
@ -50,10 +64,20 @@ func TestHTTPAPI_MethodNotAllowed_OSS(t *testing.T) {
|
|||
defer a.Shutdown()
|
||||
|
||||
all := []string{"GET", "PUT", "POST", "DELETE", "HEAD", "OPTIONS"}
|
||||
client := http.Client{}
|
||||
const testTimeout = 10 * time.Second
|
||||
|
||||
fastClient := newHttpClient(10 * time.Second)
|
||||
slowClient := newHttpClient(30 * time.Second)
|
||||
|
||||
testMethodNotAllowed := func(method string, path string, allowedMethods []string) {
|
||||
t.Run(method+" "+path, func(t *testing.T) {
|
||||
client := fastClient
|
||||
if path == "/v1/agent/leave" {
|
||||
// there are actual sleeps in this code that should take longer
|
||||
client = slowClient
|
||||
t.Logf("Using slow http client for leave tests")
|
||||
}
|
||||
|
||||
uri := fmt.Sprintf("http://%s%s", a.HTTPAddr(), path)
|
||||
req, _ := http.NewRequest(method, uri, nil)
|
||||
resp, err := client.Do(req)
|
||||
|
|
Loading…
Reference in New Issue