freeport

package
v0.16.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 28, 2024 License: MPL-2.0 Imports: 11 Imported by: 8

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Free deprecated

func Free(n int) (ports []int, err error)

Deprecated: Please use Take/Return calls instead.

func Get deprecated

func Get(n int) (ports []int)

Deprecated: Please use Take/Return calls instead.

func GetN added in v0.9.0

func GetN(t TestingT, n int) []int

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

func GetOne(t TestingT) int

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 GetT deprecated

func GetT(t TestingT, n int) (ports []int)

Deprecated: Please use Take/Return calls instead.

func MustTake deprecated added in v0.3.0

func MustTake(n int) (ports []int)

MustTake is the same as Take except it panics on error.

Deprecated: Use GetN or GetOne instead.

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

func Take(n int) (ports []int, err error)

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL