-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnet.go
34 lines (30 loc) · 849 Bytes
/
net.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package testament
import (
"net"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
// GetFreeOpenPort returns a port that is already claimed. It closes the
// listener on test cleanup.
func GetFreeOpenPort(t *testing.T) (uint, net.Listener) {
t.Helper()
l, err := net.Listen("tcp", ":0")
require.NoError(t, err, "could not open a port")
addr := l.Addr().String()
parts := strings.Split(addr, ":")
port, err := strconv.Atoi(parts[len(parts)-1])
require.NoErrorf(t, err, "could not guess the port from %q", addr)
t.Cleanup(func() {
l.Close() // nolint:errcheck // not important in tests.
})
return uint(port), l
}
// GetFreePort returns a random open port.
func GetFreePort(t *testing.T) uint {
t.Helper()
port, l := GetFreeOpenPort(t)
l.Close() // nolint:errcheck // not important in tests.
return port
}