- Notifications
You must be signed in to change notification settings - Fork928
chore: consolidate various randomPort() implementations#12362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -8,7 +8,6 @@ import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net" | ||
"net/http" | ||
"net/http/httptest" | ||
@@ -838,7 +837,7 @@ func TestAgent_TCPRemoteForwarding(t *testing.T) { | ||
var ll net.Listener | ||
var err error | ||
for { | ||
randomPort =testutil.RandomPortNoListen(t) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. nit: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. This one isn't guaranteed to be free and relies on pure randommess. | ||
addr := net.TCPAddrFromAddrPort(netip.AddrPortFrom(localhost, randomPort)) | ||
ll, err = sshClient.ListenTCP(addr) | ||
if err != nil { | ||
@@ -2666,20 +2665,6 @@ func (s *syncWriter) Write(p []byte) (int, error) { | ||
return s.w.Write(p) | ||
} | ||
// echoOnce accepts a single connection, reads 4 bytes and echos them back | ||
func echoOnce(t *testing.T, ll net.Listener) { | ||
t.Helper() | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package testutil | ||
import ( | ||
"math/rand" | ||
johnstcn marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
"net" | ||
"sync" | ||
"testing" | ||
"time" | ||
"github.com/stretchr/testify/require" | ||
) | ||
var ( | ||
// nolint:gosec // not used for cryptography | ||
rnd = rand.New(rand.NewSource(time.Now().Unix())) | ||
rndMu sync.Mutex | ||
) | ||
// RandomPort is a helper function to find a free random port. | ||
// Note that the OS may reallocate the port very quickly, so | ||
// this is not _guaranteed_. | ||
func RandomPort(t *testing.T) int { | ||
random, err := net.Listen("tcp", "127.0.0.1:0") | ||
require.NoError(t, err, "failed to listen on localhost") | ||
_ = random.Close() | ||
tcpAddr, valid := random.Addr().(*net.TCPAddr) | ||
require.True(t, valid, "random port address is not a *net.TCPAddr?!") | ||
return tcpAddr.Port | ||
} | ||
// RandomPortNoListen returns a random port in the ephemeral port range. | ||
// Does not attempt to listen and close to find a port as the OS may | ||
// reallocate the port very quickly. | ||
func RandomPortNoListen(*testing.T) uint16 { | ||
const ( | ||
// Overlap of windows, linux in https://en.wikipedia.org/wiki/Ephemeral_port | ||
min = 49152 | ||
max = 60999 | ||
) | ||
n := max - min | ||
rndMu.Lock() | ||
x := rnd.Intn(n) | ||
rndMu.Unlock() | ||
return uint16(min + x) | ||
} |