Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for multithreaded reads of a Table #848

Merged
merged 1 commit into from
Aug 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading