mirror of
https://github.com/isar/rusqlite.git
synced 2025-03-31 19:12:58 +08:00
Merge pull request #1480 from gwenn/test_busy_handler
Fix test_busy_handler
This commit is contained in:
commit
e66397b410
43
src/busy.rs
43
src/busy.rs
@ -132,38 +132,27 @@ mod test {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore] // FIXME: unstable
|
||||
fn test_busy_handler() {
|
||||
fn test_busy_handler() -> Result<()> {
|
||||
static CALLED: AtomicBool = AtomicBool::new(false);
|
||||
fn busy_handler(_: i32) -> bool {
|
||||
CALLED.store(true, Ordering::Relaxed);
|
||||
thread::sleep(Duration::from_millis(100));
|
||||
true
|
||||
fn busy_handler(n: i32) -> bool {
|
||||
if n > 2 {
|
||||
false
|
||||
} else {
|
||||
CALLED.swap(true, Ordering::Relaxed)
|
||||
}
|
||||
}
|
||||
|
||||
let temp_dir = tempfile::tempdir().unwrap();
|
||||
let path = temp_dir.path().join("test.db3");
|
||||
let path = temp_dir.path().join("busy-handler.db3");
|
||||
|
||||
let db2 = Connection::open(&path).unwrap();
|
||||
db2.busy_handler(Some(busy_handler)).unwrap();
|
||||
|
||||
let (rx, tx) = sync_channel(0);
|
||||
let child = thread::spawn(move || {
|
||||
let mut db1 = Connection::open(&path).unwrap();
|
||||
let tx1 = db1
|
||||
.transaction_with_behavior(TransactionBehavior::Exclusive)
|
||||
.unwrap();
|
||||
rx.send(1).unwrap();
|
||||
thread::sleep(Duration::from_millis(100));
|
||||
tx1.rollback().unwrap();
|
||||
});
|
||||
|
||||
assert_eq!(tx.recv().unwrap(), 1);
|
||||
let _ = db2
|
||||
.query_row("PRAGMA schema_version", [], |row| row.get::<_, i32>(0))
|
||||
.expect("unexpected error");
|
||||
let db1 = Connection::open(&path)?;
|
||||
db1.execute_batch("CREATE TABLE IF NOT EXISTS t(a)")?;
|
||||
let db2 = Connection::open(&path)?;
|
||||
db2.busy_handler(Some(busy_handler))?;
|
||||
db1.execute_batch("BEGIN EXCLUSIVE")?;
|
||||
let err = db2.prepare("SELECT * FROM t").unwrap_err();
|
||||
assert_eq!(err.sqlite_error_code(), Some(ErrorCode::DatabaseBusy));
|
||||
assert!(CALLED.load(Ordering::Relaxed));
|
||||
|
||||
child.join().unwrap();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user