2023-02-02 17:15:17 -08:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
2021-02-25 14:31:14 -05:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// TestFreePort just returns an available free localhost port
|
2022-04-25 15:52:44 -04:00
|
|
|
func TestFreePort(t testing.TB) int {
|
2021-02-25 14:31:14 -05:00
|
|
|
t.Helper()
|
|
|
|
|
require := require.New(t)
|
|
|
|
|
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
|
|
l, err := net.ListenTCP("tcp", addr)
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
defer l.Close()
|
|
|
|
|
return l.Addr().(*net.TCPAddr).Port
|
|
|
|
|
}
|