Skip to content

Commit

Permalink
fix: clashing httptest ports
Browse files Browse the repository at this point in the history
  • Loading branch information
casibbald committed Jan 17, 2025
1 parent d793c20 commit 3c554ff
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pkg/http/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"log"
"math/rand/v2"
"net"
"net/http"
"os"
"testing"
Expand All @@ -17,6 +18,15 @@ import (
wegohttp "github.com/weaveworks/weave-gitops/pkg/http"
)

func portInUse(port int) bool {
conn, err := net.Dial("tcp", fmt.Sprintf("localhost:%d", port))
if err != nil {
return false
}
conn.Close()
return true
}

func TestMultiServerStartReturnsImmediatelyWithClosedContext(t *testing.T) {
g := NewGomegaWithT(t)
srv := wegohttp.MultiServer{
Expand Down Expand Up @@ -46,7 +56,8 @@ func TestMultiServerServesOverBothProtocols(t *testing.T) {
httpPort := rand.N(49151-1024) + 1024 // #nosec G404
httpsPort := rand.N(49151-1024) + 1024 // #nosec G404

for httpPort == httpsPort {
for httpPort == httpsPort || portInUse(httpPort) || portInUse(httpsPort) {
httpPort = rand.N(49151-1024) + 1024 // #nosec G404
httpsPort = rand.N(49151-1024) + 1024 // #nosec G404
}

Expand Down

0 comments on commit 3c554ff

Please sign in to comment.