refactor(tvix/castore): 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: I38ab96978cb7f8c31ded2726262e0b1366655094
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9566
Tested-by: BuildkiteCI
Reviewed-by: Connor Brewster <cbrewster@hey.com>
Autosubmit: flokli <flokli@flokli.de>
This commit is contained in:
Florian Klink 2023-10-08 13:15:30 +02:00 committed by clbot
parent 269ab866f1
commit b196cbbc67

View file

@ -281,7 +281,6 @@ impl<W: tokio::io::AsyncWrite + Unpin> tokio::io::AsyncWrite for GRPCBlobWriter<
#[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;
@ -350,23 +349,16 @@ mod tests {
assert!(GRPCBlobService::from_url(&url).is_err()); assert!(GRPCBlobService::from_url(&url).is_err());
} }
/// 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.
/// This is not a tokio::test, because spawn two separate tokio runtimes and #[tokio::test]
// want to have explicit control. async fn test_valid_unix_path_ping_pong() {
#[test]
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 path_clone = path.clone(); let path_clone = socket_path.clone();
// Spin up a server, in a thread far away, which spawns its own tokio runtime, // Spin up a server
// and blocks on the task. tokio::spawn(async {
thread::spawn(move || {
// Create the runtime
let rt = tokio::runtime::Runtime::new().unwrap();
let task = rt.spawn(async {
let uds = UnixListener::bind(path_clone).unwrap(); let uds = UnixListener::bind(path_clone).unwrap();
let uds_stream = UnixListenerStream::new(uds); let uds_stream = UnixListenerStream::new(uds);
@ -381,19 +373,12 @@ mod tests {
router.serve_with_incoming(uds_stream).await router.serve_with_incoming(uds_stream).await
}); });
rt.block_on(task).unwrap().unwrap();
});
// Now create another tokio runtime which we'll use in the main test code.
let rt = tokio::runtime::Runtime::new().unwrap();
let task = rt.spawn(async move {
// wait for the socket to be created // wait for the socket to be created
{ {
let mut socket_created = false; let mut socket_created = false;
// TODO: exponential backoff urgently // TODO: exponential backoff urgently
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;
} }
@ -407,20 +392,17 @@ mod tests {
} }
// prepare a client // prepare a client
let client = { let grpc_client = {
let mut url = let url = url::Url::parse(&format!("grpc+unix://{}", socket_path.display()))
url::Url::parse("grpc+unix:///path/to/somewhere").expect("must parse"); .expect("must parse");
url.set_path(path.to_str().unwrap());
GRPCBlobService::from_url(&url).expect("must succeed") GRPCBlobService::from_url(&url).expect("must succeed")
}; };
let has = client let has = grpc_client
.has(&fixtures::BLOB_A_DIGEST) .has(&fixtures::BLOB_A_DIGEST)
.await .await
.expect("must not be err"); .expect("must not be err");
assert!(!has); assert!(!has);
});
rt.block_on(task).unwrap()
} }
} }