@@ -12,6 +12,8 @@ use assert_cmd::prelude::CommandCargoExt;
12
12
use async_trait:: async_trait;
13
13
use derive_more:: Display ;
14
14
use futures:: executor:: block_on;
15
+ use port_scanner:: local_port_available;
16
+ use rand:: Rng ;
15
17
use uuid:: Uuid ;
16
18
17
19
use iggy:: client:: { Client , StreamClient , UserClient } ;
@@ -247,21 +249,40 @@ impl TestServer {
247
249
}
248
250
}
249
251
252
+ fn generate_random_port ( ) -> u16 {
253
+ let mut rng = rand:: thread_rng ( ) ;
254
+ let mut port: u16 = rng. gen ( ) ;
255
+ while !local_port_available ( port) {
256
+ port = rng. gen ( ) ;
257
+ }
258
+ port
259
+ }
260
+
250
261
fn get_server_ipv4_addrs_with_random_port ( ) -> Vec < ServerProtocolAddr > {
251
- let addr = SocketAddr :: new ( Ipv4Addr :: LOCALHOST . into ( ) , 0 ) ;
262
+ let tcp_port: u16 = Self :: generate_random_port ( ) ;
263
+ let quic_port: u16 = Self :: generate_random_port ( ) ;
264
+ let http_port: u16 = Self :: generate_random_port ( ) ;
265
+ let tcp_addr = SocketAddr :: new ( Ipv4Addr :: LOCALHOST . into ( ) , tcp_port) ;
266
+ let quic_addr = SocketAddr :: new ( Ipv4Addr :: LOCALHOST . into ( ) , quic_port) ;
267
+ let http_addr = SocketAddr :: new ( Ipv4Addr :: LOCALHOST . into ( ) , http_port) ;
252
268
vec ! [
253
- ServerProtocolAddr :: QuicUdp ( addr ) ,
254
- ServerProtocolAddr :: RawTcp ( addr ) ,
255
- ServerProtocolAddr :: HttpTcp ( addr ) ,
269
+ ServerProtocolAddr :: QuicUdp ( quic_addr ) ,
270
+ ServerProtocolAddr :: RawTcp ( tcp_addr ) ,
271
+ ServerProtocolAddr :: HttpTcp ( http_addr ) ,
256
272
]
257
273
}
258
274
259
275
fn get_server_ipv6_addrs_with_random_port ( ) -> Vec < ServerProtocolAddr > {
260
- let addr = SocketAddr :: new ( Ipv6Addr :: LOCALHOST . into ( ) , 0 ) ;
276
+ let tcp_port: u16 = Self :: generate_random_port ( ) ;
277
+ let quic_port: u16 = Self :: generate_random_port ( ) ;
278
+ let http_port: u16 = Self :: generate_random_port ( ) ;
279
+ let tcp_addr = SocketAddr :: new ( Ipv6Addr :: LOCALHOST . into ( ) , tcp_port) ;
280
+ let quic_addr = SocketAddr :: new ( Ipv6Addr :: LOCALHOST . into ( ) , quic_port) ;
281
+ let http_addr = SocketAddr :: new ( Ipv6Addr :: LOCALHOST . into ( ) , http_port) ;
261
282
vec ! [
262
- ServerProtocolAddr :: QuicUdp ( addr ) ,
263
- ServerProtocolAddr :: RawTcp ( addr ) ,
264
- ServerProtocolAddr :: HttpTcp ( addr ) ,
283
+ ServerProtocolAddr :: QuicUdp ( quic_addr ) ,
284
+ ServerProtocolAddr :: RawTcp ( tcp_addr ) ,
285
+ ServerProtocolAddr :: HttpTcp ( http_addr ) ,
265
286
]
266
287
}
267
288
0 commit comments