add snapshot tests

This commit is contained in:
mpostma 2021-03-25 10:24:33 +01:00
parent d029464de8
commit 48507460b2
No known key found for this signature in database
GPG Key ID: CBC8A7C1D7A28C3A
2 changed files with 66 additions and 12 deletions

View File

@ -57,9 +57,9 @@ where
async fn perform_snapshot(&self) -> anyhow::Result<()> {
info!("Performing snapshot.");
fs::create_dir_all(&self.snapshot_path).await?;
let temp_snapshot_dir = spawn_blocking(move || tempfile::tempdir_in(".")).await??;
let snapshot_dir = self.snapshot_path.clone();
fs::create_dir_all(&snapshot_dir).await?;
let temp_snapshot_dir = spawn_blocking(move || tempfile::tempdir_in(snapshot_dir)).await??;
let temp_snapshot_path = temp_snapshot_dir.path().to_owned();
let uuids = self
@ -81,14 +81,12 @@ where
futures::future::try_join_all(tasks).await?;
let temp_snapshot_path_clone = temp_snapshot_path.clone();
let snapshot_dir = self.snapshot_path.clone();
let snapshot_path = self.snapshot_path.join(format!("{}.snapshot", self.db_name));
let snapshot_path = spawn_blocking(move || -> anyhow::Result<PathBuf> {
let temp_snapshot_file = tempfile::NamedTempFile::new_in(snapshot_dir)?;
let temp_snapshot_file_path = temp_snapshot_file.path().to_owned();
compression::to_tar_gz(temp_snapshot_path_clone, temp_snapshot_file_path)?;
compression::to_tar_gz(temp_snapshot_path, temp_snapshot_file_path)?;
temp_snapshot_file.persist(&snapshot_path)?;
Ok(snapshot_path)
})
@ -161,12 +159,13 @@ mod test {
.times(uuids_num)
.returning(move |_, _| Box::pin(ok(())));
let snapshot_path = tempfile::NamedTempFile::new_in(".").unwrap();
let snapshot_path = tempfile::tempdir_in(".").unwrap();
let snapshot_service = SnapshotService::new(
uuid_resolver,
update_handle,
Duration::from_millis(100),
snapshot_path.path().to_owned(),
"data.ms".to_string(),
);
snapshot_service.perform_snapshot().await.unwrap();
@ -183,17 +182,18 @@ mod test {
let update_handle = MockUpdateActorHandle::new();
let snapshot_path = tempfile::NamedTempFile::new_in(".").unwrap();
let snapshot_path = tempfile::tempdir_in(".").unwrap();
let snapshot_service = SnapshotService::new(
uuid_resolver,
update_handle,
Duration::from_millis(100),
snapshot_path.path().to_owned(),
"data.ms".to_string(),
);
assert!(snapshot_service.perform_snapshot().await.is_err());
// Nothing was written to the file
assert_eq!(snapshot_path.as_file().metadata().unwrap().len(), 0);
assert!(!snapshot_path.path().join("data.ms.snapshot").exists());
}
#[actix_rt::test]
@ -211,17 +211,18 @@ mod test {
// abitrary error
.returning(|_, _| Box::pin(err(UpdateError::UnexistingUpdate(0))));
let snapshot_path = tempfile::NamedTempFile::new_in(".").unwrap();
let snapshot_path = tempfile::tempdir_in(".").unwrap();
let snapshot_service = SnapshotService::new(
uuid_resolver,
update_handle,
Duration::from_millis(100),
snapshot_path.path().to_owned(),
"data.ms".to_string(),
);
assert!(snapshot_service.perform_snapshot().await.is_err());
// Nothing was written to the file
assert_eq!(snapshot_path.as_file().metadata().unwrap().len(), 0);
assert!(!snapshot_path.path().join("data.ms.snapshot").exists());
}
#[actix_rt::test]
@ -236,12 +237,13 @@ mod test {
let update_handle = MockUpdateActorHandle::new();
let snapshot_path = tempfile::NamedTempFile::new_in(".").unwrap();
let snapshot_path = tempfile::tempdir_in(".").unwrap();
let snapshot_service = SnapshotService::new(
uuid_resolver,
update_handle,
Duration::from_millis(100),
snapshot_path.path().to_owned(),
"data.ms".to_string(),
);
let _ = timeout(Duration::from_millis(300), snapshot_service.run()).await;

View File

@ -0,0 +1,52 @@
use std::time::Duration;
use crate::common::server::default_settings;
use crate::common::GetAllDocumentsOptions;
use crate::common::Server;
use tokio::time::sleep;
use meilisearch_http::Opt;
#[actix_rt::test]
async fn perform_snapshot() {
let temp = tempfile::tempdir_in(".").unwrap();
let snapshot_dir = tempfile::tempdir_in(".").unwrap();
let options = Opt {
snapshot_dir: snapshot_dir.path().to_owned(),
snapshot_interval_sec: 1,
schedule_snapshot: true,
..default_settings(temp.path())
};
let server = Server::new_with_options(options).await;
let index = server.index("test");
index.load_test_set().await;
let (response, _) = index
.get_all_documents(GetAllDocumentsOptions::default())
.await;
sleep(Duration::from_secs(2)).await;
let temp = tempfile::tempdir_in(".").unwrap();
let snapshot_path = snapshot_dir
.path()
.to_owned()
.join(format!("db.snapshot"));
let options = Opt {
import_snapshot: Some(snapshot_path),
..default_settings(temp.path())
};
let server = Server::new_with_options(options).await;
let index = server.index("test");
let (response_from_snapshot, _) = index
.get_all_documents(GetAllDocumentsOptions::default())
.await;
assert_eq!(response, response_from_snapshot);
}