about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2023-10-08T11·33+0200
committerclbot <clbot@tvl.fyi>2023-10-08T22·50+0000
commitb6bf3a87f162be158fea1386de1ee87a53c4d65b (patch)
tree30e107ca8dbf80c8fd6321082f65958e0e0fc974
parentc0376995c9ac4e4b9ca606219035e808fe815d69 (diff)
test(tvix/castore): use tokio-retry for exp backoff r/6742
Rather than using this loop, use exponential backoff while waiting for
the socket path to be created.

Change-Id: I18706a64ce06f8916a07892dfbcd409ac5b3bff1
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9568
Autosubmit: flokli <flokli@flokli.de>
Reviewed-by: Connor Brewster <cbrewster@hey.com>
Tested-by: BuildkiteCI
-rw-r--r--tvix/Cargo.lock12
-rw-r--r--tvix/Cargo.nix36
-rw-r--r--tvix/castore/Cargo.toml3
-rw-r--r--tvix/castore/src/blobservice/grpc.rs28
4 files changed, 63 insertions, 16 deletions
diff --git a/tvix/Cargo.lock b/tvix/Cargo.lock
index 547bfad8292f..5a9c290a7a39 100644
--- a/tvix/Cargo.lock
+++ b/tvix/Cargo.lock
@@ -2461,6 +2461,17 @@ dependencies = [
 ]
 
 [[package]]
+name = "tokio-retry"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7f57eb36ecbe0fc510036adff84824dd3c24bb781e21bfa67b69d556aa85214f"
+dependencies = [
+ "pin-project",
+ "rand",
+ "tokio",
+]
+
+[[package]]
 name = "tokio-stream"
 version = "0.1.14"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2700,6 +2711,7 @@ dependencies = [
  "test-case",
  "thiserror",
  "tokio",
+ "tokio-retry",
  "tokio-stream",
  "tokio-util",
  "tonic",
diff --git a/tvix/Cargo.nix b/tvix/Cargo.nix
index 7c98171a351a..68ec9b545279 100644
--- a/tvix/Cargo.nix
+++ b/tvix/Cargo.nix
@@ -7182,6 +7182,38 @@ rec {
         ];
 
       };
+      "tokio-retry" = rec {
+        crateName = "tokio-retry";
+        version = "0.3.0";
+        edition = "2018";
+        sha256 = "0kr1hnm5dmb9gfkby88yg2xj8g6x4i4gipva0c8ca3xyxhvfnmvz";
+        authors = [
+          "Sam Rijs <srijs@airpost.net>"
+        ];
+        dependencies = [
+          {
+            name = "pin-project";
+            packageId = "pin-project";
+          }
+          {
+            name = "rand";
+            packageId = "rand";
+          }
+          {
+            name = "tokio";
+            packageId = "tokio";
+            features = [ "time" ];
+          }
+        ];
+        devDependencies = [
+          {
+            name = "tokio";
+            packageId = "tokio";
+            features = [ "full" ];
+          }
+        ];
+
+      };
       "tokio-stream" = rec {
         crateName = "tokio-stream";
         version = "0.1.14";
@@ -8127,6 +8159,10 @@ rec {
             name = "test-case";
             packageId = "test-case";
           }
+          {
+            name = "tokio-retry";
+            packageId = "tokio-retry";
+          }
         ];
         features = {
           "tonic-reflection" = [ "dep:tonic-reflection" ];
diff --git a/tvix/castore/Cargo.toml b/tvix/castore/Cargo.toml
index 8f96c3fbf6ad..4dab3bfa4469 100644
--- a/tvix/castore/Cargo.toml
+++ b/tvix/castore/Cargo.toml
@@ -34,7 +34,8 @@ tonic-build = "0.10.2"
 [dev-dependencies]
 test-case = "2.2.2"
 tempfile = "3.3.0"
+tokio-retry = "0.3.0"
 
 [features]
 default = []
-tonic-reflection = ["dep:tonic-reflection"]
\ No newline at end of file
+tonic-reflection = ["dep:tonic-reflection"]
diff --git a/tvix/castore/src/blobservice/grpc.rs b/tvix/castore/src/blobservice/grpc.rs
index b0544387bb0a..115efa5f09c6 100644
--- a/tvix/castore/src/blobservice/grpc.rs
+++ b/tvix/castore/src/blobservice/grpc.rs
@@ -281,10 +281,12 @@ impl<W: tokio::io::AsyncWrite + Unpin> tokio::io::AsyncWrite for GRPCBlobWriter<
 #[cfg(test)]
 mod tests {
     use std::sync::Arc;
+    use std::time::Duration;
 
     use tempfile::TempDir;
     use tokio::net::UnixListener;
-    use tokio::time;
+    use tokio_retry::strategy::ExponentialBackoff;
+    use tokio_retry::Retry;
     use tokio_stream::wrappers::UnixListenerStream;
 
     use crate::blobservice::MemoryBlobService;
@@ -374,22 +376,18 @@ mod tests {
         });
 
         // wait for the socket to be created
-        {
-            let mut socket_created = false;
-            // TODO: exponential backoff urgently
-            for _try in 1..20 {
+        Retry::spawn(
+            ExponentialBackoff::from_millis(20).max_delay(Duration::from_secs(10)),
+            || async {
                 if socket_path.exists() {
-                    socket_created = true;
-                    break;
+                    Ok(())
+                } else {
+                    Err(())
                 }
-                tokio::time::sleep(time::Duration::from_millis(20)).await;
-            }
-
-            assert!(
-                socket_created,
-                "expected socket path to eventually get created, but never happened"
-            );
-        }
+            },
+        )
+        .await
+        .expect("failed to wait for socket");
 
         // prepare a client
         let grpc_client = {