FindFreePort returns an available port that can be used for a server. If any errors are encountered, this function will panic and fail the current test.
()
| 217 | cachedDevBuildPath = fullpath |
| 218 | |
| 219 | return cachedDevBuildPath |
| 220 | } |
| 221 | |
| 222 | // FindFreePort returns an available port that can be used for a server. If any errors are |
| 223 | // encountered, this function will panic and fail the current test. |
| 224 | func FindFreePort() int { |
| 225 | listener, err := net.Listen("tcp", ":0") |
| 226 | if err != nil { |
| 227 | panic(fmt.Sprintf("unable to find available TCP port: %v", err.Error())) |
| 228 | } |
| 229 | freePort := listener.Addr().(*net.TCPAddr).Port |
| 230 | err = listener.Close() |
| 231 | if err != nil { |
| 232 | panic(fmt.Sprintf("unable to find available TCP port: %v", err.Error())) |
| 233 | } |
| 234 | |
| 235 | if freePort < 0 { |
| 236 | panic(fmt.Sprintf("unable to find available TCP port; found port %v", freePort)) |
| 237 | } |
| 238 |