Skip to content

Commit

Permalink
Fix small nits in testutil, handle errors, initialize slices (open-te…
Browse files Browse the repository at this point in the history
…lemetry#4845)

* Fix small nits in testutil, handle errors, initialize slices

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>

* Update testutil.go
  • Loading branch information
bogdandrutu authored Feb 18, 2022
1 parent c4134ba commit 70271f2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
7 changes: 5 additions & 2 deletions internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -39,7 +40,9 @@ func GetAvailableLocalAddress(t *testing.T) string {
require.NoError(t, err, "Failed to get a free local port")
// There is a possible race if something else takes this same port before
// the test uses it, however, that is unlikely in practice.
defer ln.Close()
defer func() {
assert.NoError(t, ln.Close())
}()
return ln.Addr().String()
}

Expand Down Expand Up @@ -92,7 +95,7 @@ func getExclusionsList(t *testing.T) []portpair {
}

func createExclusionsList(exclusionsText string, t *testing.T) []portpair {
exclusions := []portpair{}
var exclusions []portpair

parts := strings.Split(exclusionsText, "--------")
require.Equal(t, len(parts), 3)
Expand Down
5 changes: 4 additions & 1 deletion internal/testutil/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"strconv"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -38,7 +39,9 @@ func testEndpointAvailable(t *testing.T, endpoint string) {
ln0, err := net.Listen("tcp", endpoint)
require.NoError(t, err)
require.NotNil(t, ln0)
defer ln0.Close()
t.Cleanup(func() {
assert.NoError(t, ln0.Close())
})

// Ensure that the endpoint wasn't something like ":0" by checking that a
// second listener will fail.
Expand Down

0 comments on commit 70271f2

Please sign in to comment.