simple write multi threaded test

This commit is contained in:
Pere Diaz Bou
2025-07-21 11:06:31 +02:00
parent e3fdb6bab9
commit c55cb74dc8
2 changed files with 41 additions and 3 deletions

View File

@@ -1,3 +1,5 @@
use std::sync::Arc;
use crate::common::TempDatabase;
#[test]
@@ -25,3 +27,39 @@ fn test_schema_change() {
};
println!("{:?} {:?}", row.get_value(0), row.get_value(1));
}
#[test]
fn test_create_multiple_connections() -> anyhow::Result<()> {
let tries = 10;
for _ in 0..tries {
let tmp_db = Arc::new(TempDatabase::new_empty(false));
{
let conn = tmp_db.connect_limbo();
conn.execute("CREATE TABLE t(x)").unwrap();
}
let mut threads = Vec::new();
for i in 0..10 {
let tmp_db_ = tmp_db.clone();
threads.push(std::thread::spawn(move || {
let conn = tmp_db_.connect_limbo();
conn.execute(format!("INSERT INTO t VALUES ({i})").as_str())
.unwrap();
}));
}
for thread in threads {
thread.join().unwrap();
}
let conn = tmp_db.connect_limbo();
let mut stmt = conn.prepare("SELECT * FROM t").unwrap();
let mut rows = Vec::new();
while matches!(stmt.step().unwrap(), turso_core::StepResult::Row) {
let row = stmt.row().unwrap();
rows.push(row.get::<i64>(0).unwrap());
}
rows.sort();
assert_eq!(rows, (0..10).collect::<Vec<_>>());
}
Ok(())
}