AryaWu/sqlite
0
1# 2007 September 72#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#12# $Id: thread001.test,v 1.10 2009/03/26 14:48:07 danielk1977 Exp $13 14set testdir [file dirname $argv0]15 16source $testdir/tester.tcl17if {[run_thread_tests]==0} { finish_test ; return }18ifcapable !shared_cache { finish_test ; return }19 20set ::enable_shared_cache [sqlite3_enable_shared_cache]21 22set ::NTHREAD 1023 24# Run this test three times: 25# 26# 1) All threads use the same database handle.27# 2) All threads use their own database handles.28# 3) All threads use their own database handles, shared-cache is enabled.29#30#31#32foreach {tn same_db shared_cache} [list \33 1 1 0 \34 2 0 0 \35 3 0 1 \36] {37 # Empty the database.38 #39 catchsql { DROP TABLE ab; }40 41 do_test thread001.$tn.0 {42 db close43 sqlite3_enable_shared_cache $shared_cache44 sqlite3_enable_shared_cache $shared_cache45 } $shared_cache46 sqlite3 db test.db -fullmutex 1 -key xyzzy47 48 set dbconfig ""49 if {$same_db} {50 set dbconfig [list set ::DB [sqlite3_connection_pointer db]]51 }52 53 # Set up a database and a schema. The database contains a single54 # table with two columns. The first column ("a") is an INTEGER PRIMARY 55 # KEY. The second contains the md5sum of all rows in the table with56 # a smaller value stored in column "a".57 #58 do_test thread001.$tn.1 {59 execsql {60 CREATE TABLE ab(a INTEGER PRIMARY KEY, b);61 CREATE INDEX ab_i ON ab(b);62 INSERT INTO ab SELECT NULL, md5sum(a, b) FROM ab;63 SELECT count(*) FROM ab;64 }65 } {1}66 do_test thread001.$tn.2 {67 execsql {68 SELECT 69 (SELECT md5sum(a, b) FROM ab WHERE a < (SELECT max(a) FROM ab)) ==70 (SELECT b FROM ab WHERE a = (SELECT max(a) FROM ab))71 }72 } {1}73 do_test thread001.$tn.3 {74 execsql { PRAGMA integrity_check }75 } {ok}76 77 set thread_program {78 #sqlthread parent {puts STARTING..}79 set needToClose 080 if {![info exists ::DB]} {81 set ::DB [sqlthread open test.db xyzzy]82 #sqlthread parent "puts \"OPEN $::DB\""83 set needToClose 184 }85 86 for {set i 0} {$i < 100} {incr i} {87 # Test that the invariant is true.88 do_test t1 {89 execsql {90 SELECT 91 (SELECT md5sum(a, b) FROM ab WHERE +a < (SELECT max(a) FROM ab)) ==92 (SELECT b FROM ab WHERE a = (SELECT max(a) FROM ab))93 }94 } {1}95 96 # Add another row to the database.97 execsql { INSERT INTO ab SELECT NULL, md5sum(a, b) FROM ab }98 }99 100 if {$needToClose} {101 #sqlthread parent "puts \"CLOSE $::DB\""102 sqlite3_close $::DB103 }104 #sqlthread parent "puts \"DONE\""105 106 list OK107 }108 109 # Kick off $::NTHREAD threads:110 #111 array unset finished112 for {set i 0} {$i < $::NTHREAD} {incr i} {113 thread_spawn finished($i) $dbconfig $thread_procs $thread_program114 }115 116 # Wait for all threads to finish, then check they all returned "OK".117 #118 for {set i 0} {$i < $::NTHREAD} {incr i} {119 if {![info exists finished($i)]} {120 vwait finished($i)121 }122 do_test thread001.$tn.4.$i {123 set ::finished($i)124 } OK125 }126 127 # Check the database still looks Ok.128 #129 do_test thread001.$tn.5 {130 execsql { SELECT count(*) FROM ab; }131 } [expr {1 + $::NTHREAD*100}]132 do_test thread001.$tn.6 {133 execsql {134 SELECT 135 (SELECT md5sum(a, b) FROM ab WHERE +a < (SELECT max(a) FROM ab)) ==136 (SELECT b FROM ab WHERE a = (SELECT max(a) FROM ab))137 }138 } {1}139 do_test thread001.$tn.7 {140 execsql { PRAGMA integrity_check }141 } {ok}142}143 144sqlite3_enable_shared_cache $::enable_shared_cache145catch { db close }146set sqlite_open_file_count 0147finish_test148 