From 800101733822d4396a8ef2cfa1ec2fece0ee9a11 Mon Sep 17 00:00:00 2001 From: John Smith Date: Fri, 26 May 2023 20:39:35 +0100 Subject: [PATCH] more table store work for password protecting encryption key, also fix unit tests hang in routing table test --- .../src/routing_table/tests/test_serialize.rs | 44 ++++++++++--------- veilid-core/src/table_store/table_store.rs | 24 ++++++++++ .../src/tests/common/test_veilid_config.rs | 2 +- 3 files changed, 49 insertions(+), 21 deletions(-) diff --git a/veilid-core/src/routing_table/tests/test_serialize.rs b/veilid-core/src/routing_table/tests/test_serialize.rs index 3290de9a..14e07930 100644 --- a/veilid-core/src/routing_table/tests/test_serialize.rs +++ b/veilid-core/src/routing_table/tests/test_serialize.rs @@ -41,31 +41,35 @@ pub async fn test_routingtable_buckets_round_trip() { ) .unwrap(); - let original_inner = &*original.inner.read(); - let copy_inner = &*copy.inner.read(); + // Wrap to close lifetime of 'inner' which is borrowed here so terminate() can succeed + // (it also .write() locks routing table inner) + { + let original_inner = &*original.inner.read(); + let copy_inner = &*copy.inner.read(); - let routing_table_keys: Vec<_> = original_inner.buckets.keys().clone().collect(); - let copy_keys: Vec<_> = copy_inner.buckets.keys().clone().collect(); + let routing_table_keys: Vec<_> = original_inner.buckets.keys().clone().collect(); + let copy_keys: Vec<_> = copy_inner.buckets.keys().clone().collect(); - assert_eq!(routing_table_keys.len(), copy_keys.len()); + assert_eq!(routing_table_keys.len(), copy_keys.len()); - for crypto in routing_table_keys { - // The same keys are present in the original and copy RoutingTables. - let original_buckets = original_inner.buckets.get(&crypto).unwrap(); - let copy_buckets = copy_inner.buckets.get(&crypto).unwrap(); + for crypto in routing_table_keys { + // The same keys are present in the original and copy RoutingTables. + let original_buckets = original_inner.buckets.get(&crypto).unwrap(); + let copy_buckets = copy_inner.buckets.get(&crypto).unwrap(); - // Recurse into RoutingTable.inner.buckets - for (left_buckets, right_buckets) in original_buckets.iter().zip(copy_buckets.iter()) { - // Recurse into RoutingTable.inner.buckets.entries - for ((left_crypto, left_entries), (right_crypto, right_entries)) in - left_buckets.entries().zip(right_buckets.entries()) - { - assert_eq!(left_crypto, right_crypto); + // Recurse into RoutingTable.inner.buckets + for (left_buckets, right_buckets) in original_buckets.iter().zip(copy_buckets.iter()) { + // Recurse into RoutingTable.inner.buckets.entries + for ((left_crypto, left_entries), (right_crypto, right_entries)) in + left_buckets.entries().zip(right_buckets.entries()) + { + assert_eq!(left_crypto, right_crypto); - assert_eq!( - format!("{:?}", left_entries), - format!("{:?}", right_entries) - ); + assert_eq!( + format!("{:?}", left_entries), + format!("{:?}", right_entries) + ); + } } } } diff --git a/veilid-core/src/table_store/table_store.rs b/veilid-core/src/table_store/table_store.rs index cd35dacb..05703849 100644 --- a/veilid-core/src/table_store/table_store.rs +++ b/veilid-core/src/table_store/table_store.rs @@ -421,6 +421,15 @@ impl TableStore { /// existing TableDB's column count, the database will be upgraded to add the missing columns pub async fn open(&self, name: &str, column_count: u32) -> VeilidAPIResult { let _async_guard = self.async_lock.lock().await; + + // If we aren't initialized yet, bail + { + let inner = self.inner.lock(); + if inner.all_tables_db.is_none() { + apibail_not_initialized!(); + } + } + let table_name = self.name_get_or_create(name).await?; // See if this table is already opened @@ -477,6 +486,14 @@ impl TableStore { /// Delete a TableDB table by name pub async fn delete(&self, name: &str) -> VeilidAPIResult { let _async_guard = self.async_lock.lock().await; + // If we aren't initialized yet, bail + { + let inner = self.inner.lock(); + if inner.all_tables_db.is_none() { + apibail_not_initialized!(); + } + } + let Some(table_name) = self.name_get(name).await? else { // Did not exist in name table return Ok(false); @@ -510,6 +527,13 @@ impl TableStore { /// Rename a TableDB table pub async fn rename(&self, old_name: &str, new_name: &str) -> VeilidAPIResult<()> { let _async_guard = self.async_lock.lock().await; + // If we aren't initialized yet, bail + { + let inner = self.inner.lock(); + if inner.all_tables_db.is_none() { + apibail_not_initialized!(); + } + } trace!("TableStore::rename {} -> {}", old_name, new_name); self.name_rename(old_name, new_name).await } diff --git a/veilid-core/src/tests/common/test_veilid_config.rs b/veilid-core/src/tests/common/test_veilid_config.rs index c8458d43..f7951d6d 100644 --- a/veilid-core/src/tests/common/test_veilid_config.rs +++ b/veilid-core/src/tests/common/test_veilid_config.rs @@ -296,7 +296,7 @@ pub async fn test_config() { } let inner = vc.get(); - assert_eq!(inner.program_name, String::from("Veilid")); + assert_eq!(inner.program_name, String::from("VeilidCoreTests")); assert_eq!(inner.namespace, String::from("")); assert_eq!(inner.capabilities.protocol_udp, true); assert_eq!(inner.capabilities.protocol_connect_tcp, true);