1
0
mirror of https://github.com/rclone/rclone.git synced 2026-01-06 18:43:50 +00:00

tests: make test servers choose a random port to make more reliable

Tests have been randomly failing with messages like

    listen tcp 127.0.0.1:51778: bind: address already in use

Rework all the test servers so they choose a random free port on
startup and use that for the tests to avoid.
This commit is contained in:
Nick Craig-Wood
2019-05-01 11:16:22 +01:00
parent 8f89b03d7b
commit 9df322e889
5 changed files with 14 additions and 15 deletions

View File

@@ -3,7 +3,6 @@ package http
import (
"flag"
"io/ioutil"
"net"
"net/http"
"strings"
"testing"
@@ -21,11 +20,11 @@ import (
var (
updateGolden = flag.Bool("updategolden", false, "update golden files for regression test")
httpServer *server
testURL string
)
const (
testBindAddress = "localhost:51777"
testURL = "http://" + testBindAddress + "/"
testBindAddress = "localhost:0"
)
func startServer(t *testing.T, f fs.Fs) {
@@ -33,13 +32,14 @@ func startServer(t *testing.T, f fs.Fs) {
opt.ListenAddr = testBindAddress
httpServer = newServer(f, &opt)
assert.NoError(t, httpServer.Serve())
testURL = httpServer.Server.URL()
// try to connect to the test server
pause := time.Millisecond
for i := 0; i < 10; i++ {
conn, err := net.Dial("tcp", testBindAddress)
resp, err := http.Head(testURL)
if err == nil {
_ = conn.Close()
_ = resp.Body.Close()
return
}
// t.Logf("couldn't connect, sleeping for %v: %v", pause, err)