use std::time::Duration;
use pliantdb_core::{
connection::{AccessPolicy, Connection},
test_util::{
Basic, BasicByBrokenParentId, BasicByParentId, BasicCollectionWithNoViews,
BasicCollectionWithOnlyBrokenParentId, ConnectionTest, TestDirectory,
},
};
use super::*;
use crate::Storage;
struct TestHarness {
_directory: TestDirectory,
db: Storage<Basic>,
}
impl TestHarness {
pub async fn new(test: ConnectionTest) -> anyhow::Result<Self> {
let directory = TestDirectory::new(test.to_string());
let db = Storage::<Basic>::open_local(&directory, &Configuration::default()).await?;
Ok(Self {
_directory: directory,
db,
})
}
pub async fn connect(&self) -> anyhow::Result<Storage<Basic>> {
Ok(self.db.clone())
}
}
pliantdb_core::define_connection_test_suite!(TestHarness);
#[tokio::test(flavor = "multi_thread")]
async fn integrity_checks() -> anyhow::Result<()> {
let path = TestDirectory::new("integrity-checks");
{
let db =
Storage::<BasicCollectionWithNoViews>::open_local(&path, &Configuration::default())
.await?;
let collection = db.collection::<BasicCollectionWithNoViews>();
collection.push(&Basic::default().with_parent_id(1)).await?;
}
{
let db = Storage::<BasicCollectionWithOnlyBrokenParentId>::open_local(
&path,
&Configuration::default(),
)
.await?;
tokio::time::sleep(Duration::from_millis(100)).await;
assert_eq!(
db.view::<BasicByBrokenParentId>()
.with_access_policy(AccessPolicy::NoUpdate)
.query()
.await?
.len(),
0
);
assert_eq!(db.view::<BasicByBrokenParentId>().query().await?.len(), 1);
}
{
let db = Storage::<Basic>::open_local(
&path,
&Configuration {
views: config::Views {
check_integrity_on_open: true,
},
..Configuration::default()
},
)
.await?;
for _ in 0_u8..10 {
tokio::time::sleep(Duration::from_millis(20)).await;
if db
.view::<BasicByParentId>()
.with_access_policy(AccessPolicy::NoUpdate)
.with_key(Some(1))
.query()
.await?
.len()
== 1
{
return Ok(());
}
}
panic!("Integrity checker didn't run in the allocated time")
}
}