Merge pull request #591 from docker/flaky-aci

Attempt to fix flaky ACI tests
This commit is contained in:
Chris Crone 2020-09-07 17:05:58 +02:00 committed by GitHub
commit 7545485f78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 2 deletions

View File

@ -198,7 +198,7 @@ func TestContainerRunVolume(t *testing.T) {
})
t.Run("http get", func(t *testing.T) {
r, err := http.Get(endpoint)
r, err := HTTPGetWithRetry(endpoint, 3)
assert.NilError(t, err)
assert.Equal(t, r.StatusCode, http.StatusOK)
b, err := ioutil.ReadAll(r.Body)
@ -434,7 +434,7 @@ func TestComposeUpUpdate(t *testing.T) {
assert.Assert(t, is.Len(containerInspect.Ports, 1))
endpoint := fmt.Sprintf("http://%s:%d", containerInspect.Ports[0].HostIP, containerInspect.Ports[0].HostPort)
r, err := http.Get(endpoint + "/words/noun")
r, err := HTTPGetWithRetry(endpoint+"/words/noun", 3)
assert.NilError(t, err)
assert.Equal(t, r.StatusCode, http.StatusOK)
b, err := ioutil.ReadAll(r.Body)

View File

@ -22,12 +22,14 @@ import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
@ -192,3 +194,21 @@ func ParseContainerInspect(stdout string) (*containers.Container, error) {
}
return &res, nil
}
// HTTPGetWithRetry performs an HTTP GET on an `endpoint`.
// In the case of an error it retries the same request after a 5 second sleep,
// returning the error if count of `tries` is reached
func HTTPGetWithRetry(endpoint string, tries int) (*http.Response, error) {
var (
r *http.Response
err error
)
for t := 0; t < tries; t++ {
r, err = http.Get(endpoint)
if err == nil || t == tries-1 {
break
}
time.Sleep(5 * time.Second)
}
return r, err
}