Skip to content

Commit 3c554ff

Browse files
committed
fix: clashing httptest ports
1 parent d793c20 commit 3c554ff

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

pkg/http/server_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"log"
1010
"math/rand/v2"
11+
"net"
1112
"net/http"
1213
"os"
1314
"testing"
@@ -17,6 +18,15 @@ import (
1718
wegohttp "github.com/weaveworks/weave-gitops/pkg/http"
1819
)
1920

21+
func portInUse(port int) bool {
22+
conn, err := net.Dial("tcp", fmt.Sprintf("localhost:%d", port))
23+
if err != nil {
24+
return false
25+
}
26+
conn.Close()
27+
return true
28+
}
29+
2030
func TestMultiServerStartReturnsImmediatelyWithClosedContext(t *testing.T) {
2131
g := NewGomegaWithT(t)
2232
srv := wegohttp.MultiServer{
@@ -46,7 +56,8 @@ func TestMultiServerServesOverBothProtocols(t *testing.T) {
4656
httpPort := rand.N(49151-1024) + 1024 // #nosec G404
4757
httpsPort := rand.N(49151-1024) + 1024 // #nosec G404
4858

49-
for httpPort == httpsPort {
59+
for httpPort == httpsPort || portInUse(httpPort) || portInUse(httpsPort) {
60+
httpPort = rand.N(49151-1024) + 1024 // #nosec G404
5061
httpsPort = rand.N(49151-1024) + 1024 // #nosec G404
5162
}
5263

0 commit comments

Comments
 (0)