From c55cb74dc8f4c2159f9d93e55c0dd3a3b05b0450 Mon Sep 17 00:00:00 2001 From: Pere Diaz Bou Date: Mon, 21 Jul 2025 11:06:31 +0200 Subject: [PATCH] simple write multi threaded test --- .../query_processing/test_multi_thread.rs | 38 +++++++++++++++++++ .../query_processing/test_write_path.rs | 6 +-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/tests/integration/query_processing/test_multi_thread.rs b/tests/integration/query_processing/test_multi_thread.rs index 1d96e0422..13d4a8608 100644 --- a/tests/integration/query_processing/test_multi_thread.rs +++ b/tests/integration/query_processing/test_multi_thread.rs @@ -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::(0).unwrap()); + } + rows.sort(); + assert_eq!(rows, (0..10).collect::>()); + } + Ok(()) +} diff --git a/tests/integration/query_processing/test_write_path.rs b/tests/integration/query_processing/test_write_path.rs index f7605323e..b5c4e0b51 100644 --- a/tests/integration/query_processing/test_write_path.rs +++ b/tests/integration/query_processing/test_write_path.rs @@ -765,11 +765,11 @@ fn test_read_wal_dumb_no_frames() -> anyhow::Result<()> { Ok(()) } -fn run_query(tmp_db: &TempDatabase, conn: &Arc, query: &str) -> anyhow::Result<()> { +pub fn run_query(tmp_db: &TempDatabase, conn: &Arc, query: &str) -> anyhow::Result<()> { run_query_core(tmp_db, conn, query, None::) } -fn run_query_on_row( +pub fn run_query_on_row( tmp_db: &TempDatabase, conn: &Arc, query: &str, @@ -778,7 +778,7 @@ fn run_query_on_row( run_query_core(tmp_db, conn, query, Some(on_row)) } -fn run_query_core( +pub fn run_query_core( _tmp_db: &TempDatabase, conn: &Arc, query: &str,