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.
300271func 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