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)
| 20 | // Note that the OS may reallocate the port very quickly, so |
| 21 | // this is not _guaranteed_. |
| 22 | func 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 |