test(tvix/composition): add recursion and concurrent tests

Change-Id: Ic10773b08d940b45c3067bd514c3080cadac9606
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12003
Autosubmit: yuka <yuka@yuka.dev>
Reviewed-by: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
This commit is contained in:
Yureka 2024-07-21 15:19:46 +02:00 committed by yuka
parent eea9f3d9ee
commit e408783bac

View file

@ -476,3 +476,66 @@ impl Composition {
}
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::blobservice::BlobService;
use std::sync::Arc;
/// Test that we return a reference to the same instance of MemoryBlobService (via ptr_eq)
/// when instantiating the same entrypoint twice. By instantiating concurrently, we also
/// test the channels notifying the second consumer when the store has been instantiated.
#[tokio::test]
async fn concurrent() {
let blob_services_configs_json = serde_json::json!({
"default": {
"type": "memory",
}
});
let blob_services_configs =
with_registry(&REG, || serde_json::from_value(blob_services_configs_json)).unwrap();
let mut blob_service_composition = Composition::default();
blob_service_composition.extend_with_configs::<dyn BlobService>(blob_services_configs);
let (blob_service1, blob_service2) = tokio::join!(
blob_service_composition.build::<dyn BlobService>("default"),
blob_service_composition.build::<dyn BlobService>("default")
);
assert!(Arc::ptr_eq(
&blob_service1.unwrap(),
&blob_service2.unwrap()
));
}
/// Test that we throw the correct error when an instantiation would recurse (deadlock)
#[tokio::test]
async fn reject_recursion() {
let blob_services_configs_json = serde_json::json!({
"default": {
"type": "combined",
"local": "other",
"remote": "other"
},
"other": {
"type": "combined",
"local": "default",
"remote": "default"
}
});
let blob_services_configs =
with_registry(&REG, || serde_json::from_value(blob_services_configs_json)).unwrap();
let mut blob_service_composition = Composition::default();
blob_service_composition.extend_with_configs::<dyn BlobService>(blob_services_configs);
match blob_service_composition
.build::<dyn BlobService>("default")
.await
{
Err(CompositionError::Recursion(stack)) => {
assert_eq!(stack, vec!["default".to_string(), "other".to_string()])
}
other => panic!("should have returned an error, returned: {:?}", other.err()),
}
}
}