Skip to content

Commit

Permalink
Add retry flag to remote-test-server
Browse files Browse the repository at this point in the history
This allows retrying binding TCP Socket multiple times. This is useful
when using emulators as network might not be available in the beginning.
This was orignally implemented in rust-lang#100316

Signed-off-by: Ayush Singh <[email protected]>
  • Loading branch information
Ayush1325 committed Nov 8, 2022
1 parent 57d3c58 commit 06a77af
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/tools/remote-test-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ macro_rules! t {
}

static TEST: AtomicUsize = AtomicUsize::new(0);
const RETRY_INTERVAL: u64 = 1;
const NUMBER_OF_RETRIES: usize = 5;

#[derive(Copy, Clone)]
struct Config {
Expand Down Expand Up @@ -115,7 +117,7 @@ fn main() {
let config = Config::parse_args();
println!("starting test server");

let listener = t!(TcpListener::bind(config.bind));
let listener = bind_socket(config.bind);
let (work, tmp): (PathBuf, PathBuf) = if cfg!(target_os = "android") {
("/data/local/tmp/work".into(), "/data/local/tmp/work/tmp".into())
} else {
Expand Down Expand Up @@ -159,6 +161,16 @@ fn main() {
}
}

fn bind_socket(addr: SocketAddr) -> TcpListener {
for _ in 0..(NUMBER_OF_RETRIES - 1) {
if let Ok(x) = TcpListener::bind(addr) {
return x;
}
std::thread::sleep(std::time::Duration::from_secs(RETRY_INTERVAL));
}
TcpListener::bind(addr).unwrap()
}

fn handle_push(socket: TcpStream, work: &Path, config: Config) {
let mut reader = BufReader::new(socket);
let dst = recv(&work, &mut reader);
Expand Down

0 comments on commit 06a77af

Please sign in to comment.