From fe0450cb00008ed2b962fd21c0d47393a4309cad Mon Sep 17 00:00:00 2001 From: Christopher Berner Date: Sat, 24 Aug 2024 09:47:41 -0700 Subject: [PATCH] Add test for multithreaded reads of a Table --- tests/multithreading_tests.rs | 44 ++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/tests/multithreading_tests.rs b/tests/multithreading_tests.rs index e87ec628..eb9a6855 100644 --- a/tests/multithreading_tests.rs +++ b/tests/multithreading_tests.rs @@ -1,6 +1,6 @@ #[cfg(not(target_os = "wasi"))] mod multithreading_test { - use redb::{Database, ReadableTableMetadata, TableDefinition}; + use redb::{Database, ReadableTable, ReadableTableMetadata, TableDefinition}; use std::sync::Arc; use std::thread; @@ -70,4 +70,46 @@ mod multithreading_test { let table = read_txn.open_table(DEF2).unwrap(); assert_eq!(table.len().unwrap(), 2); } + + #[test] + fn multithreaded_re_read() { + let tmpfile = create_tempfile(); + let db = Database::create(tmpfile.path()).unwrap(); + + const DEF1: TableDefinition<&str, &str> = TableDefinition::new("x"); + const DEF2: TableDefinition<&str, &str> = TableDefinition::new("y"); + const DEF3: TableDefinition<&str, &str> = TableDefinition::new("z"); + let write_txn = db.begin_write().unwrap(); + { + let mut table1 = write_txn.open_table(DEF1).unwrap(); + let mut table2 = write_txn.open_table(DEF2).unwrap(); + let mut table3 = write_txn.open_table(DEF3).unwrap(); + table1.insert("hello", "world").unwrap(); + + thread::scope(|s| { + s.spawn(|| { + let value = table1.get("hello").unwrap().unwrap(); + table2.insert("hello2", value.value()).unwrap(); + }); + }); + thread::scope(|s| { + s.spawn(|| { + let value = table1.get("hello").unwrap().unwrap(); + table3.insert("hello2", value.value()).unwrap(); + }); + }); + + assert_eq!(table2.get("hello2").unwrap().unwrap().value(), "world"); + assert_eq!(table3.get("hello2").unwrap().unwrap().value(), "world"); + } + write_txn.commit().unwrap(); + + let read_txn = db.begin_read().unwrap(); + let table = read_txn.open_table(DEF1).unwrap(); + assert_eq!(table.len().unwrap(), 1); + let table = read_txn.open_table(DEF2).unwrap(); + assert_eq!(table.len().unwrap(), 1); + let table = read_txn.open_table(DEF3).unwrap(); + assert_eq!(table.len().unwrap(), 1); + } }