about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAyush Singh <ayushsingh1325@gmail.com>2022-11-08 17:22:44 +0530
committerAyush Singh <ayushsingh1325@gmail.com>2022-11-08 19:21:24 +0530
commit06a77af35dea5dcdabe1676b795d375e119a0407 (patch)
tree1e2665a208df52901b69230e6b2d950aa1b4a80b
parent57d3c58ed6e0faf89a62411f96c000ffc9fd3937 (diff)
downloadrust-06a77af35dea5dcdabe1676b795d375e119a0407.tar.gz
rust-06a77af35dea5dcdabe1676b795d375e119a0407.zip
Add retry flag to 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 https://github.com/rust-lang/rust/pull/100316

Signed-off-by: Ayush Singh <ayushsingh1325@gmail.com>
-rw-r--r--src/tools/remote-test-server/src/main.rs14
1 files changed, 13 insertions, 1 deletions
diff --git a/src/tools/remote-test-server/src/main.rs b/src/tools/remote-test-server/src/main.rs
index ec992da6812..8e7c39e72b6 100644
--- a/src/tools/remote-test-server/src/main.rs
+++ b/src/tools/remote-test-server/src/main.rs
@@ -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 {
@@ -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 {
@@ -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);