Skip to content

Commit b78154c

Browse files
committed
Make sure all ports are allocated at once to minimize clashes
1 parent dda2a98 commit b78154c

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

cmd/stellar-rpc/internal/integrationtest/infrastructure/test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -477,10 +477,11 @@ func (i *Test) spawnRPCDaemon() {
477477
// We need to dynamically allocate port numbers since tests run in parallel.
478478
// Unfortunately this isn't completely clash-free,
479479
// but there is no way to tell core to allocate the port dynamically
480-
i.testPorts.captiveCorePort = getFreeTCPPort(i.t)
480+
freePorts := getFreeTCPPorts(i.t, 3)
481+
i.testPorts.captiveCorePort = freePorts[0]
481482
if i.enableCoreHTTPQueryServer {
482-
i.testPorts.captiveCoreHTTPQueryPort = getFreeTCPPort(i.t)
483-
i.testPorts.captiveCoreHTTPPort = getFreeTCPPort(i.t)
483+
i.testPorts.captiveCoreHTTPQueryPort = freePorts[1]
484+
i.testPorts.captiveCoreHTTPPort = freePorts[2]
484485
}
485486
i.generateCaptiveCoreCfgForDaemon()
486487
rpcCfg := i.getRPConfigForDaemon()

cmd/stellar-rpc/internal/integrationtest/infrastructure/util.go

+13-9
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@ func GetCurrentDirectory() string {
1616
return filepath.Dir(currentFilename)
1717
}
1818

19-
func getFreeTCPPort(t require.TestingT) uint16 {
20-
var a *net.TCPAddr
21-
a, err := net.ResolveTCPAddr("tcp", "localhost:0")
22-
require.NoError(t, err)
23-
var l *net.TCPListener
24-
l, err = net.ListenTCP("tcp", a)
25-
require.NoError(t, err)
26-
defer l.Close()
27-
return uint16(l.Addr().(*net.TCPAddr).Port)
19+
func getFreeTCPPorts(t require.TestingT, count int) []uint16 {
20+
result := make([]uint16, count)
21+
for i := range count {
22+
var a *net.TCPAddr
23+
a, err := net.ResolveTCPAddr("tcp", "localhost:0")
24+
require.NoError(t, err)
25+
var l *net.TCPListener
26+
l, err = net.ListenTCP("tcp", a)
27+
require.NoError(t, err)
28+
defer l.Close()
29+
result[i] = uint16(l.Addr().(*net.TCPAddr).Port)
30+
}
31+
return result
2832
}
2933

3034
func CreateTransactionParams(account txnbuild.Account, op txnbuild.Operation) txnbuild.TransactionParams {

0 commit comments

Comments
 (0)