1# 2010 June 30 2# 3# The author disclaims copyright to this source code. In place of 4# 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. The 12# focus of this file is testing the sqlite3_unlock_notify() API. 13# 14 15set testdir [file dirname $argv0] 16source $testdir/tester.tcl 17 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.db 28file delete -force test.db2 test.db2-journal test.db2-wal 29sqlite3 db2 test.db2 30 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 } db2 42} {} 43 44do_test notify3-1.3 { 45 execsql { 46 BEGIN EXCLUSIVE; 47 INSERT INTO t2 VALUES('t2 C', 't2 D'); 48 } db2 49} {} 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 1 64 catch { set ::stmt [sqlite3_prepare_v2 db "SELECT * FROM t2" -1 tail] } msg 65 set msg 66} {(262) database schema is locked: aux} 67 68do_test notify3-1.8 { 69 set ::when 1 70 db unlock_notify { set ::res $::when } 71 set ::when 2 72 execsql { COMMIT } db2 73 set ::res 74} {2} 75do_test notify3-1.9 { 76 catchsql { SELECT * FROM t2 } 77} {0 {{t2 A} {t2 B} {t2 C} {t2 D}}} 78db close 79 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 any 86# already attached databases (including the main database). If it is 87# 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 an 90# ATTACH fails due to locks on the schema table held by other shared-cache 91# connections the extended error code is SQLITE_LOCKED_SHAREDCACHE and 92# it is possible to use the unlock-notify mechanism to determine when 93# the ATTACH might succeed. 94# 95foreach { 96 tn 97 db1_loaded 98 db2_loaded 99 enable_extended_errors 100 result 101 error1 error2 102} " 103 0 0 0 0 $err SQLITE_LOCKED SQLITE_LOCKED_SHAREDCACHE 104 1 0 0 1 $err SQLITE_LOCKED_SHAREDCACHE SQLITE_LOCKED_SHAREDCACHE 105 2 0 1 0 $err SQLITE_LOCKED SQLITE_LOCKED_SHAREDCACHE 106 3 0 1 1 $err SQLITE_LOCKED_SHAREDCACHE SQLITE_LOCKED_SHAREDCACHE 107 4 1 0 0 $err SQLITE_LOCKED SQLITE_LOCKED_SHAREDCACHE 108 5 1 0 1 $err SQLITE_LOCKED_SHAREDCACHE SQLITE_LOCKED_SHAREDCACHE 109 6 1 1 0 $noerr SQLITE_OK SQLITE_OK 110 7 1 1 1 $noerr SQLITE_OK SQLITE_OK 111" { 112 113 do_test notify3-2.$tn.1 { 114 catch { db1 close } 115 catch { db2 close } 116 sqlite3 db1 test.db 117 sqlite3 db2 test.db2 118 119 sqlite3_extended_result_codes db1 $enable_extended_errors 120 sqlite3_extended_result_codes db2 $enable_extended_errors 121 122 if { $db1_loaded } { db1 eval "SELECT * FROM sqlite_master" } 123 if { $db2_loaded } { db2 eval "SELECT * FROM sqlite_master" } 124 125 db2 eval "BEGIN EXCLUSIVE" 126 catchsql "ATTACH 'test.db2' AS two" db1 127 } $result 128 129 do_test notify3-2.$tn.2 { 130 list [sqlite3_errcode db1] [sqlite3_extended_errcode db1] 131 } [list $error1 $error2] 132 133 do_test notify3-2.$tn.3 { 134 db1 unlock_notify {set invoked 1} 135 set invoked 0 136 db2 eval commit 137 set invoked 138 } [lindex $result 0] 139} 140catch { db1 close } 141catch { db2 close } 142 143 144sqlite3_enable_shared_cache $esc 145finish_test 146 147