chore: rustfmt, clippy lints and code tidying
This commit is contained in:
1 parent
3716c3e698
commit
1752753fcc
19 files changed
+1273
-738
No files matched your search
+23
-13
@@ -22,13 +22,11 @@ impl<T: DeserializeOwned + Default + Serialize + Clone + Send + 'static> Store<T
|
||||
|
||||
fn load_from_db(key: &str, db: &Database) -> T {
|
||||
let read_txn = db.begin_read().unwrap();
|
||||
if let Ok(table) = read_txn.open_table(STORE_TABLE) {
|
||||
if let Ok(Some(value)) = table.get(key) {
|
||||
if let Ok(parsed) = serde_json::from_slice::<T>(value.value()) {
|
||||
if let Ok(table) = read_txn.open_table(STORE_TABLE)
|
||||
&& let Ok(Some(value)) = table.get(key)
|
||||
&& let Ok(parsed) = serde_json::from_slice::<T>(value.value()) {
|
||||
return parsed;
|
||||
}
|
||||
}
|
||||
}
|
||||
T::default()
|
||||
}
|
||||
|
||||
@@ -74,7 +72,7 @@ mod tests {
|
||||
async fn test_store_read_write() {
|
||||
let temp_file = NamedTempFile::new().unwrap();
|
||||
let db = Database::create(temp_file.path()).unwrap();
|
||||
|
||||
|
||||
let write_txn = db.begin_write().unwrap();
|
||||
{
|
||||
write_txn.open_table(STORE_TABLE).unwrap();
|
||||
@@ -94,18 +92,30 @@ mod tests {
|
||||
// Need to wait for spawn_blocking to finish
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
|
||||
|
||||
assert_eq!(store.read(), TestData { name: "Hello".to_string(), value: 42 });
|
||||
assert_eq!(
|
||||
store.read(),
|
||||
TestData {
|
||||
name: "Hello".to_string(),
|
||||
value: 42
|
||||
}
|
||||
);
|
||||
|
||||
// Load again to verify persistence
|
||||
let store2 = Store::<TestData>::new("test_key", db.clone());
|
||||
assert_eq!(store2.read(), TestData { name: "Hello".to_string(), value: 42 });
|
||||
assert_eq!(
|
||||
store2.read(),
|
||||
TestData {
|
||||
name: "Hello".to_string(),
|
||||
value: 42
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||
async fn test_store_concurrency() {
|
||||
let temp_file = NamedTempFile::new().unwrap();
|
||||
let db = Database::create(temp_file.path()).unwrap();
|
||||
|
||||
|
||||
let write_txn = db.begin_write().unwrap();
|
||||
{
|
||||
write_txn.open_table(STORE_TABLE).unwrap();
|
||||
@@ -114,7 +124,7 @@ mod tests {
|
||||
|
||||
let db = Arc::new(db);
|
||||
let store = Arc::new(Store::<TestData>::new("concurrent_key", db.clone()));
|
||||
|
||||
|
||||
let mut handles = vec![];
|
||||
for _ in 0..50 {
|
||||
let s = store.clone();
|
||||
@@ -124,14 +134,14 @@ mod tests {
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
for h in handles {
|
||||
h.await.unwrap();
|
||||
}
|
||||
|
||||
|
||||
// Wait for all blocking writes to flush
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;
|
||||
|
||||
|
||||
assert_eq!(store.read().value, 50);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user