From 5fd76aa54b0a4cf19b0964cb2aae6e1f4c0c460d Mon Sep 17 00:00:00 2001 From: gwenn Date: Fri, 22 Sep 2017 23:27:05 +0200 Subject: [PATCH] Unlock notification Test added --- src/raw_statement.rs | 4 ++-- src/unlock_notify.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/raw_statement.rs b/src/raw_statement.rs index b92e122..08d6873 100644 --- a/src/raw_statement.rs +++ b/src/raw_statement.rs @@ -35,8 +35,8 @@ impl RawStatement { loop { rc = unsafe { ffi::sqlite3_step(self.0) }; if rc == ffi::SQLITE_LOCKED { - if unsafe { ffi::sqlite3_extended_errcode(self.1) } != - ffi::SQLITE_LOCKED_SHAREDCACHE + if unsafe { ffi::sqlite3_extended_errcode(self.1) } + != ffi::SQLITE_LOCKED_SHAREDCACHE { break; } diff --git a/src/unlock_notify.rs b/src/unlock_notify.rs index 9676dd3..724358f 100644 --- a/src/unlock_notify.rs +++ b/src/unlock_notify.rs @@ -82,3 +82,36 @@ pub fn wait_for_unlock_notify(db: *mut ffi::sqlite3) -> c_int { pub fn wait_for_unlock_notify(_db: *mut ffi::sqlite3) -> c_int { unreachable!() } + +#[cfg(feature = "unlock_notify")] +#[cfg(test)] +mod test { + use std::sync::mpsc::sync_channel; + use std::thread; + use std::time; + use {Connection, Result, Transaction, TransactionBehavior, SQLITE_OPEN_READ_WRITE, + SQLITE_OPEN_URI}; + + #[test] + fn test_unlock_notify() { + let url = "file::memory:?cache=shared"; + let flags = SQLITE_OPEN_READ_WRITE | SQLITE_OPEN_URI; + let db1 = Connection::open_with_flags(url, flags).unwrap(); + db1.execute_batch("CREATE TABLE foo (x)").unwrap(); + let (sender, receiver) = sync_channel(0); + let sender2 = sender.clone(); + let child = thread::spawn(move || { + let mut db2 = Connection::open_with_flags(url, flags).unwrap(); + let tx2 = Transaction::new(&mut db2, TransactionBehavior::Immediate).unwrap(); + tx2.execute_batch("INSERT INTO foo VALUES (42)").unwrap(); + sender2.send(1).unwrap(); + let ten_millis = time::Duration::from_millis(10); + thread::sleep(ten_millis); + tx2.commit().unwrap(); + }); + assert_eq!(receiver.recv().unwrap(), 1); + let the_answer: Result = db1.query_row("SELECT x FROM foo", &[], |r| r.get(0)); + assert_eq!(42i64, the_answer.unwrap()); + child.join().unwrap(); + } +}