MCPcopy Index your code
hub / github.com/coder/coder / RandomPort

Function RandomPort

testutil/port.go:22–29  ·  view source on GitHub ↗

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_.

(t *testing.T)

Source from the content-addressed store, hash-verified

20// Note that the OS may reallocate the port very quickly, so
21// this is not _guaranteed_.
22func RandomPort(t *testing.T) int {
23 random, err := net.Listen("tcp", "127.0.0.1:0")
24 require.NoError(t, err, "failed to listen on localhost")
25 _ = random.Close()
26 tcpAddr, valid := random.Addr().(*net.TCPAddr)
27 require.True(t, valid, "random port address is not a *net.TCPAddr?!")
28 return tcpAddr.Port
29}
30
31// RandomPortNoListen returns a random port in the ephemeral port range.
32// Does not attempt to listen and close to find a port as the OS may

Callers 1

TestRunFunction · 0.92

Calls 3

ListenMethod · 0.65
CloseMethod · 0.65
AddrMethod · 0.45

Tested by 1

TestRunFunction · 0.74