aboutsummaryrefslogtreecommitdiffhomepage
path: root/helpers
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2022-03-18 08:54:44 +0100
committerBjørn Erik Pedersen <[email protected]>2022-03-21 09:32:35 +0100
commit9539069f5eb5f5abbd7138eda10893353d0c5eea (patch)
tree576a583ab532525c66a113e5945fb9d1bdf5485e /helpers
parent0e305d695820779a5c66fbe3434e3293911d7f98 (diff)
downloadhugo-9539069f5eb5f5abbd7138eda10893353d0c5eea.tar.gz
hugo-9539069f5eb5f5abbd7138eda10893353d0c5eea.zip
commands: Improve server startup to make tests less flaky
Do this by announcing/listen on the local address before we start the server.
Diffstat (limited to 'helpers')
-rw-r--r--helpers/general.go15
1 files changed, 15 insertions, 0 deletions
diff --git a/helpers/general.go b/helpers/general.go
index b5f6d0dba..e31bbfc9d 100644
--- a/helpers/general.go
+++ b/helpers/general.go
@@ -62,6 +62,21 @@ func FindAvailablePort() (*net.TCPAddr, error) {
return nil, err
}
+// TCPListen starts listening on a valid TCP port.
+func TCPListen() (net.Listener, *net.TCPAddr, error) {
+ l, err := net.Listen("tcp", ":0")
+ if err != nil {
+ return nil, nil, err
+ }
+ addr := l.Addr()
+ if a, ok := addr.(*net.TCPAddr); ok {
+ return l, a, nil
+ }
+ l.Close()
+ return nil, nil, fmt.Errorf("unable to obtain a valid tcp port: %v", addr)
+
+}
+
// InStringArray checks if a string is an element of a slice of strings
// and returns a boolean value.
func InStringArray(arr []string, el string) bool {