From a464088d2e3cc9bb0ff7a432d592157a811ef6c8 Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Sun, 8 Oct 2023 13:40:20 +0200 Subject: test(tvix/store): use tokio-retry for exp backoff Rather than using this loop, use exponential backoff while waiting for the socket path to be created. Change-Id: I3056b1525784cd712b1d81f84876c9ca0be10dc6 Reviewed-on: https://cl.tvl.fyi/c/depot/+/9569 Autosubmit: flokli Reviewed-by: Connor Brewster Tested-by: BuildkiteCI --- tvix/Cargo.lock | 1 + tvix/Cargo.nix | 4 ++++ tvix/store/Cargo.toml | 1 + tvix/store/src/pathinfoservice/grpc.rs | 32 +++++++++++++++----------------- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/tvix/Cargo.lock b/tvix/Cargo.lock index 5a9c290a7a39..e5e4870c3de7 100644 --- a/tvix/Cargo.lock +++ b/tvix/Cargo.lock @@ -2818,6 +2818,7 @@ dependencies = [ "thiserror", "tokio", "tokio-listener", + "tokio-retry", "tokio-stream", "tokio-util", "tonic", diff --git a/tvix/Cargo.nix b/tvix/Cargo.nix index 68ec9b545279..868620b76ad2 100644 --- a/tvix/Cargo.nix +++ b/tvix/Cargo.nix @@ -8634,6 +8634,10 @@ rec { name = "test-case"; packageId = "test-case"; } + { + name = "tokio-retry"; + packageId = "tokio-retry"; + } ]; features = { "default" = [ "fuse" "tonic-reflection" ]; diff --git a/tvix/store/Cargo.toml b/tvix/store/Cargo.toml index e4ebfc9a0f0f..43421ef4a302 100644 --- a/tvix/store/Cargo.toml +++ b/tvix/store/Cargo.toml @@ -77,6 +77,7 @@ tonic-build = "0.10.2" [dev-dependencies] test-case = "2.2.2" tempfile = "3.3.0" +tokio-retry = "0.3.0" [features] default = ["fuse", "tonic-reflection"] diff --git a/tvix/store/src/pathinfoservice/grpc.rs b/tvix/store/src/pathinfoservice/grpc.rs index a8cd1c0c2d80..a88828083940 100644 --- a/tvix/store/src/pathinfoservice/grpc.rs +++ b/tvix/store/src/pathinfoservice/grpc.rs @@ -180,10 +180,12 @@ impl PathInfoService for GRPCPathInfoService { #[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::pathinfoservice::MemoryPathInfoService; @@ -274,13 +276,11 @@ mod tests { let tmpdir = TempDir::new().unwrap(); let socket_path = tmpdir.path().join("daemon"); - // let mut join_set = JoinSet::new(); - - let path_copy = socket_path.clone(); + let path_clone = socket_path.clone(); // Spin up a server tokio::spawn(async { - let uds = UnixListener::bind(path_copy).unwrap(); + let uds = UnixListener::bind(path_clone).unwrap(); let uds_stream = UnixListenerStream::new(uds); // spin up a new server @@ -298,21 +298,19 @@ mod tests { }); // wait for the socket to be created - { - let mut socket_created = false; - 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; - } + }, + ) + .await + .expect("failed to wait for socket"); - assert!( - socket_created, - "expected socket path to eventually get created, but never happened" - ); - } // prepare a client let grpc_client = { let url = url::Url::parse(&format!("grpc+unix://{}", socket_path.display())) -- cgit 1.4.1