AryaWu/sqlite
0
1# 2010 June 302#3# The author disclaims copyright to this source code. In place of4# a legal notice, here is a blessing:5#6# May you do good and not evil.7# May you find forgiveness for yourself and forgive others.8# May you share freely, never taking more than you give.9#10#***********************************************************************11# This file implements regression tests for SQLite library. The12# focus of this file is testing the sqlite3_unlock_notify() API.13#14 15set testdir [file dirname $argv0]16source $testdir/tester.tcl17 18# This script only runs if shared-cache and unlock-notify are available.19#20ifcapable !unlock_notify||!shared_cache { 21 finish_test 22 return 23}24 25set esc [sqlite3_enable_shared_cache 1]26 27sqlite3 db test.db28forcedelete test.db2 test.db2-journal test.db2-wal29sqlite3 db2 test.db230 31do_test notify3-1.1 {32 execsql { 33 CREATE TABLE t1(a, b); 34 INSERT INTO t1 VALUES('t1 A', 't1 B');35 }36} {}37do_test notify3-1.2 {38 execsql { 39 CREATE TABLE t2(a, b);40 INSERT INTO t2 VALUES('t2 A', 't2 B');41 } db242} {}43 44do_test notify3-1.3 {45 execsql { 46 BEGIN EXCLUSIVE;47 INSERT INTO t2 VALUES('t2 C', 't2 D');48 } db249} {}50do_test notify3-1.4 {51 catchsql { ATTACH 'test.db2' AS aux }52} {0 {}}53 54do_test notify3-1.5 {55 catchsql { SELECT * FROM t2 }56} {1 {database schema is locked: aux}}57 58do_test notify3-1.6 {59 list [sqlite3_errcode db] [sqlite3_extended_errcode db]60} {SQLITE_LOCKED SQLITE_LOCKED_SHAREDCACHE}61 62do_test notify3-1.7 {63 sqlite3_extended_result_codes db 164 catch { set ::stmt [sqlite3_prepare_v2 db "SELECT * FROM t2" -1 tail] } msg65 set msg66} {(262) database schema is locked: aux}67 68do_test notify3-1.8 {69 set ::when 170 db unlock_notify { set ::res $::when }71 set ::when 272 execsql { COMMIT } db273 set ::res74} {2}75do_test notify3-1.9 {76 catchsql { SELECT * FROM t2 }77} {0 {{t2 A} {t2 B} {t2 C} {t2 D}}}78db close79 80 81set err {{1 {unable to open database: test.db2}}}82set noerr {{0 {}}}83 84# When a new database is attached, the connection doing the attaching 85# tries to load any unloaded schemas for both the new database and any86# already attached databases (including the main database). If it is87# unable to load any such schemas, then the ATTACH statement fails.88#89# This block tests that if the loading of schemas as a result of an90# ATTACH fails due to locks on the schema table held by other shared-cache91# connections the extended error code is SQLITE_LOCKED_SHAREDCACHE and92# it is possible to use the unlock-notify mechanism to determine when93# the ATTACH might succeed.94#95# This test does not work for test-permutations that specify SQL to96# be executed as part of the [sqlite3] command that opens the database.97# Executing such SQL causes SQLite to load the database schema into memory 98# earlier than expected, causing test cases to fail.99#100if {[presql] == ""} {101 foreach {102 tn103 db1_loaded104 db2_loaded105 enable_extended_errors106 result107 error1 error2108 } "109 0 0 0 0 $err SQLITE_LOCKED SQLITE_LOCKED_SHAREDCACHE110 1 0 0 1 $err SQLITE_LOCKED_SHAREDCACHE SQLITE_LOCKED_SHAREDCACHE111 2 0 1 0 $noerr SQLITE_OK SQLITE_OK112 3 0 1 1 $noerr SQLITE_OK SQLITE_OK113 4 1 0 0 $err SQLITE_LOCKED SQLITE_LOCKED_SHAREDCACHE114 5 1 0 1 $err SQLITE_LOCKED_SHAREDCACHE SQLITE_LOCKED_SHAREDCACHE115 6 1 1 0 $noerr SQLITE_OK SQLITE_OK116 7 1 1 1 $noerr SQLITE_OK SQLITE_OK117 " {118 119 do_test notify3-2.$tn.1 {120 catch { db1 close }121 catch { db2 close }122 sqlite3 db1 test.db123 sqlite3 db2 test.db2124 125 sqlite3_extended_result_codes db1 $enable_extended_errors126 sqlite3_extended_result_codes db2 $enable_extended_errors127 128 if { $db1_loaded } { db1 eval "SELECT * FROM sqlite_master" }129 if { $db2_loaded } { db2 eval "SELECT * FROM sqlite_master" }130 131 db2 eval "BEGIN EXCLUSIVE"132 catchsql "ATTACH 'test.db2' AS two" db1133 } $result134 135 do_test notify3-2.$tn.2 {136 list [sqlite3_errcode db1] [sqlite3_extended_errcode db1]137 } [list $error1 $error2]138 139 do_test notify3-2.$tn.3 {140 db1 unlock_notify {set invoked 1}141 set invoked 0142 db2 eval commit143 set invoked144 } [lindex $result 0]145 }146}147catch { db1 close }148catch { db2 close }149 150 151sqlite3_enable_shared_cache $esc152finish_test153 