@@ -137,26 +137,44 @@ func TestDiscovery(t *testing.T) {
137137 srvTLS1 := & http.Server {Addr : "127.0.0.1:20001" , Handler : tlsMux , ReadTimeout : 1 * time .Second }
138138 srvTLS2 := & http.Server {Addr : "localhost:20002" , Handler : tlsMux , ReadTimeout : 1 * time .Second }
139139
140+ // Create listeners first to ensure ports are bound before tests run
141+ ln1 , err := net .Listen ("tcp" , srv .Addr )
142+ if err != nil {
143+ t .Fatalf ("Failed to create listener for srv: %s" , err )
144+ }
145+ ln2 , err := net .Listen ("tcp" , srv2 .Addr )
146+ if err != nil {
147+ t .Fatalf ("Failed to create listener for srv2: %s" , err )
148+ }
149+ lnTLS1 , err := net .Listen ("tcp" , srvTLS1 .Addr )
150+ if err != nil {
151+ t .Fatalf ("Failed to create listener for srvTLS1: %s" , err )
152+ }
153+ lnTLS2 , err := net .Listen ("tcp" , srvTLS2 .Addr )
154+ if err != nil {
155+ t .Fatalf ("Failed to create listener for srvTLS2: %s" , err )
156+ }
157+
140158 go func () {
141- if err := srv .ListenAndServe ( ); err != nil && err != http .ErrServerClosed {
159+ if err := srv .Serve ( ln1 ); err != nil && err != http .ErrServerClosed {
142160 t .Errorf ("Unable to start server: %s" , err )
143161 return
144162 }
145163 }()
146164 go func () {
147- if err := srv2 .ListenAndServe ( ); err != nil && err != http .ErrServerClosed {
165+ if err := srv2 .Serve ( ln2 ); err != nil && err != http .ErrServerClosed {
148166 t .Errorf ("Unable to start server2: %s" , err )
149167 return
150168 }
151169 }()
152170 go func () {
153- if err := srvTLS1 .ListenAndServeTLS ( "testdata/cert.pem" , "testdata/key.pem" ); err != nil && err != http .ErrServerClosed {
171+ if err := srvTLS1 .ServeTLS ( lnTLS1 , "testdata/cert.pem" , "testdata/key.pem" ); err != nil && err != http .ErrServerClosed {
154172 t .Errorf ("Unable to start TLS server1: %s" , err )
155173 return
156174 }
157175 }()
158176 go func () {
159- if err := srvTLS2 .ListenAndServeTLS ( "testdata/cert.pem" , "testdata/key.pem" ); err != nil && err != http .ErrServerClosed {
177+ if err := srvTLS2 .ServeTLS ( lnTLS2 , "testdata/cert.pem" , "testdata/key.pem" ); err != nil && err != http .ErrServerClosed {
160178 t .Errorf ("Unable to start TLS server2: %s" , err )
161179 return
162180 }
@@ -661,10 +679,10 @@ func TestDiscovery(t *testing.T) {
661679
662680 // Add health check handler (catch-all for /{$} and /)
663681 testMux .HandleFunc ("/{$}" , func (w http.ResponseWriter , r * http.Request ) {
664- healthResp := map [string ]interface {} {
682+ healthResp := map [string ]any {
665683 "name" : "test-node" ,
666684 "cluster_name" : "test-cluster" ,
667- "version" : map [string ]interface {} {
685+ "version" : map [string ]any {
668686 "number" : "2.0.0" ,
669687 },
670688 }
@@ -684,25 +702,25 @@ func TestDiscovery(t *testing.T) {
684702 // Add nodes info handler with this test's data
685703 testMux .HandleFunc ("/_nodes/http" , func (w http.ResponseWriter , r * http.Request ) {
686704 // Create a simple response structure compatible with the discovery parsing
687- response := map [string ]interface {} {
688- "_nodes" : map [string ]interface {} {
705+ response := map [string ]any {
706+ "_nodes" : map [string ]any {
689707 "total" : len (tt .args .Nodes ),
690708 "successful" : len (tt .args .Nodes ),
691709 "failed" : 0 ,
692710 },
693711 "cluster_name" : "test-cluster" ,
694- "nodes" : make (map [string ]interface {} ),
712+ "nodes" : make (map [string ]any ),
695713 }
696714
697- nodes := response ["nodes" ].(map [string ]interface {} )
715+ nodes := response ["nodes" ].(map [string ]any )
698716 for name , node := range tt .args .Nodes {
699717 // Use the test server address for publish_address so health checks work
700- nodes [name ] = map [string ]interface {} {
718+ nodes [name ] = map [string ]any {
701719 "name" : name ,
702720 "host" : "127.0.0.1" ,
703721 "ip" : "127.0.0.1" ,
704722 "roles" : node .Roles ,
705- "http" : map [string ]interface {} {
723+ "http" : map [string ]any {
706724 "publish_address" : testServer .Addr , // Point to our test server, not fictional hostnames
707725 },
708726 }
@@ -1056,10 +1074,10 @@ func TestDiscoverNodesWithNewRoleValidation(t *testing.T) {
10561074
10571075 // Health check endpoint - exact root path match
10581076 mux .HandleFunc ("/{$}" , func (w http.ResponseWriter , r * http.Request ) {
1059- healthResp := map [string ]interface {} {
1077+ healthResp := map [string ]any {
10601078 "name" : "test-node" ,
10611079 "cluster_name" : "test-cluster" ,
1062- "version" : map [string ]interface {} {
1080+ "version" : map [string ]any {
10631081 "number" : "2.0.0" ,
10641082 },
10651083 }
@@ -1194,17 +1212,21 @@ func TestIncludeDedicatedClusterManagersConfiguration(t *testing.T) {
11941212 testMux := http .NewServeMux ()
11951213
11961214 // Start a test server first so we have the address
1197- testServer := & http.Server {Addr : "127.0.0.1:0" , Handler : testMux }
1215+ testServer := & http.Server {
1216+ Addr : "127.0.0.1:0" ,
1217+ Handler : testMux ,
1218+ ReadHeaderTimeout : 5 * time .Second ,
1219+ }
11981220 listener , err := net .Listen ("tcp" , testServer .Addr )
11991221 require .NoError (t , err )
12001222 testServer .Addr = listener .Addr ().String ()
12011223
12021224 // Health check endpoint (catch-all for /{$} and /)
12031225 testMux .HandleFunc ("/{$}" , func (w http.ResponseWriter , r * http.Request ) {
1204- healthResp := map [string ]interface {} {
1226+ healthResp := map [string ]any {
12051227 "name" : "test-node" ,
12061228 "cluster_name" : "test-cluster" ,
1207- "version" : map [string ]interface {} {
1229+ "version" : map [string ]any {
12081230 "number" : "2.0.0" ,
12091231 },
12101232 }
@@ -1215,24 +1237,24 @@ func TestIncludeDedicatedClusterManagersConfiguration(t *testing.T) {
12151237
12161238 // Nodes info endpoint
12171239 testMux .HandleFunc ("/_nodes/http" , func (w http.ResponseWriter , r * http.Request ) {
1218- response := map [string ]interface {} {
1219- "_nodes" : map [string ]interface {} {
1240+ response := map [string ]any {
1241+ "_nodes" : map [string ]any {
12201242 "total" : len (tt .nodes ),
12211243 "successful" : len (tt .nodes ),
12221244 "failed" : 0 ,
12231245 },
12241246 "cluster_name" : "test-cluster" ,
1225- "nodes" : make (map [string ]interface {} ),
1247+ "nodes" : make (map [string ]any ),
12261248 }
12271249
1228- nodes := response ["nodes" ].(map [string ]interface {} )
1250+ nodes := response ["nodes" ].(map [string ]any )
12291251 for name , roles := range tt .nodes {
1230- nodes [name ] = map [string ]interface {} {
1252+ nodes [name ] = map [string ]any {
12311253 "name" : name ,
12321254 "host" : "127.0.0.1" ,
12331255 "ip" : "127.0.0.1" ,
12341256 "roles" : roles ,
1235- "http" : map [string ]interface {} {
1257+ "http" : map [string ]any {
12361258 "publish_address" : testServer .Addr , // Point to our test server
12371259 },
12381260 }
0 commit comments