Skip to content

Commit

Permalink
Rollup merge of #104146 - Ayush1325:remote-test-server, r=jyn514
Browse files Browse the repository at this point in the history
Retry binding TCP Socket in remote-test-server

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 #100316

Signed-off-by: Ayush Singh <ayushsingh1325@gmail.com>
  • Loading branch information
Dylan-DPC authored Nov 11, 2022
2 parents 6e819af + 06a77af commit e9b4a84
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 e9b4a84

Please sign in to comment.