mirror of
https://github.com/GothenburgBitFactory/taskchampion-sync-server.git
synced 2025-06-26 10:54:29 +02:00

This is a bit tricky because the `Storage` trait is `Send + Sync`, but an SQLite connection is neither. Since this is not intended for large-scale use, the simple solution is just to open a new SQLite connection for each transaction. More complex alternatives include thread-local connection pools or a "worker thread" that owns the connection and communicates with other threads via channels. This also updates the InMemoryStorage implementation to be a bit more strict about transactional integrity, which led to a number of test changes.
69 lines
2.1 KiB
Rust
69 lines
2.1 KiB
Rust
use std::thread;
|
|
use taskchampion_sync_server_core::{Storage, NIL_VERSION_ID};
|
|
use taskchampion_sync_server_storage_sqlite::SqliteStorage;
|
|
use tempfile::TempDir;
|
|
use uuid::Uuid;
|
|
|
|
/// Test that calls to `add_version` from different threads maintain sequential consistency.
|
|
#[test]
|
|
fn add_version_concurrency() -> anyhow::Result<()> {
|
|
let tmp_dir = TempDir::new()?;
|
|
let client_id = Uuid::new_v4();
|
|
|
|
{
|
|
let con = SqliteStorage::new(tmp_dir.path())?;
|
|
let mut txn = con.txn()?;
|
|
txn.new_client(client_id, NIL_VERSION_ID)?;
|
|
txn.commit()?;
|
|
}
|
|
|
|
const N: i32 = 100;
|
|
const T: i32 = 4;
|
|
|
|
// Add N versions to the DB.
|
|
let add_versions = || {
|
|
let con = SqliteStorage::new(tmp_dir.path())?;
|
|
|
|
for _ in 0..N {
|
|
let mut txn = con.txn()?;
|
|
let client = txn.get_client(client_id)?.unwrap();
|
|
let version_id = Uuid::new_v4();
|
|
let parent_version_id = client.latest_version_id;
|
|
std::thread::yield_now(); // Make failure more likely.
|
|
txn.add_version(client_id, version_id, parent_version_id, b"data".to_vec())?;
|
|
txn.commit()?;
|
|
}
|
|
|
|
Ok::<_, anyhow::Error>(())
|
|
};
|
|
|
|
thread::scope(|s| {
|
|
// Spawn T threads.
|
|
for _ in 0..T {
|
|
s.spawn(add_versions);
|
|
}
|
|
});
|
|
|
|
// There should now be precisely N*T versions. This number will be smaller if there were
|
|
// concurrent transactions, which would have allowed two `add_version` calls with the
|
|
// same `parent_version_id`.
|
|
{
|
|
let con = SqliteStorage::new(tmp_dir.path())?;
|
|
let mut txn = con.txn()?;
|
|
let client = txn.get_client(client_id)?.unwrap();
|
|
|
|
let mut n = 0;
|
|
let mut version_id = client.latest_version_id;
|
|
while version_id != NIL_VERSION_ID {
|
|
let version = txn
|
|
.get_version(client_id, version_id)?
|
|
.expect("version should exist");
|
|
n += 1;
|
|
version_id = version.parent_version_id;
|
|
}
|
|
|
|
assert_eq!(n, N * T);
|
|
}
|
|
|
|
Ok(())
|
|
}
|