From 7d0c8a33797ca13815be5ff1f63ad9be7727c863 Mon Sep 17 00:00:00 2001 From: Tamo Date: Thu, 15 Sep 2022 12:23:41 +0200 Subject: [PATCH] test the register tasks --- Cargo.lock | 1 + index-scheduler/Cargo.toml | 1 + index-scheduler/src/index_scheduler.rs | 78 ++++++++++++++++++++++++++ index-scheduler/src/lib.rs | 19 +++++++ index-scheduler/src/task.rs | 6 +- 5 files changed, 102 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5495075e0..e08525829 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1781,6 +1781,7 @@ name = "index-scheduler" version = "0.1.0" dependencies = [ "anyhow", + "big_s", "bincode", "csv", "file-store", diff --git a/index-scheduler/Cargo.toml b/index-scheduler/Cargo.toml index d6080ca4b..dd5d2b5f2 100644 --- a/index-scheduler/Cargo.toml +++ b/index-scheduler/Cargo.toml @@ -24,3 +24,4 @@ synchronoise = "1.0.1" [dev-dependencies] nelson = { git = "https://github.com/meilisearch/nelson.git", rev = "675f13885548fb415ead8fbb447e9e6d9314000a"} insta = "1.19.1" +big_s = "1.0.2" diff --git a/index-scheduler/src/index_scheduler.rs b/index-scheduler/src/index_scheduler.rs index 752808d88..88424fac3 100644 --- a/index-scheduler/src/index_scheduler.rs +++ b/index-scheduler/src/index_scheduler.rs @@ -412,7 +412,12 @@ impl IndexScheduler { #[cfg(test)] mod tests { + use big_s::S; + use insta::assert_debug_snapshot; use tempfile::TempDir; + use uuid::Uuid; + + use crate::assert_smol_debug_snapshot; use super::*; @@ -432,4 +437,77 @@ mod tests { fn simple_new() { new(); } + + #[test] + fn register() { + let index_scheduler = new(); + let kinds = [ + KindWithContent::IndexCreation { + index_uid: S("catto"), + primary_key: Some(S("mouse")), + }, + KindWithContent::DocumentAddition { + index_uid: S("catto"), + primary_key: None, + content_file: Uuid::new_v4(), + documents_count: 12, + allow_index_creation: true, + }, + KindWithContent::CancelTask { tasks: vec![0, 1] }, + KindWithContent::DocumentAddition { + index_uid: S("catto"), + primary_key: None, + content_file: Uuid::new_v4(), + documents_count: 50, + allow_index_creation: true, + }, + KindWithContent::DocumentAddition { + index_uid: S("doggo"), + primary_key: Some(S("bone")), + content_file: Uuid::new_v4(), + documents_count: 5000, + allow_index_creation: true, + }, + ]; + let mut inserted_tasks = Vec::new(); + for (idx, kind) in kinds.into_iter().enumerate() { + let k = kind.as_kind(); + let task = index_scheduler.register(kind).unwrap(); + + assert_eq!(task.uid, idx as u32); + assert_eq!(task.status, Status::Enqueued); + assert_eq!(task.kind, k); + + inserted_tasks.push(task); + } + + let rtxn = index_scheduler.env.read_txn().unwrap(); + let mut all_tasks = Vec::new(); + for ret in index_scheduler.all_tasks.iter(&rtxn).unwrap() { + all_tasks.push(ret.unwrap()); + } + + assert_smol_debug_snapshot!(all_tasks, @r###"[(U32(0), Task { uid: 0, enqueued_at: OffsetDateTime { local_datetime: PrimitiveDateTime { date: Date { year: 2022, ordinal: 258 }, time: Time { hour: 10, minute: 20, second: 33, nanosecond: 531850695 } }, offset: UtcOffset { hours: 0, minutes: 0, seconds: 0 } }, started_at: None, finished_at: None, error: None, details: None, status: Enqueued, kind: IndexCreation { index_uid: "catto", primary_key: Some("mouse") } }), (U32(1), Task { uid: 1, enqueued_at: OffsetDateTime { local_datetime: PrimitiveDateTime { date: Date { year: 2022, ordinal: 258 }, time: Time { hour: 10, minute: 20, second: 33, nanosecond: 531928625 } }, offset: UtcOffset { hours: 0, minutes: 0, seconds: 0 } }, started_at: None, finished_at: None, error: None, details: None, status: Enqueued, kind: DocumentAddition { index_uid: "catto", primary_key: None, content_file: c0d2f89e-a2ea-4357-a3ea-8c3135d1e9c7, documents_count: 12, allow_index_creation: true } }), (U32(2), Task { uid: 2, enqueued_at: OffsetDateTime { local_datetime: PrimitiveDateTime { date: Date { year: 2022, ordinal: 258 }, time: Time { hour: 10, minute: 20, second: 33, nanosecond: 531966226 } }, offset: UtcOffset { hours: 0, minutes: 0, seconds: 0 } }, started_at: None, finished_at: None, error: None, details: None, status: Enqueued, kind: CancelTask { tasks: [0, 1] } }), (U32(3), Task { uid: 3, enqueued_at: OffsetDateTime { local_datetime: PrimitiveDateTime { date: Date { year: 2022, ordinal: 258 }, time: Time { hour: 10, minute: 20, second: 33, nanosecond: 531997016 } }, offset: UtcOffset { hours: 0, minutes: 0, seconds: 0 } }, started_at: None, finished_at: None, error: None, details: None, status: Enqueued, kind: DocumentAddition { index_uid: "catto", primary_key: None, content_file: 84aa6582-645c-4347-abbe-32cb85d488a8, documents_count: 50, allow_index_creation: true } }), (U32(4), Task { uid: 4, enqueued_at: OffsetDateTime { local_datetime: PrimitiveDateTime { date: Date { year: 2022, ordinal: 258 }, time: Time { hour: 10, minute: 20, second: 33, nanosecond: 532027497 } }, offset: UtcOffset { hours: 0, minutes: 0, seconds: 0 } }, started_at: None, finished_at: None, error: None, details: None, status: Enqueued, kind: DocumentAddition { index_uid: "doggo", primary_key: Some("bone"), content_file: 4335f1d6-0cab-4474-beef-5d564f23f5a0, documents_count: 5000, allow_index_creation: true } })]"###); + + let mut status = Vec::new(); + for ret in index_scheduler.status.iter(&rtxn).unwrap() { + status.push(ret.unwrap()); + } + + assert_smol_debug_snapshot!(status, @"[(Enqueued, RoaringBitmap<[0, 1, 2, 3, 4]>)]"); + + let mut kind = Vec::new(); + for ret in index_scheduler.kind.iter(&rtxn).unwrap() { + kind.push(ret.unwrap()); + } + + assert_smol_debug_snapshot!(kind, @"[(DocumentAddition, RoaringBitmap<[1, 3, 4]>), (IndexCreation, RoaringBitmap<[0]>), (CancelTask, RoaringBitmap<[2]>)]"); + + let mut index_tasks = Vec::new(); + for ret in index_scheduler.index_tasks.iter(&rtxn).unwrap() { + index_tasks.push(ret.unwrap()); + } + + assert_smol_debug_snapshot!(index_tasks, @r###"[("catto", RoaringBitmap<[0, 1, 3]>), ("doggo", RoaringBitmap<[4]>)]"###); + } } diff --git a/index-scheduler/src/lib.rs b/index-scheduler/src/lib.rs index 3503c1ca9..ff9ad3470 100644 --- a/index-scheduler/src/lib.rs +++ b/index-scheduler/src/lib.rs @@ -15,3 +15,22 @@ pub use error::Error; pub use task::KindWithContent as TaskKind; /// from the exterior you don't need to know there is multiple type of `Task` pub use task::TaskView as Task; + +#[cfg(test)] +mod tests { + #[macro_export] + macro_rules! assert_smol_debug_snapshot { + ($value:expr, @$snapshot:literal) => {{ + let value = format!("{:?}", $value); + insta::assert_snapshot!(value, stringify!($value), @$snapshot); + }}; + ($name:expr, $value:expr) => {{ + let value = format!("{:?}", $value); + insta::assert_snapshot!(Some($name), value, stringify!($value)); + }}; + ($value:expr) => {{ + let value = format!("{:?}", $value); + insta::assert_snapshot!($crate::_macro_support::AutoName, value, stringify!($value)); + }}; +} +} diff --git a/index-scheduler/src/task.rs b/index-scheduler/src/task.rs index 6d51d33cb..e2c0253f2 100644 --- a/index-scheduler/src/task.rs +++ b/index-scheduler/src/task.rs @@ -17,7 +17,7 @@ pub enum Status { Failed, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Error { message: String, @@ -27,7 +27,7 @@ pub struct Error { link: String, } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct TaskView { pub uid: TaskId, @@ -269,7 +269,7 @@ pub enum Kind { Snapshot, } -#[derive(Debug, Clone, Serialize, Deserialize)] +#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)] #[serde(untagged)] #[allow(clippy::large_enum_variant)] pub enum Details {