1# 2009 March 04 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# 12# $Id: notify2.test,v 1.2 2009/03/19 07:58:31 danielk1977 Exp $ 13 14set testdir [file dirname $argv0] 15source $testdir/tester.tcl 16 17# The tests in this file test the sqlite3_blocking_step() function in 18# test_thread.c. sqlite3_blocking_step() is not an SQLite API function, 19# it is just a demonstration of how the sqlite3_unlock_notify() function 20# can be used to synchronize multi-threaded access to SQLite databases 21# in shared-cache mode. 22# 23# Since the implementation of sqlite3_blocking_step() is included on the 24# website as example code, it is important to test that it works. 25# 26# notify2-1.*: 27# 28# This test uses $nThread threads. Each thread opens the main database 29# and attaches two other databases. Each database contains a single table. 30# 31# Each thread repeats transactions over and over for 20 seconds. Each 32# transaction consists of 3 operations. Each operation is either a read 33# or a write of one of the tables. The read operations verify an invariant 34# to make sure that things are working as expected. If an SQLITE_LOCKED 35# error is returned the current transaction is rolled back immediately. 36# 37# This exercise is repeated twice, once using sqlite3_step(), and the 38# other using sqlite3_blocking_step(). The results are compared to ensure 39# that sqlite3_blocking_step() resulted in higher transaction throughput. 40# 41 42if {[info commands sqlite3_blocking_step] eq ""} { 43 finish_test 44 return 45} 46db close 47set ::enable_shared_cache [sqlite3_enable_shared_cache 1] 48source $testdir/thread_common.tcl 49 50# Number of threads to run simultaneously. 51# 52set nThread 3 53set nSecond 5 54 55# The Tcl script executed by each of the $nThread threads used by this test. 56# 57set ThreadProgram { 58 59 # Proc used by threads to execute SQL. 60 # 61 proc execsql_blocking {db zSql} { 62 set lRes [list] 63 set rc SQLITE_OK 64 65 while {$rc=="SQLITE_OK" && $zSql ne ""} { 66 set STMT [$::xPrepare $db $zSql -1 zSql] 67 while {[set rc [$::xStep $STMT]] eq "SQLITE_ROW"} { 68 for {set i 0} {$i < [sqlite3_column_count $STMT]} {incr i} { 69 lappend lRes [sqlite3_column_text $STMT 0] 70 } 71 } 72 set rc [sqlite3_finalize $STMT] 73 } 74 75 if {$rc != "SQLITE_OK"} { error "$rc [sqlite3_errmsg $db]" } 76 return $lRes 77 } 78 79 proc select_one {args} { 80 set n [llength $args] 81 lindex $args [expr int($n*rand())] 82 } 83 84 # Open a database connection. Attach the two auxillary databases. 85 set ::DB [sqlite3_open test.db] 86 execsql_blocking $::DB { 87 ATTACH 'test2.db' AS aux2; 88 ATTACH 'test3.db' AS aux3; 89 } 90 91 # This loop runs for ~20 seconds. 92 # 93 set iStart [clock_seconds] 94 while { ([clock_seconds]-$iStart) < $nSecond } { 95 96 # Each transaction does 3 operations. Each operation is either a read 97 # or write of a randomly selected table (t1, t2 or t3). Set the variables 98 # $SQL(1), $SQL(2) and $SQL(3) to the SQL commands used to implement 99 # each operation. 100 # 101 for {set ii 1} {$ii <= 3} {incr ii} { 102 foreach {tbl database} [select_one {t1 main} {t2 aux2} {t3 aux3}] {} 103 104 set SQL($ii) [string map [list xxx $tbl yyy $database] [select_one { 105 SELECT 106 (SELECT b FROM xxx WHERE a=(SELECT max(a) FROM xxx))==total(a) 107 FROM xxx WHERE a!=(SELECT max(a) FROM xxx); 108 } { 109 DELETE FROM xxx WHERE a<(SELECT max(a)-100 FROM xxx); 110 INSERT INTO xxx SELECT NULL, total(a) FROM xxx; 111 } 112# { 113# CREATE INDEX IF NOT EXISTS yyy.xxx_i ON xxx(b); 114# } { 115# DROP INDEX IF EXISTS yyy.xxx_i; 116# } 117 ]] 118 } 119 120 # Execute the SQL transaction. 121 # 122 set rc [catch { execsql_blocking $::DB " 123 BEGIN; 124 $SQL(1); 125 $SQL(2); 126 $SQL(3); 127 COMMIT; 128 " 129 } msg] 130 131 if {$rc && [string match "SQLITE_LOCKED*" $msg]} { 132 # Hit an SQLITE_LOCKED error. Rollback the current transaction. 133 execsql_blocking $::DB ROLLBACK 134 } elseif {$rc} { 135 # Hit some other kind of error. This is a malfunction. 136 error $msg 137 } else { 138 # No error occured. Check that any SELECT statements in the transaction 139 # returned "1". Otherwise, the invariant was false, indicating that 140 # some malfunction has occured. 141 foreach r $msg { if {$r != 1} { puts "Invariant check failed: $msg" } } 142 } 143 } 144 145 # Close the database connection and return 0. 146 # 147 sqlite3_close $::DB 148 expr 0 149} 150 151foreach {iTest xStep xPrepare} { 152 1 sqlite3_blocking_step sqlite3_blocking_prepare_v2 153 2 sqlite3_step sqlite3_prepare_v2 154} { 155 file delete -force test.db test2.db test3.db 156 157 set ThreadSetup "set xStep $xStep;set xPrepare $xPrepare;set nSecond $nSecond" 158 159 # Set up the database schema used by this test. Each thread opens file 160 # test.db as the main database, then attaches files test2.db and test3.db 161 # as auxillary databases. Each file contains a single table (t1, t2 and t3, in 162 # files test.db, test2.db and test3.db, respectively). 163 # 164 do_test notify2-$iTest.1.1 { 165 sqlite3 db test.db 166 execsql { 167 ATTACH 'test2.db' AS aux2; 168 ATTACH 'test3.db' AS aux3; 169 CREATE TABLE main.t1(a INTEGER PRIMARY KEY, b); 170 CREATE TABLE aux2.t2(a INTEGER PRIMARY KEY, b); 171 CREATE TABLE aux3.t3(a INTEGER PRIMARY KEY, b); 172 INSERT INTO t1 SELECT NULL, 0; 173 INSERT INTO t2 SELECT NULL, 0; 174 INSERT INTO t3 SELECT NULL, 0; 175 } 176 } {} 177 do_test notify2-$iTest.1.2 { 178 db close 179 } {} 180 181 182 # Launch $nThread threads. Then wait for them to finish. 183 # 184 puts "Running $xStep test for $nSecond seconds" 185 unset -nocomplain finished 186 for {set ii 0} {$ii < $nThread} {incr ii} { 187 thread_spawn finished($ii) $ThreadSetup $ThreadProgram 188 } 189 for {set ii 0} {$ii < $nThread} {incr ii} { 190 do_test notify2-$iTest.2.$ii { 191 if {![info exists finished($ii)]} { vwait finished($ii) } 192 set finished($ii) 193 } {0} 194 } 195 196 # Count the total number of succesful writes. 197 do_test notify2-$iTest.3.1 { 198 sqlite3 db test.db 199 execsql { 200 ATTACH 'test2.db' AS aux2; 201 ATTACH 'test3.db' AS aux3; 202 } 203 set anWrite($xStep) [execsql { 204 SELECT (SELECT max(a) FROM t1) 205 + (SELECT max(a) FROM t2) 206 + (SELECT max(a) FROM t3) 207 }] 208 db close 209 } {} 210} 211 212do_test notify2-3 { 213 expr {$anWrite(sqlite3_blocking_step) > $anWrite(sqlite3_step)} 214} {1} 215 216sqlite3_enable_shared_cache $::enable_shared_cache 217finish_test 218 219