From 8274a8cfee1d16e77154c71f8f12f619e30b8836 Mon Sep 17 00:00:00 2001 From: Matthew Jaffee Date: Tue, 4 May 2021 15:17:41 -0500 Subject: [PATCH] avoid nil pointer exception when failing to get listener instead of an opaque NPE on the next line, panic with explicit error telling you what went wrong (in my case it was too many open files) --- cluster_internal_test.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cluster_internal_test.go b/cluster_internal_test.go index 96e27108b..9484b7721 100644 --- a/cluster_internal_test.go +++ b/cluster_internal_test.go @@ -30,6 +30,7 @@ import ( "github.com/pilosa/pilosa/v2/testhook" "github.com/pilosa/pilosa/v2/topology" . "github.com/pilosa/pilosa/v2/vprint" // nolint:staticcheck + "github.com/pkg/errors" ) // GlobalPortMap avoids many races and port conflicts when setting @@ -53,7 +54,10 @@ func NewGlobalPortMapper(n int) (pm *GlobalPortMapper) { availPorts: make(map[int]net.Listener), } for i := 0; i < n; i++ { - lsn, _ := net.Listen("tcp", ":0") + lsn, err := net.Listen("tcp", ":0") + if err != nil { + panic(errors.Wrap(err, "trying to listen on ephemeral port")) + } r := lsn.Addr() port := r.(*net.TCPAddr).Port pm.availPorts[port] = lsn