Skip to content

Commit a684d46

Browse files
Random port (#258)
* Add random ports and remove whitespaces add indv random ports * update * update * add asserts * add comments * update random_port
1 parent c531399 commit a684d46

File tree

3 files changed

+89
-52
lines changed

3 files changed

+89
-52
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ libc = "0.2"
2323
ringbuf = "0.2.6"
2424
dashmap = { version = "5.1", features=["serde"] }
2525
parking_lot = "0.12"
26+
rand = "0.8.4"
2627

2728
[dev-dependencies]
2829
criterion = { version = "0.3", features = ["html_reports"]}

src/tests/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
mod fs_tests;
44
mod ipc_tests;
55
mod networking_tests;
6+
use rand::Rng;
7+
use std::net::{TcpListener, UdpSocket};
68

79
use crate::interface;
810
use crate::safeposix::{cage::*, filesystem::*};
@@ -90,3 +92,19 @@ pub fn sizecbuf<'a>(size: usize) -> Box<[u8]> {
9092
pub fn cbuf2str(buf: &[u8]) -> &str {
9193
std::str::from_utf8(buf).unwrap()
9294
}
95+
96+
// The RustPOSIX test suite avoids conflicts caused by repeatedly binding to the same ports by generating a random port number within the valid range (49152-65535) for each test run. This eliminates the need for waiting between tests.
97+
98+
fn is_port_available(port: u16) -> bool {
99+
TcpListener::bind(("127.0.0.1", port)).is_ok() &&
100+
UdpSocket::bind(("127.0.0.1", port)).is_ok()
101+
}
102+
103+
pub fn generate_random_port() -> u16 {
104+
for port in 49152..65535 {
105+
if is_port_available(port) {
106+
return port;
107+
}
108+
}
109+
panic!("No available ports found");
110+
}

0 commit comments

Comments
 (0)