fix hard coded port usage

This commit is contained in:
Lucia 2025-11-02 17:54:43 +13:00
parent 18a902799e
commit cb9b031a0b

View file

@ -20,6 +20,7 @@ import (
"crypto/rand" "crypto/rand"
"math/big" "math/big"
"path/filepath" "path/filepath"
"regexp"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
@ -34,6 +35,11 @@ const (
httpAPIs = "eth:1.0 net:1.0 rpc:1.0 web3:1.0" httpAPIs = "eth:1.0 net:1.0 rpc:1.0 web3:1.0"
) )
var (
httpEndpointRegexp = regexp.MustCompile(`HTTP server started\s+endpoint=([^\s]+)`)
wsEndpointRegexp = regexp.MustCompile(`WebSocket enabled\s+url=(ws://[^\s]+)`)
)
// spawns geth with the given command line args, using a set of flags to minimise // spawns geth with the given command line args, using a set of flags to minimise
// memory and disk IO. If the args don't set --datadir, the // memory and disk IO. If the args don't set --datadir, the
// child g gets a temporary data directory. // child g gets a temporary data directory.
@ -84,8 +90,8 @@ To exit, press ctrl-d or type exit
func TestAttachWelcome(t *testing.T) { func TestAttachWelcome(t *testing.T) {
var ( var (
ipc string ipc string
httpPort string httpURL string
wsPort string wsURL string
) )
// Configure the instance for IPC attachment // Configure the instance for IPC attachment
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
@ -94,26 +100,26 @@ func TestAttachWelcome(t *testing.T) {
ipc = filepath.Join(t.TempDir(), "geth.ipc") ipc = filepath.Join(t.TempDir(), "geth.ipc")
} }
// And HTTP + WS attachment // And HTTP + WS attachment
p := trulyRandInt(1024, 65533) // Yeah, sometimes this will fail, sorry :P
httpPort = strconv.Itoa(p)
wsPort = strconv.Itoa(p + 1)
geth := runMinimalGeth(t, "--miner.etherbase", "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182", geth := runMinimalGeth(t, "--miner.etherbase", "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182",
"--ipcpath", ipc, "--ipcpath", ipc,
"--http", "--http.port", httpPort, "--http", "--http.addr", "127.0.0.1", "--http.port", "0",
"--ws", "--ws.port", wsPort) "--ws", "--ws.addr", "127.0.0.1", "--ws.port", "0")
httpEndpoint := waitForLogMatch(t, geth, httpEndpointRegexp)
httpURL = "http://" + httpEndpoint
wsURL = waitForLogMatch(t, geth, wsEndpointRegexp)
t.Run("ipc", func(t *testing.T) { t.Run("ipc", func(t *testing.T) {
waitForEndpoint(t, ipc, 2*time.Minute) waitForEndpoint(t, ipc, 2*time.Minute)
testAttachWelcome(t, geth, "ipc:"+ipc, ipcAPIs) testAttachWelcome(t, geth, "ipc:"+ipc, ipcAPIs)
}) })
t.Run("http", func(t *testing.T) { t.Run("http", func(t *testing.T) {
endpoint := "http://127.0.0.1:" + httpPort waitForEndpoint(t, httpURL, 2*time.Minute)
waitForEndpoint(t, endpoint, 2*time.Minute) testAttachWelcome(t, geth, httpURL, httpAPIs)
testAttachWelcome(t, geth, endpoint, httpAPIs)
}) })
t.Run("ws", func(t *testing.T) { t.Run("ws", func(t *testing.T) {
endpoint := "ws://127.0.0.1:" + wsPort waitForEndpoint(t, wsURL, 2*time.Minute)
waitForEndpoint(t, endpoint, 2*time.Minute) testAttachWelcome(t, geth, wsURL, httpAPIs)
testAttachWelcome(t, geth, endpoint, httpAPIs)
}) })
geth.Kill() geth.Kill()
} }
@ -151,8 +157,21 @@ To exit, press ctrl-d or type exit
attach.ExpectExit() attach.ExpectExit()
} }
func waitForLogMatch(t *testing.T, geth *testgeth, re *regexp.Regexp) string {
t.Helper()
deadline := time.Now().Add(30 * time.Second)
for time.Now().Before(deadline) {
if matches := re.FindStringSubmatch(geth.StderrText()); len(matches) > 1 {
return matches[1]
}
time.Sleep(100 * time.Millisecond)
}
t.Fatalf("timeout waiting for log %q\nstderr:\n%s", re.String(), geth.StderrText())
return ""
}
// trulyRandInt generates a crypto random integer used by the console tests to // trulyRandInt generates a crypto random integer used by the console tests to
// not clash network ports with other tests running concurrently. // avoid name collisions (pipe identifiers, ports, etc.) when running concurrently.
func trulyRandInt(lo, hi int) int { func trulyRandInt(lo, hi int) int {
num, _ := rand.Int(rand.Reader, big.NewInt(int64(hi-lo))) num, _ := rand.Int(rand.Reader, big.NewInt(int64(hi-lo)))
return int(num.Int64()) + lo return int(num.Int64()) + lo