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

fix: test: use monotonical port numbers#13999

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

Closed
mtojek wants to merge4 commits intomainfrom13931-port-2
Closed
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
8 changes: 4 additions & 4 deletionsagent/agent_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -834,12 +834,12 @@ func TestAgent_TCPRemoteForwarding(t *testing.T) {
sshClient := setupAgentSSHClient(ctx, t)

localhost := netip.MustParseAddr("127.0.0.1")
varrandomPort uint16
varport uint16
var ll net.Listener
var err error
for {
randomPort = testutil.RandomPortNoListen(t)
addr := net.TCPAddrFromAddrPort(netip.AddrPortFrom(localhost,randomPort))
port = testutil.EphemeralPortNoListen()
addr := net.TCPAddrFromAddrPort(netip.AddrPortFrom(localhost,port))
ll, err = sshClient.ListenTCP(addr)
if err != nil {
t.Logf("error remote forwarding: %s", err.Error())
Expand All@@ -855,7 +855,7 @@ func TestAgent_TCPRemoteForwarding(t *testing.T) {
defer ll.Close()
go echoOnce(t, ll)

conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d",randomPort))
conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d",port))
require.NoError(t, err)
defer conn.Close()
requireEcho(t, conn)
Expand Down
4 changes: 1 addition & 3 deletionsenterprise/cli/provisionerdaemons_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -303,9 +303,7 @@ func TestProvisionerDaemon_SessionToken(t *testing.T) {

//nolint:paralleltest,tparallel // Prometheus endpoint tends to fail with `bind: address already in use`.
func TestProvisionerDaemon_PrometheusEnabled(t *testing.T) {
t.Skip("Flaky test - see https://github.com/coder/coder/issues/13931")

prometheusPort := testutil.RandomPortNoListen(t)
prometheusPort := testutil.EphemeralPortNoListen()

// Configure CLI client
client, admin := coderdenttest.New(t, &coderdenttest.Options{
Expand Down
32 changes: 17 additions & 15 deletionstestutil/port.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
package testutil

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

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

const (
// Overlap of windows, linux in https://en.wikipedia.org/wiki/Ephemeral_port
minPort = 49152
maxPort = 60999
)

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

// RandomPort is a helper function to find a free random port.
Expand All@@ -28,18 +31,17 @@ func RandomPort(t *testing.T) int {
return tcpAddr.Port
}

//RandomPortNoListen returnsa random port in the ephemeral port range.
//EphemeralPortNoListen returnsthe next 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
func EphemeralPortNoListen() uint16 {
rndMu.Lock()
x := rnd.Intn(n)
p := rndPort

rndPort--
if rndPort < minPort {
rndPort = maxPort
}
rndMu.Unlock()
return uint16(min + x)
return uint16(p)
}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp