diff options
author | Florian Klink <flokli@flokli.de> | 2024-03-23T20·49+0100 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2024-03-24T20·00+0000 |
commit | 3ece32bbf9078f44d2c38098697e9a1cfaebd00c (patch) | |
tree | 8738fceb954cfca6df706632a22a36a1f3175bce /tvix/castore/src/directoryservice/tests/utils.rs | |
parent | 6f5474bf028045cc3cb64eff04cf80aef7e22412 (diff) |
feat(tvix/castore): add rstest-based DirectoryService tests r/7777
This creates test scenarios (using the DirectoryService trait) that we want all DirectoryService implementations to pass. Some of these tests are ported from proto::tests::grpc_directoryservice, which tested this on the gRPC interface (rather than the trait), some others ensure certain behaviour for which we only recently introduced general checking logic (through ClosureValidator). We also borrow some code related to setting up a gRPC DirectoryService client (connecting to a server exposing a in-memory DiretoryService) from castore::utils, this will be deleted once it's all ported over. Change-Id: I6810215a76101f908e2aaecafa803c70d85bc552 Reviewed-on: https://cl.tvl.fyi/c/depot/+/11247 Reviewed-by: raitobezarius <tvl@lahfa.xyz> Autosubmit: flokli <flokli@flokli.de> Reviewed-by: Connor Brewster <cbrewster@hey.com> Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/castore/src/directoryservice/tests/utils.rs')
-rw-r--r-- | tvix/castore/src/directoryservice/tests/utils.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/tvix/castore/src/directoryservice/tests/utils.rs b/tvix/castore/src/directoryservice/tests/utils.rs new file mode 100644 index 000000000000..72a3ff754d19 --- /dev/null +++ b/tvix/castore/src/directoryservice/tests/utils.rs @@ -0,0 +1,45 @@ +use crate::directoryservice::{DirectoryService, GRPCDirectoryService}; +use crate::proto::directory_service_client::DirectoryServiceClient; +use crate::proto::GRPCDirectoryServiceWrapper; +use crate::{ + directoryservice::MemoryDirectoryService, + proto::directory_service_server::DirectoryServiceServer, +}; +use tonic::transport::{Endpoint, Server, Uri}; + +/// Constructs and returns a gRPC DirectoryService. +/// The server part is a [MemoryDirectoryService], exposed via the +/// [GRPCDirectoryServiceWrapper], and connected through a DuplexStream. +pub async fn make_grpc_directory_service_client() -> Box<dyn DirectoryService> { + let (left, right) = tokio::io::duplex(64); + + // spin up a server, which will only connect once, to the left side. + tokio::spawn(async { + let directory_service = + Box::<MemoryDirectoryService>::default() as Box<dyn DirectoryService>; + + let mut server = Server::builder(); + let router = server.add_service(DirectoryServiceServer::new( + GRPCDirectoryServiceWrapper::new(directory_service), + )); + + router + .serve_with_incoming(tokio_stream::once(Ok::<_, std::io::Error>(left))) + .await + }); + + // Create a client, connecting to the right side. The URI is unused. + let mut maybe_right = Some(right); + Box::new(GRPCDirectoryService::from_client( + DirectoryServiceClient::new( + Endpoint::try_from("http://[::]:50051") + .unwrap() + .connect_with_connector(tower::service_fn(move |_: Uri| { + let right = maybe_right.take().unwrap(); + async move { Ok::<_, std::io::Error>(right) } + })) + .await + .unwrap(), + ), + )) +} |