Skip to content

Commit

Permalink
Add test for multithreaded reads of a Table
Browse files Browse the repository at this point in the history
  • Loading branch information
cberner committed Aug 24, 2024
1 parent 7a35233 commit 64217bb
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion tests/multithreading_tests.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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);
}
}

0 comments on commit 64217bb

Please sign in to comment.