use super::*;
#[test]
fn test_cached_statement_queries() {
let mut conn = Connection::open_in_memory().unwrap();
migrations::apply_migrations(&mut conn, "content_memory").unwrap();
let mut stmt = conn
.prepare_cached("INSERT OR IGNORE INTO card (hash, content, g_time) VALUES (?1, ?2, ?3)")
.unwrap();
stmt.execute(rusqlite::params!["test_hash_1", "test_content_1", 1000])
.unwrap();
let content = get_card_cached(&conn, "test_hash_1").unwrap();
assert_eq!(content, Some("test_content_1".to_string()));
let exists = card_exists_cached(&conn, "test_hash_1").unwrap();
assert!(exists);
let not_exists = card_exists_cached(&conn, "nonexistent").unwrap();
assert!(!not_exists);
let now = crate::utils::now_iso8601();
let mut stmt_h = conn
.prepare_cached("INSERT OR IGNORE INTO handle_registry (handle, current_hash, updated_at) VALUES (?1, ?2, ?3)")
.unwrap();
stmt_h.execute(params!["test_handle_1", "test_hash_1", &now]).unwrap();
conn.prepare_cached(
"INSERT INTO handle_history (handle, previous_hash, changed_at) VALUES (?1, ?2, ?3)",
)
.unwrap()
.execute(params!["test_handle_1", "test_hash_1", &now])
.unwrap();
let resolved_hash = resolve_handle_hash_cached(&conn, "test_handle_1").unwrap();
assert_eq!(resolved_hash, Some("test_hash_1".to_string()));
let resolved_content = resolve_handle_content_cached(&conn, "test_handle_1").unwrap();
assert_eq!(resolved_content, Some("test_content_1".to_string()));
}
#[cfg(any(feature = "native", feature = "db"))]
#[test]
fn test_concurrent_readers_in_pool() {
let temp_dir = tempfile::tempdir().unwrap();
let clm_root = temp_dir.path().to_str().unwrap();
let pool = DbPillarPool::new("content_memory", clm_root).unwrap();
{
let writer_arc = pool.writer();
let mut conn = writer_arc.lock().unwrap();
let tx = conn.transaction().unwrap();
for i in 0..50 {
let hash = format!("hash_{:04}", i);
let content = format!("content_{:04}", i);
let mut stmt = tx
.prepare_cached("INSERT INTO card (hash, content, g_time) VALUES (?1, ?2, ?3)")
.unwrap();
stmt.execute(rusqlite::params![hash, content, i as i64])
.unwrap();
}
tx.commit().unwrap();
}
let mut handles = Vec::new();
for thread_idx in 0..16 {
let pool_clone = pool.clone();
let h = std::thread::spawn(move || {
let reader = pool_clone.reader().expect("Acquiring reader connection should succeed");
for j in 0..50 {
let card_idx = (thread_idx * 7 + j) % 50;
let target_hash = format!("hash_{:04}", card_idx);
let expected_content = format!("content_{:04}", card_idx);
let content = get_card_cached(&reader, &target_hash)
.expect("Read query should not fail");
assert_eq!(content, Some(expected_content));
}
});
handles.push(h);
}
for h in handles {
h.join().expect("Reader thread should complete without panicking");
}
}
#[tokio::test]
async fn test_query_db_async_isolation() {
let temp_dir = tempfile::tempdir().unwrap();
let clm_root = temp_dir.path().to_str().unwrap();
let pool = DbPillarPool::new("content_memory", clm_root).unwrap();
let pool_writer = pool.clone();
query_db_async(move || {
let writer_arc = pool_writer.writer();
let mut conn = writer_arc.lock().unwrap();
let tx = conn.transaction()?;
{
let mut stmt = tx.prepare_cached("INSERT OR REPLACE INTO card (hash, content, g_time) VALUES (?1, ?2, ?3)")?;
stmt.execute(rusqlite::params!["async_hash", "async_content", 12345])?;
}
tx.commit()?;
Ok(())
})
.await
.unwrap();
let pool_reader = pool.clone();
let res = query_db_async(move || {
let reader = pool_reader.reader()?;
let content = get_card_cached(&reader, "async_hash")?;
Ok(content)
})
.await
.unwrap();
assert_eq!(res, Some("async_content".to_string()));
}