Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

chore: consolidate various randomPort() implementations#12362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
johnstcn merged 2 commits intomainfromcj/dry-testutil-random-port
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletionsagent/agent_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"net/http/httptest"
Expand DownExpand Up@@ -838,7 +837,7 @@ func TestAgent_TCPRemoteForwarding(t *testing.T) {
var ll net.Listener
var err error
for {
randomPort =pickRandomPort()
randomPort =testutil.RandomPortNoListen(t)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

nit:testutil.FreePort?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This one isn't guaranteed to be free and relies on pure randommess.
Regarding the other implementation, I just picked the existing name.
We can rename later if need be. 👍

addr := net.TCPAddrFromAddrPort(netip.AddrPortFrom(localhost, randomPort))
ll, err = sshClient.ListenTCP(addr)
if err != nil {
Expand DownExpand Up@@ -2666,20 +2665,6 @@ func (s *syncWriter) Write(p []byte) (int, error) {
return s.w.Write(p)
}

// pickRandomPort picks a random port number for the ephemeral range. We do this entirely randomly
// instead of opening a listener and closing it to find a port that is likely to be free, since
// sometimes the OS reallocates the port very quickly.
func pickRandomPort() uint16 {
const (
// Overlap of windows, linux in https://en.wikipedia.org/wiki/Ephemeral_port
min = 49152
max = 60999
)
n := max - min
x := rand.Intn(n) //nolint: gosec
return uint16(min + x)
}

// echoOnce accepts a single connection, reads 4 bytes and echos them back
func echoOnce(t *testing.T, ll net.Listener) {
t.Helper()
Expand Down
13 changes: 2 additions & 11 deletionscli/server_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -937,22 +937,13 @@ func TestServer(t *testing.T) {
t.Run("Prometheus", func(t *testing.T) {
t.Parallel()

randomPort := func(t *testing.T) int {
random, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
_ = random.Close()
tcpAddr, valid := random.Addr().(*net.TCPAddr)
require.True(t, valid)
return tcpAddr.Port
}

t.Run("DBMetricsDisabled", func(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
defer cancel()

randPort :=randomPort(t)
randPort :=testutil.RandomPort(t)
inv, cfg := clitest.New(t,
"server",
"--in-memory",
Expand DownExpand Up@@ -1008,7 +999,7 @@ func TestServer(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
defer cancel()

randPort :=randomPort(t)
randPort :=testutil.RandomPort(t)
inv, cfg := clitest.New(t,
"server",
"--in-memory",
Expand Down
13 changes: 1 addition & 12 deletionsenterprise/cli/provisionerdaemons_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,7 +4,6 @@ import (
"bufio"
"context"
"fmt"
"net"
"net/http"
"strings"
"testing"
Expand DownExpand Up@@ -170,7 +169,7 @@ func TestProvisionerDaemon_SessionToken(t *testing.T) {
t.Run("PrometheusEnabled", func(t *testing.T) {
t.Parallel()

prometheusPort :=randomPort(t)
prometheusPort :=testutil.RandomPort(t)

// Configure CLI client
client, admin := coderdenttest.New(t, &coderdenttest.Options{
Expand DownExpand Up@@ -242,13 +241,3 @@ func TestProvisionerDaemon_SessionToken(t *testing.T) {
require.True(t, hasPromHTTP, "Prometheus HTTP metrics are missing")
})
}

// randomPort is a helper function to find a free random port, for instance to spawn Prometheus endpoint.
func randomPort(t *testing.T) int {
random, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
_ = random.Close()
tcpAddr, valid := random.Addr().(*net.TCPAddr)
require.True(t, valid)
return tcpAddr.Port
}
4 changes: 2 additions & 2 deletionsenterprise/cli/proxyserver_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,7 +63,7 @@ func Test_Headers(t *testing.T) {
func TestWorkspaceProxy_Server_PrometheusEnabled(t *testing.T) {
t.Parallel()

prometheusPort :=randomPort(t)
prometheusPort :=testutil.RandomPort(t)

var wg sync.WaitGroup
wg.Add(1)
Expand DownExpand Up@@ -96,7 +96,7 @@ func TestWorkspaceProxy_Server_PrometheusEnabled(t *testing.T) {
"--primary-access-url", srv.URL,
"--proxy-session-token", "test-token",
"--access-url", "http://foobar:3001",
"--http-address", fmt.Sprintf("127.0.0.1:%d",randomPort(t)),
"--http-address", fmt.Sprintf("127.0.0.1:%d",testutil.RandomPort(t)),
"--prometheus-enable",
"--prometheus-address", fmt.Sprintf("127.0.0.1:%d", prometheusPort),
)
Expand Down
2 changes: 1 addition & 1 deletionenterprise/cli/server_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,7 +22,7 @@ func TestServer(t *testing.T) {
var root cli.RootCmd
cmd, err := root.Command(root.EnterpriseSubcommands())
require.NoError(t, err)
port :=randomPort(t)
port :=testutil.RandomPort(t)
inv, _ := clitest.NewWithCommand(t, cmd,
"server",
"--in-memory",
Expand Down
45 changes: 45 additions & 0 deletionstestutil/port.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
package testutil

import (
"math/rand"
"net"
"sync"
"testing"
"time"

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

var (
// nolint:gosec // not used for cryptography
rnd = rand.New(rand.NewSource(time.Now().Unix()))
rndMu sync.Mutex
)

// RandomPort is a helper function to find a free random port.
// Note that the OS may reallocate the port very quickly, so
// this is not _guaranteed_.
func RandomPort(t *testing.T) int {
random, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err, "failed to listen on localhost")
_ = random.Close()
tcpAddr, valid := random.Addr().(*net.TCPAddr)
require.True(t, valid, "random port address is not a *net.TCPAddr?!")
return tcpAddr.Port
}

// RandomPortNoListen returns a random port in the ephemeral port range.
// Does not attempt to listen and close to find a port as the OS may
// reallocate the port very quickly.
func RandomPortNoListen(*testing.T) uint16 {
const (
// Overlap of windows, linux in https://en.wikipedia.org/wiki/Ephemeral_port
min = 49152
max = 60999
)
n := max - min
rndMu.Lock()
x := rnd.Intn(n)
rndMu.Unlock()
return uint16(min + x)
}

[8]ページ先頭

©2009-2025 Movatter.jp