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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/consul/logger"
|
"github.com/hashicorp/consul/logger"
|
||||||
)
|
)
|
||||||
|
@ -43,6 +45,18 @@ func includePathInTest(path string) bool {
|
||||||
return !ignored
|
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) {
|
func TestHTTPAPI_MethodNotAllowed_OSS(t *testing.T) {
|
||||||
|
|
||||||
a := NewTestAgent(t.Name(), `acl_datacenter = "dc1"`)
|
a := NewTestAgent(t.Name(), `acl_datacenter = "dc1"`)
|
||||||
|
@ -50,10 +64,20 @@ func TestHTTPAPI_MethodNotAllowed_OSS(t *testing.T) {
|
||||||
defer a.Shutdown()
|
defer a.Shutdown()
|
||||||
|
|
||||||
all := []string{"GET", "PUT", "POST", "DELETE", "HEAD", "OPTIONS"}
|
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) {
|
testMethodNotAllowed := func(method string, path string, allowedMethods []string) {
|
||||||
t.Run(method+" "+path, func(t *testing.T) {
|
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)
|
uri := fmt.Sprintf("http://%s%s", a.HTTPAddr(), path)
|
||||||
req, _ := http.NewRequest(method, uri, nil)
|
req, _ := http.NewRequest(method, uri, nil)
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
|
|
Loading…
Reference in New Issue