Skip to content

Commit 3652b3e

Browse files
committed
itest: use node.NextAvailablePort() everywhere
That function produces ports that are unique across processes of a machine and allows running tests in parallel without port collisions.
1 parent ce81f8f commit 3652b3e

File tree

2 files changed

+6
-34
lines changed

2 files changed

+6
-34
lines changed

itest/tapd_harness.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
lfn "github.com/lightningnetwork/lnd/fn/v2"
3232
"github.com/lightningnetwork/lnd/lnrpc"
3333
"github.com/lightningnetwork/lnd/lntest/node"
34+
"github.com/lightningnetwork/lnd/lntest/port"
3435
"github.com/lightningnetwork/lnd/lntest/wait"
3536
"github.com/lightningnetwork/lnd/macaroons"
3637
"github.com/stretchr/testify/require"
@@ -199,10 +200,10 @@ func newTapdHarness(t *testing.T, ht *harnessTest, cfg tapdConfig,
199200
}
200201

201202
tapCfg.RpcConf.RawRPCListeners = []string{
202-
fmt.Sprintf("127.0.0.1:%d", nextAvailablePort()),
203+
fmt.Sprintf("127.0.0.1:%d", port.NextAvailablePort()),
203204
}
204205
tapCfg.RpcConf.RawRESTListeners = []string{
205-
fmt.Sprintf("127.0.0.1:%d", nextAvailablePort()),
206+
fmt.Sprintf("127.0.0.1:%d", port.NextAvailablePort()),
206207
}
207208

208209
// Update the config with the lnd node's connection info.

itest/test_harness.go

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"context"
55
"flag"
66
"fmt"
7-
"net"
8-
"sync/atomic"
97
"testing"
108
"time"
119

@@ -22,6 +20,7 @@ import (
2220
"github.com/lightningnetwork/lnd/build"
2321
"github.com/lightningnetwork/lnd/lntest"
2422
"github.com/lightningnetwork/lnd/lntest/node"
23+
"github.com/lightningnetwork/lnd/lntest/port"
2524
"github.com/lightningnetwork/lnd/lntest/wait"
2625
"github.com/lightningnetwork/lnd/signal"
2726
"github.com/stretchr/testify/require"
@@ -267,34 +266,6 @@ func (h *harnessTest) addFederationServer(host string, target *tapdHarness) {
267266
require.NoError(h.t, err)
268267
}
269268

270-
// nextAvailablePort returns the first port that is available for listening by
271-
// a new node. It panics if no port is found and the maximum available TCP port
272-
// is reached.
273-
func nextAvailablePort() int {
274-
port := atomic.AddUint32(&lastPort, 1)
275-
for port < 65535 {
276-
// If there are no errors while attempting to listen on this
277-
// port, close the socket and return it as available. While it
278-
// could be the case that some other process picks up this port
279-
// between the time the socket is closed and it's reopened in
280-
// the harness node, in practice in CI servers this seems much
281-
// less likely than simply some other process already being
282-
// bound at the start of the tests.
283-
addr := fmt.Sprintf("127.0.0.1:%d", port)
284-
l, err := net.Listen("tcp4", addr)
285-
if err == nil {
286-
err := l.Close()
287-
if err == nil {
288-
return int(port)
289-
}
290-
}
291-
port = atomic.AddUint32(&lastPort, 1)
292-
}
293-
294-
// No ports available? Must be a mistake.
295-
panic("no ports available for listening")
296-
}
297-
298269
// setupHarnesses creates new server and client harnesses that are connected
299270
// to each other through an in-memory gRPC connection.
300271
func setupHarnesses(t *testing.T, ht *harnessTest,
@@ -317,8 +288,8 @@ func setupHarnesses(t *testing.T, ht *harnessTest,
317288
var proofCourier proof.CourierHarness
318289
switch proofCourierType {
319290
case proof.HashmailCourierType:
320-
port := nextAvailablePort()
321-
apertureHarness := NewApertureHarness(ht.t, port)
291+
listenPort := port.NextAvailablePort()
292+
apertureHarness := NewApertureHarness(ht.t, listenPort)
322293
err := apertureHarness.Start(nil)
323294
require.NoError(t, err, "aperture proof courier harness")
324295

0 commit comments

Comments
 (0)