refactor(tvix/store): simplify test_valid_unix_path_ping_pong

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>
This commit is contained in:
Florian Klink 2023-10-08 13:24:44 +02:00 committed by clbot
parent b196cbbc67
commit c0376995c9

View file

@ -180,7 +180,6 @@ impl PathInfoService for GRPCPathInfoService {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::sync::Arc; use std::sync::Arc;
use std::thread;
use tempfile::TempDir; use tempfile::TempDir;
use tokio::net::UnixListener; 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] #[tokio::test]
async fn test_valid_unix_path_ping_pong() { async fn test_valid_unix_path_ping_pong() {
let tmpdir = TempDir::new().unwrap(); let tmpdir = TempDir::new().unwrap();
let path = tmpdir.path().join("daemon"); let socket_path = tmpdir.path().join("daemon");
// let mut join_set = JoinSet::new(); // let mut join_set = JoinSet::new();
// prepare a client let path_copy = socket_path.clone();
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
tokio::spawn(async {
let uds = UnixListener::bind(path_copy).unwrap();
let uds_stream = UnixListenerStream::new(uds);
// Spin up a server, in a thread far away, which spawns its own tokio runtime, // spin up a new server
// and blocks on the task. let mut server = tonic::transport::Server::builder();
thread::spawn(move || { let router = server.add_service(
// Create the runtime crate::proto::path_info_service_server::PathInfoServiceServer::new(
let rt = tokio::runtime::Runtime::new().unwrap(); GRPCPathInfoServiceWrapper::from(Arc::new(MemoryPathInfoService::new(
// Get a handle from this runtime gen_blob_service(),
let handle = rt.handle(); gen_directory_service(),
))
let task = handle.spawn(async { as Arc<dyn PathInfoService>),
let uds = UnixListener::bind(path_copy).unwrap(); ),
let uds_stream = UnixListenerStream::new(uds); );
router.serve_with_incoming(uds_stream).await
// 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)
}); });
// wait for the socket to be created // wait for the socket to be created
{ {
let mut socket_created = false; let mut socket_created = false;
for _try in 1..20 { for _try in 1..20 {
if path.exists() { if socket_path.exists() {
socket_created = true; socket_created = true;
break; break;
} }
@ -332,12 +313,19 @@ mod tests {
"expected socket path to eventually get created, but never happened" "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()) .get(fixtures::DUMMY_OUTPUT_HASH.to_vec().try_into().unwrap())
.await .await
.expect("must not be error"); .expect("must not be error");
assert!(pi.is_none()); assert!(path_info.is_none());
} }
} }