File tree Expand file tree Collapse file tree 1 file changed +12
-1
lines changed Expand file tree Collapse file tree 1 file changed +12
-1
lines changed Original file line number Diff line number Diff line change 8
8
"io"
9
9
"log"
10
10
"math/rand/v2"
11
+ "net"
11
12
"net/http"
12
13
"os"
13
14
"testing"
@@ -17,6 +18,15 @@ import (
17
18
wegohttp "github.com/weaveworks/weave-gitops/pkg/http"
18
19
)
19
20
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
+
20
30
func TestMultiServerStartReturnsImmediatelyWithClosedContext (t * testing.T ) {
21
31
g := NewGomegaWithT (t )
22
32
srv := wegohttp.MultiServer {
@@ -46,7 +56,8 @@ func TestMultiServerServesOverBothProtocols(t *testing.T) {
46
56
httpPort := rand .N (49151 - 1024 ) + 1024 // #nosec G404
47
57
httpsPort := rand .N (49151 - 1024 ) + 1024 // #nosec G404
48
58
49
- for httpPort == httpsPort {
59
+ for httpPort == httpsPort || portInUse (httpPort ) || portInUse (httpsPort ) {
60
+ httpPort = rand .N (49151 - 1024 ) + 1024 // #nosec G404
50
61
httpsPort = rand .N (49151 - 1024 ) + 1024 // #nosec G404
51
62
}
52
63
You can’t perform that action at this time.
0 commit comments