-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace freeport dependency with modified version (#3470)
- Loading branch information
Showing
11 changed files
with
122 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package network | ||
|
||
import ( | ||
"net" | ||
) | ||
|
||
// GetFreePort returns a single available port by asking the operating | ||
// system to pick one for us. Luckily ports are not re-used so after asking | ||
// for a port number, we attempt to create a tcp listener. | ||
// | ||
// Essentially the same code as https://github.com/phayes/freeport but we bind | ||
// to 0.0.0.0 to ensure the port is free on all interfaces, and not just localhost.GetFreePort | ||
// Ports must be unique for an address, not an entire system and so checking just localhost | ||
// is not enough. | ||
func GetFreePort() (int, error) { | ||
addr, err := net.ResolveTCPAddr("tcp", ":0") | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
l, err := net.ListenTCP("tcp", addr) | ||
if err != nil { | ||
return 0, err | ||
} | ||
defer l.Close() | ||
return l.Addr().(*net.TCPAddr).Port, nil | ||
} | ||
|
||
// GetFreePorts returns an array available ports by asking the operating | ||
// system to pick one for us. | ||
// | ||
// Essentially the same code as https://github.com/phayes/freeport apart from | ||
// the caveats described in GetFreePort. | ||
func GetFreePorts(count int) ([]int, error) { | ||
ports := []int{} | ||
|
||
for i := 0; i < count; i++ { | ||
port, err := GetFreePort() | ||
if err != nil { | ||
return nil, err | ||
} | ||
ports = append(ports, port) | ||
} | ||
return ports, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//go:build unit || !integration | ||
|
||
package network_test | ||
|
||
import ( | ||
"net" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/bacalhau-project/bacalhau/pkg/lib/network" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type FreePortTestSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func TestFreePortTestSuite(t *testing.T) { | ||
suite.Run(t, new(FreePortTestSuite)) | ||
} | ||
|
||
func (s *FreePortTestSuite) TestGetFreePort() { | ||
port, err := network.GetFreePort() | ||
s.Require().NoError(err) | ||
s.NotEqual(0, port, "expected a non-zero port") | ||
|
||
// Try to listen on the port | ||
l, err := net.Listen("tcp", "127.0.0.1:"+strconv.Itoa(port)) | ||
s.Require().NoError(err) | ||
defer l.Close() | ||
} | ||
|
||
func (s *FreePortTestSuite) TestGetFreePorts() { | ||
count := 3 | ||
ports, err := network.GetFreePorts(count) | ||
s.Require().NoError(err) | ||
s.Equal(count, len(ports), "expected %d ports", count) | ||
|
||
for _, port := range ports { | ||
s.NotEqual(0, port, "expected a non-zero port") | ||
|
||
// Try to listen on the port | ||
l, err := net.Listen("tcp", ":"+strconv.Itoa(port)) | ||
s.Require().NoError(err, "failed to listen on newly given port") | ||
defer l.Close() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters