fix various bugs

This commit is contained in:
mpostma 2021-03-12 00:37:43 +01:00
parent 7d9637861f
commit e4d45b0500
No known key found for this signature in database
GPG Key ID: CBC8A7C1D7A28C3A
7 changed files with 27 additions and 65 deletions

View File

@ -343,6 +343,7 @@ impl<S: IndexStore + Sync + Send> IndexActor<S> {
async fn handle_get_meta(&self, uuid: Uuid) -> Result<Option<IndexMeta>> { async fn handle_get_meta(&self, uuid: Uuid) -> Result<Option<IndexMeta>> {
match self.store.get(uuid).await? { match self.store.get(uuid).await? {
Some(index) => { Some(index) => {
println!("geting meta yoyo");
let meta = spawn_blocking(move || IndexMeta::new(&index)) let meta = spawn_blocking(move || IndexMeta::new(&index))
.await .await
.map_err(|e| IndexError::Error(e.into()))??; .map_err(|e| IndexError::Error(e.into()))??;

View File

@ -131,7 +131,16 @@ impl IndexController {
pub async fn update_settings(&self, uid: String, settings: Settings, create: bool) -> anyhow::Result<UpdateStatus> { pub async fn update_settings(&self, uid: String, settings: Settings, create: bool) -> anyhow::Result<UpdateStatus> {
let uuid = if create { let uuid = if create {
self.uuid_resolver.get_or_create(uid).await? let uuid = self.uuid_resolver.get_or_create(uid).await?;
// We need to create the index upfront, since it would otherwise only be created when
// the update is processed. This would make calls to GET index to fail until the update
// is complete. Since this is get or create, we ignore the error when the index already
// exists.
match self.index_handle.create_index(uuid.clone(), None).await {
Ok(_) | Err(index_actor::IndexError::IndexAlreadyExists) => (),
Err(e) => return Err(e.into()),
}
uuid
} else { } else {
self.uuid_resolver.resolve(uid).await? self.uuid_resolver.resolve(uid).await?
}; };

View File

@ -270,7 +270,7 @@ impl UuidStore for HeedUuidStore {
let uuid = Uuid::from_slice(uuid)?; let uuid = Uuid::from_slice(uuid)?;
db.delete(&mut txn, &name)?; db.delete(&mut txn, &name)?;
txn.commit()?; txn.commit()?;
Ok(None) Ok(Some(uuid))
} }
None => Ok(None) None => Ok(None)
} }
@ -289,57 +289,6 @@ impl UuidStore for HeedUuidStore {
entries.push((name.to_owned(), uuid)) entries.push((name.to_owned(), uuid))
} }
Ok(entries) Ok(entries)
}).await? } }).await?
}
#[cfg(test)]
mod test {
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::sync::Arc;
use tokio::sync::RwLock;
use super::*;
struct MapUuidStore(Arc<RwLock<HashMap<String, Uuid>>>);
#[async_trait::async_trait]
impl UuidStore for MapUuidStore {
async fn create_uuid(&self, name: String, err: bool) -> Result<Uuid> {
match self.0.write().await.entry(name) {
Entry::Occupied(entry) => {
if err {
Err(UuidError::NameAlreadyExist)
} else {
Ok(entry.get().clone())
}
}
Entry::Vacant(entry) => {
let uuid = Uuid::new_v4();
let uuid = entry.insert(uuid);
Ok(uuid.clone())
}
}
}
async fn get_uuid(&self, name: String) -> Result<Option<Uuid>> {
Ok(self.0.read().await.get(&name).cloned())
}
async fn delete(&self, name: String) -> Result<Option<Uuid>> {
Ok(self.0.write().await.remove(&name))
}
async fn list(&self) -> Result<Vec<(String, Uuid)>> {
let list = self
.0
.read()
.await
.iter()
.map(|(name, uuid)| (name.to_owned(), uuid.clone()))
.collect();
Ok(list)
}
} }
} }

View File

@ -70,5 +70,5 @@ async fn test_create_multiple_indexes() {
assert_eq!(index1.get().await.1, 200); assert_eq!(index1.get().await.1, 200);
assert_eq!(index2.get().await.1, 200); assert_eq!(index2.get().await.1, 200);
assert_eq!(index3.get().await.1, 200); assert_eq!(index3.get().await.1, 200);
assert_eq!(index4.get().await.1, 400); assert_eq!(index4.get().await.1, 404);
} }

View File

@ -6,13 +6,17 @@ async fn create_and_delete_index() {
let index = server.index("test"); let index = server.index("test");
let (_response, code) = index.create(None).await; let (_response, code) = index.create(None).await;
println!("response: {}", _response);
assert_eq!(code, 200); assert_eq!(code, 200);
let (_response, code) = index.delete().await; let (_response, code) = index.delete().await;
println!("response: {}", _response);
assert_eq!(code, 200); assert_eq!(code, 200);
assert_eq!(index.get().await.1, 400); assert_eq!(index.get().await.1, 404);
} }
#[actix_rt::test] #[actix_rt::test]

View File

@ -13,12 +13,11 @@ async fn create_and_get_index() {
assert_eq!(code, 200); assert_eq!(code, 200);
assert_eq!(response["uid"], "test"); assert_eq!(response["uid"], "test");
assert!(response.get("uuid").is_some());
assert!(response.get("createdAt").is_some()); assert!(response.get("createdAt").is_some());
assert!(response.get("updatedAt").is_some()); assert!(response.get("updatedAt").is_some());
assert_eq!(response["createdAt"], response["updatedAt"]); assert_eq!(response["createdAt"], response["updatedAt"]);
assert_eq!(response["primaryKey"], Value::Null); assert_eq!(response["primaryKey"], Value::Null);
assert_eq!(response.as_object().unwrap().len(), 5); assert_eq!(response.as_object().unwrap().len(), 4);
} }
// TODO: partial test since we are testing error, amd error is not yet fully implemented in // TODO: partial test since we are testing error, amd error is not yet fully implemented in
@ -30,7 +29,7 @@ async fn get_unexisting_index() {
let (_response, code) = index.get().await; let (_response, code) = index.get().await;
assert_eq!(code, 400); assert_eq!(code, 404);
} }
#[actix_rt::test] #[actix_rt::test]
@ -55,5 +54,4 @@ async fn list_multiple_indexes() {
assert_eq!(arr.len(), 2); assert_eq!(arr.len(), 2);
assert!(arr.iter().find(|entry| entry["uid"] == "test" && entry["primaryKey"] == Value::Null).is_some()); assert!(arr.iter().find(|entry| entry["uid"] == "test" && entry["primaryKey"] == Value::Null).is_some());
assert!(arr.iter().find(|entry| entry["uid"] == "test1" && entry["primaryKey"] == "key").is_some()); assert!(arr.iter().find(|entry| entry["uid"] == "test1" && entry["primaryKey"] == "key").is_some());
} }

View File

@ -43,7 +43,8 @@ async fn test_partial_update() {
assert_eq!(response["displayedAttributes"],json!(["foo"])); assert_eq!(response["displayedAttributes"],json!(["foo"]));
assert_eq!(response["searchableAttributes"],json!(["*"])); assert_eq!(response["searchableAttributes"],json!(["*"]));
index.update_settings(json!({"searchableAttributes": ["bar"]})).await; let (response, _) = index.update_settings(json!({"searchableAttributes": ["bar"]})).await;
println!("resp: {}", response);
index.wait_update_id(1).await; index.wait_update_id(1).await;
let (response, code) = index.settings().await; let (response, code) = index.settings().await;
@ -125,10 +126,10 @@ macro_rules! test_setting_routes {
.chars() .chars()
.map(|c| if c == '_' { '-' } else { c }) .map(|c| if c == '_' { '-' } else { c })
.collect::<String>()); .collect::<String>());
let (_response, code) = server.service.post(url, serde_json::Value::Null).await; let (response, code) = server.service.post(url, serde_json::Value::Null).await;
assert_eq!(code, 200); assert_eq!(code, 200, "{}", response);
let (_response, code) = server.index("test").get().await; let (response, code) = server.index("test").get().await;
assert_eq!(code, 200); assert_eq!(code, 200, "{}", response);
} }
#[actix_rt::test] #[actix_rt::test]