#![cfg(feature = "std")]
use graphitesql::vfs::memory::MemoryVfs;
use graphitesql::{Connection, Error, Value};
#[test]
fn second_writer_is_busy_during_open_write_txn() {
let vfs = MemoryVfs::new();
let mut a = Connection::create_vfs(&vfs, "db", 4096).unwrap();
a.execute("CREATE TABLE t(x)").unwrap();
let mut b = Connection::open_vfs(&vfs, "db").unwrap();
a.execute("BEGIN").unwrap();
a.execute("INSERT INTO t VALUES (1)").unwrap();
let err = b.execute("INSERT INTO t VALUES (2)").unwrap_err();
assert!(matches!(err, Error::Busy), "expected Busy, got {err:?}");
a.execute("COMMIT").unwrap();
b.execute("INSERT INTO t VALUES (3)").unwrap();
let rows = a.query("SELECT x FROM t ORDER BY x").unwrap().rows;
assert_eq!(rows, vec![vec![Value::Integer(1)], vec![Value::Integer(3)]]);
}
#[test]
fn writer_lock_released_after_autocommit() {
let vfs = MemoryVfs::new();
let mut a = Connection::create_vfs(&vfs, "db", 4096).unwrap();
a.execute("CREATE TABLE t(x)").unwrap();
a.execute("INSERT INTO t VALUES (1)").unwrap();
let mut b = Connection::open_vfs(&vfs, "db").unwrap();
b.execute("INSERT INTO t VALUES (2)").unwrap();
assert_eq!(
b.query("SELECT count(*) FROM t").unwrap().rows[0][0],
Value::Integer(2)
);
}
#[test]
fn rolled_back_writer_frees_the_lock() {
let vfs = MemoryVfs::new();
let mut a = Connection::create_vfs(&vfs, "db", 4096).unwrap();
a.execute("CREATE TABLE t(x)").unwrap();
let mut b = Connection::open_vfs(&vfs, "db").unwrap();
a.execute("BEGIN").unwrap();
a.execute("INSERT INTO t VALUES (1)").unwrap();
assert!(matches!(
b.execute("INSERT INTO t VALUES (2)"),
Err(Error::Busy)
));
a.execute("ROLLBACK").unwrap();
b.execute("INSERT INTO t VALUES (2)").unwrap();
assert_eq!(
b.query("SELECT x FROM t").unwrap().rows,
vec![vec![Value::Integer(2)]]
);
}