testutil: pick dynamic metrics port to avoid bind conflicts in tests

This commit is contained in:
Chris Lu
2025-12-24 20:06:08 -08:00
parent d0d2b021d9
commit 7dfa361e6e

View File

@@ -2,6 +2,7 @@ package testutil
import (
"fmt"
"net"
"net/http"
"os"
"os/exec"
@@ -71,9 +72,20 @@ func StartServer(config ServerConfig) (*TestServer, error) {
"-volume.max=100",
fmt.Sprintf("-dir=%s", config.DataDir),
"-volume.preStopSeconds=1",
"-metricsPort=9324",
}
// choose a free metrics port to avoid collisions in parallel test runs
metricsPort := 0
if l, err := net.Listen("tcp", "127.0.0.1:0"); err == nil {
addr := l.Addr().(*net.TCPAddr)
metricsPort = addr.Port
l.Close()
} else {
metricsPort = 9324
}
args = append(args, fmt.Sprintf("-metricsPort=%d", metricsPort))
if config.S3Config != "" {
args = append(args, fmt.Sprintf("-s3.config=%s", config.S3Config))
}