From b53a019b07bde3662cb85dd6ae31959124854677 Mon Sep 17 00:00:00 2001 From: Tamo Date: Tue, 30 Jul 2024 10:47:58 +0200 Subject: [PATCH] fix the initialization problem over the shared indexes with documents --- meilisearch/tests/common/mod.rs | 44 +++++++++++++------------------ meilisearch/tests/index/errors.rs | 2 +- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/meilisearch/tests/common/mod.rs b/meilisearch/tests/common/mod.rs index 58f531b6d..972c9de60 100644 --- a/meilisearch/tests/common/mod.rs +++ b/meilisearch/tests/common/mod.rs @@ -12,6 +12,7 @@ use once_cell::sync::Lazy; use serde::{Deserialize, Serialize}; #[allow(unused)] pub use server::{default_settings, Server}; +use tokio::sync::OnceCell; use crate::common::index::Index; @@ -192,25 +193,21 @@ pub static DOCUMENTS: Lazy = Lazy::new(|| { }); pub async fn shared_index_with_documents() -> &'static Index<'static, Shared> { - // We cannot store a `Shared` index directly because we need to do more initialization work later on - static INDEX: Lazy> = Lazy::new(|| { + static INDEX: OnceCell> = OnceCell::const_new(); + INDEX.get_or_init(|| async { let server = Server::new_shared(); - server._index("SHARED_DOCUMENTS").to_shared() - }); - let index = Lazy::get(&INDEX); - // That means the lazy has never been initialized, we need to create the index and index the documents - if index.is_none() { + let index = server._index("SHARED_DOCUMENTS").to_shared(); let documents = DOCUMENTS.clone(); - let (response, _code) = INDEX._add_documents(documents, None).await; - INDEX.wait_task(response.uid()).await.succeeded(); - let (response, _code) = INDEX + let (response, _code) = index._add_documents(documents, None).await; + index.wait_task(response.uid()).await.succeeded(); + let (response, _code) = index ._update_settings( json!({"filterableAttributes": ["id", "title"], "sortableAttributes": ["id", "title"]}), ) .await; - INDEX.wait_task(response.uid()).await.succeeded(); - } - &INDEX + index.wait_task(response.uid()).await.succeeded(); + index + }).await } pub static SCORE_DOCUMENTS: Lazy = Lazy::new(|| { @@ -298,24 +295,21 @@ pub static NESTED_DOCUMENTS: Lazy = Lazy::new(|| { }); pub async fn shared_index_with_nested_documents() -> &'static Index<'static, Shared> { - static INDEX: Lazy> = Lazy::new(|| { + static INDEX: OnceCell> = OnceCell::const_new(); + INDEX.get_or_init(|| async { let server = Server::new_shared(); - server._index("SHARED_NESTED_DOCUMENTS").to_shared() - }); - let index = Lazy::get(&INDEX); - // That means the lazy has never been initialized, we need to create the index and index the documents - if index.is_none() { + let index = server._index("SHARED_NESTED_DOCUMENTS").to_shared(); let documents = NESTED_DOCUMENTS.clone(); - let (response, _code) = INDEX._add_documents(documents, None).await; - INDEX.wait_task(response.uid()).await.succeeded(); - let (response, _code) = INDEX + let (response, _code) = index._add_documents(documents, None).await; + index.wait_task(response.uid()).await.succeeded(); + let (response, _code) = index ._update_settings( json!({"filterableAttributes": ["father", "doggos"], "sortableAttributes": ["doggos"]}), ) .await; - INDEX.wait_task(response.uid()).await.succeeded(); - } - &INDEX + index.wait_task(response.uid()).await.succeeded(); + index + }).await } pub static FRUITS_DOCUMENTS: Lazy = Lazy::new(|| { diff --git a/meilisearch/tests/index/errors.rs b/meilisearch/tests/index/errors.rs index 2b422d685..9c677ee12 100644 --- a/meilisearch/tests/index/errors.rs +++ b/meilisearch/tests/index/errors.rs @@ -1,6 +1,6 @@ use meili_snap::*; -use crate::common::{shared_does_not_exists_index, Server}; +use crate::common::Server; use crate::json; #[actix_rt::test]