diff options
author | Florian Klink <flokli@flokli.de> | 2023-10-08T11·24+0200 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2023-10-08T22·47+0000 |
commit | c0376995c9ac4e4b9ca606219035e808fe815d69 (patch) | |
tree | 5390bdf67f36e3d7df7c3f59307efe658082f736 | |
parent | b196cbbc677ec79c9669997f55615bdbf7bbbba5 (diff) |
refactor(tvix/store): simplify test_valid_unix_path_ping_pong r/6741
We don't need to spawn two tokio runtimes anymore, and can do the URL parsing at once, too. Change-Id: I7885a894bb1250cd087d4e1893e3e73b631331da Reviewed-on: https://cl.tvl.fyi/c/depot/+/9567 Autosubmit: flokli <flokli@flokli.de> Tested-by: BuildkiteCI Reviewed-by: Connor Brewster <cbrewster@hey.com>
-rw-r--r-- | tvix/store/src/pathinfoservice/grpc.rs | 74 |
1 files changed, 31 insertions, 43 deletions
diff --git a/tvix/store/src/pathinfoservice/grpc.rs b/tvix/store/src/pathinfoservice/grpc.rs index 6883c56104a6..a8cd1c0c2d80 100644 --- a/tvix/store/src/pathinfoservice/grpc.rs +++ b/tvix/store/src/pathinfoservice/grpc.rs @@ -180,7 +180,6 @@ impl PathInfoService for GRPCPathInfoService { #[cfg(test)] mod tests { use std::sync::Arc; - use std::thread; use tempfile::TempDir; use tokio::net::UnixListener; @@ -269,58 +268,40 @@ mod tests { ); } - /// This uses the correct scheme for a unix socket, and provides a server on the other side. + /// This ensures connecting via gRPC works as expected. #[tokio::test] async fn test_valid_unix_path_ping_pong() { let tmpdir = TempDir::new().unwrap(); - let path = tmpdir.path().join("daemon"); + let socket_path = tmpdir.path().join("daemon"); // let mut join_set = JoinSet::new(); - // prepare a client - let client = { - let mut url = url::Url::parse("grpc+unix:///path/to/somewhere").expect("must parse"); - url.set_path(path.to_str().unwrap()); - GRPCPathInfoService::from_url(&url, gen_blob_service(), gen_directory_service()) - .expect("must succeed") - }; - - let path_copy = path.clone(); - - // Spin up a server, in a thread far away, which spawns its own tokio runtime, - // and blocks on the task. - thread::spawn(move || { - // Create the runtime - let rt = tokio::runtime::Runtime::new().unwrap(); - // Get a handle from this runtime - let handle = rt.handle(); - - let task = handle.spawn(async { - let uds = UnixListener::bind(path_copy).unwrap(); - let uds_stream = UnixListenerStream::new(uds); - - // spin up a new server - let mut server = tonic::transport::Server::builder(); - let router = server.add_service( - crate::proto::path_info_service_server::PathInfoServiceServer::new( - GRPCPathInfoServiceWrapper::from(Arc::new(MemoryPathInfoService::new( - gen_blob_service(), - gen_directory_service(), - )) - as Arc<dyn PathInfoService>), - ), - ); - router.serve_with_incoming(uds_stream).await - }); - - handle.block_on(task) + let path_copy = socket_path.clone(); + + // Spin up a server + tokio::spawn(async { + let uds = UnixListener::bind(path_copy).unwrap(); + let uds_stream = UnixListenerStream::new(uds); + + // spin up a new server + let mut server = tonic::transport::Server::builder(); + let router = server.add_service( + crate::proto::path_info_service_server::PathInfoServiceServer::new( + GRPCPathInfoServiceWrapper::from(Arc::new(MemoryPathInfoService::new( + gen_blob_service(), + gen_directory_service(), + )) + as Arc<dyn PathInfoService>), + ), + ); + router.serve_with_incoming(uds_stream).await }); // wait for the socket to be created { let mut socket_created = false; for _try in 1..20 { - if path.exists() { + if socket_path.exists() { socket_created = true; break; } @@ -332,12 +313,19 @@ mod tests { "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())) + .expect("must parse"); + GRPCPathInfoService::from_url(&url, gen_blob_service(), gen_directory_service()) + .expect("must succeed") + }; - let pi = client + let path_info = grpc_client .get(fixtures::DUMMY_OUTPUT_HASH.to_vec().try_into().unwrap()) .await .expect("must not be error"); - assert!(pi.is_none()); + assert!(path_info.is_none()); } } |