From e408783bacec6a4c3b63341b0764ea05adca3745 Mon Sep 17 00:00:00 2001 From: Yureka Date: Sun, 21 Jul 2024 15:19:46 +0200 Subject: [PATCH] test(tvix/composition): add recursion and concurrent tests Change-Id: Ic10773b08d940b45c3067bd514c3080cadac9606 Reviewed-on: https://cl.tvl.fyi/c/depot/+/12003 Autosubmit: yuka Reviewed-by: flokli Tested-by: BuildkiteCI --- tvix/castore/src/composition.rs | 63 +++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/tvix/castore/src/composition.rs b/tvix/castore/src/composition.rs index 53673cf46..3550695a8 100644 --- a/tvix/castore/src/composition.rs +++ b/tvix/castore/src/composition.rs @@ -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(®, || serde_json::from_value(blob_services_configs_json)).unwrap(); + let mut blob_service_composition = Composition::default(); + blob_service_composition.extend_with_configs::(blob_services_configs); + let (blob_service1, blob_service2) = tokio::join!( + blob_service_composition.build::("default"), + blob_service_composition.build::("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(®, || serde_json::from_value(blob_services_configs_json)).unwrap(); + let mut blob_service_composition = Composition::default(); + blob_service_composition.extend_with_configs::(blob_services_configs); + match blob_service_composition + .build::("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()), + } + } +}