Skip to content

Commit df16081

Browse files
authored
Get second port without closing first one to prevent port clashing (#5077)
Get second port without closing first one to prevent port clashing
1 parent 3b87970 commit df16081

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

pkg/util/tls/test/tls_integration_test.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,21 @@ func (h *grpcHealthCheck) Watch(_ *grpc_health_v1.HealthCheckRequest, _ grpc_hea
5151
return status.Error(codes.Unimplemented, "Watching is not supported")
5252
}
5353

54-
func getLocalHostPort() (int, error) {
54+
func getLocalHostPort() (int, func() error, error) {
5555
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
5656
if err != nil {
57-
return 0, err
57+
return 0, nil, err
5858
}
5959

6060
l, err := net.ListenTCP("tcp", addr)
6161
if err != nil {
62-
return 0, err
62+
return 0, nil, err
6363
}
6464

65-
if err := l.Close(); err != nil {
66-
return 0, err
65+
closePort := func() error {
66+
return l.Close()
6767
}
68-
return l.Addr().(*net.TCPAddr).Port, nil
68+
return l.Addr().(*net.TCPAddr).Port, closePort, nil
6969
}
7070

7171
func newIntegrationClientServer(
@@ -80,9 +80,14 @@ func newIntegrationClientServer(
8080
prometheus.DefaultRegisterer = savedRegistry
8181
}()
8282

83-
grpcPort, err := getLocalHostPort()
83+
grpcPort, closeGrpcPort, err := getLocalHostPort()
8484
require.NoError(t, err)
85-
httpPort, err := getLocalHostPort()
85+
httpPort, closeHTTPPort, err := getLocalHostPort()
86+
require.NoError(t, err)
87+
88+
err = closeGrpcPort()
89+
require.NoError(t, err)
90+
err = closeHTTPPort()
8691
require.NoError(t, err)
8792

8893
cfg.HTTPListenPort = httpPort

0 commit comments

Comments
 (0)