sl@0: # 2004 September 2 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file implements regression tests for SQLite library. The sl@0: # focus of this script testing the callback-free C/C++ API and in sl@0: # particular the behavior of sqlite3_step() when trying to commit sl@0: # with lock contention. sl@0: # sl@0: # $Id: capi3b.test,v 1.4 2007/08/10 19:46:14 drh Exp $ sl@0: # sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: sl@0: # These tests depend on the pager holding changes in cache sl@0: # until it is time to commit. But that won't happen if the sl@0: # soft-heap-limit is set too low. So disable the soft heap limit sl@0: # for the duration of this test. sl@0: # sl@0: sqlite3_soft_heap_limit 0 sl@0: sl@0: sl@0: set DB [sqlite3_connection_pointer db] sl@0: sqlite3 db2 test.db sl@0: set DB2 [sqlite3_connection_pointer db2] sl@0: sl@0: # Create some data in the database sl@0: # sl@0: do_test capi3b-1.1 { sl@0: execsql { sl@0: CREATE TABLE t1(x); sl@0: INSERT INTO t1 VALUES(1); sl@0: INSERT INTO t1 VALUES(2); sl@0: SELECT * FROM t1 sl@0: } sl@0: } {1 2} sl@0: sl@0: # Make sure the second database connection can see the data sl@0: # sl@0: do_test capi3b-1.2 { sl@0: execsql { sl@0: SELECT * FROM t1 sl@0: } db2 sl@0: } {1 2} sl@0: sl@0: # First database connection acquires a shared lock sl@0: # sl@0: do_test capi3b-1.3 { sl@0: execsql { sl@0: BEGIN; sl@0: SELECT * FROM t1; sl@0: } sl@0: } {1 2} sl@0: sl@0: # Second database connection tries to write. The sqlite3_step() sl@0: # function returns SQLITE_BUSY because it cannot commit. sl@0: # sl@0: do_test capi3b-1.4 { sl@0: set VM [sqlite3_prepare $DB2 {INSERT INTO t1 VALUES(3)} -1 TAIL] sl@0: sqlite3_step $VM sl@0: } SQLITE_BUSY sl@0: sl@0: # The sqlite3_step call can be repeated multiple times. sl@0: # sl@0: do_test capi3b-1.5.1 { sl@0: sqlite3_step $VM sl@0: } SQLITE_BUSY sl@0: do_test capi3b-1.5.2 { sl@0: sqlite3_step $VM sl@0: } SQLITE_BUSY sl@0: sl@0: # The first connection closes its transaction. This allows the second sl@0: # connections sqlite3_step to succeed. sl@0: # sl@0: do_test capi3b-1.6 { sl@0: execsql COMMIT sl@0: sqlite3_step $VM sl@0: } SQLITE_DONE sl@0: do_test capi3b-1.7 { sl@0: sqlite3_finalize $VM sl@0: } SQLITE_OK sl@0: do_test capi3b-1.8 { sl@0: execsql {SELECT * FROM t1} db2 sl@0: } {1 2 3} sl@0: do_test capi3b-1.9 { sl@0: execsql {SELECT * FROM t1} sl@0: } {1 2 3} sl@0: sl@0: # Start doing a SELECT with one connection. This gets a SHARED lock. sl@0: # Then do an INSERT with the other connection. The INSERT should sl@0: # not be able to complete until the SELECT finishes. sl@0: # sl@0: do_test capi3b-2.1 { sl@0: set VM1 [sqlite3_prepare $DB {SELECT * FROM t1} -1 TAIL] sl@0: sqlite3_step $VM1 sl@0: } SQLITE_ROW sl@0: do_test capi3b-2.2 { sl@0: sqlite3_column_text $VM1 0 sl@0: } 1 sl@0: do_test capi3b-2.3 { sl@0: set VM2 [sqlite3_prepare $DB2 {INSERT INTO t1 VALUES(4)} -1 TAIL] sl@0: sqlite3_step $VM2 sl@0: } SQLITE_BUSY sl@0: do_test capi3b-2.4 { sl@0: sqlite3_step $VM1 sl@0: } SQLITE_ROW sl@0: do_test capi3b-2.5 { sl@0: sqlite3_column_text $VM1 0 sl@0: } 2 sl@0: do_test capi3b-2.6 { sl@0: sqlite3_step $VM2 sl@0: } SQLITE_BUSY sl@0: do_test capi3b-2.7 { sl@0: sqlite3_step $VM1 sl@0: } SQLITE_ROW sl@0: do_test capi3b-2.8 { sl@0: sqlite3_column_text $VM1 0 sl@0: } 3 sl@0: do_test capi3b-2.9 { sl@0: sqlite3_step $VM2 sl@0: } SQLITE_BUSY sl@0: do_test capi3b-2.10 { sl@0: sqlite3_step $VM1 sl@0: } SQLITE_DONE sl@0: do_test capi3b-2.11 { sl@0: sqlite3_step $VM2 sl@0: } SQLITE_DONE sl@0: do_test capi3b-2.12 { sl@0: sqlite3_finalize $VM1 sl@0: sqlite3_finalize $VM2 sl@0: execsql {SELECT * FROM t1} sl@0: } {1 2 3 4} sl@0: sl@0: catch {db2 close} sl@0: sl@0: sqlite3_soft_heap_limit $soft_limit sl@0: finish_test