Documentation ¶
Overview ¶
Package freeport provides a helper for reserving free TCP ports across multiple processes on the same machine. Each process reserves a block of ports outside the ephemeral port range. Tests can request one of these reserved ports and freeport will ensure that no other test uses that port until it is returned to freeport.
Freeport is particularly useful when the code being tested does not accept a net.Listener. Any code that accepts a net.Listener (or uses net/http/httptest.Server) can use port 0 (ex: 127.0.0.1:0) to find an unused ephemeral port that will not conflict.
Any code that does not accept a net.Listener or can not bind directly to port zero should use freeport to find an unused port.
Index ¶
- func Free(n int) (ports []int, err error)deprecated
- func Get(n int) (ports []int)deprecated
- func GetN(t TestingT, n int) []int
- func GetOne(t TestingT) int
- func GetT(t TestingT, n int) (ports []int)deprecated
- func MustTake(n int) (ports []int)deprecated
- func Return(ports []int)
- func Take(n int) (ports []int, err error)
- type TestingT
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetN ¶ added in v0.9.0
GetN returns n free ports from the reserved port block, and returns the ports to the pool when the test ends. See Take for more details.
func GetOne ¶ added in v0.9.0
GetOne returns a single free port from the reserved port block, and returns the port to the pool when the test ends. See Take for more details. Use GetN if more than a single port is required.
func Return ¶ added in v0.3.0
func Return(ports []int)
Return returns a block of ports back to the general pool. These ports should have been returned from a call to Take().
func Take ¶ added in v0.3.0
Take returns a list of free ports from the reserved port block. It is safe to call this method concurrently. Ports have been tested to be available on 127.0.0.1 TCP but there is no guarantee that they will remain free in the future.
Most callers should prefer GetN or GetOne.
Types ¶
type TestingT ¶ added in v0.9.0
type TestingT interface { Cleanup(func()) Helper() Fatalf(format string, args ...interface{}) Name() string }
TestingT is the minimal set of methods implemented by *testing.T that are used by functions in freelist.
In the future new methods may be added to this interface, but those methods should always be implemented by *testing.T