3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
12 # $Id: thread001.test,v 1.10 2009/03/26 14:48:07 danielk1977 Exp $
14 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
17 if {[run_thread_tests]==0} { finish_test ; return }
18 ifcapable !shared_cache { finish_test ; return }
20 set ::enable_shared_cache [sqlite3_enable_shared_cache]
24 # Run this test three times:
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.
32 foreach {tn same_db shared_cache} [list \
39 catchsql { DROP TABLE ab; }
41 do_test thread001.$tn.0 {
43 sqlite3_enable_shared_cache $shared_cache
44 sqlite3_enable_shared_cache $shared_cache
46 sqlite3 db test.db -fullmutex 1 -key xyzzy
50 set dbconfig [list set ::DB [sqlite3_connection_pointer db]]
53 # Set up a database and a schema. The database contains a single
54 # 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 with
56 # a smaller value stored in column "a".
58 do_test thread001.$tn.1 {
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;
66 do_test thread001.$tn.2 {
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))
73 do_test thread001.$tn.3 {
74 execsql { PRAGMA integrity_check }
78 #sqlthread parent {puts STARTING..}
80 if {![info exists ::DB]} {
81 set ::DB [sqlthread open test.db xyzzy]
82 #sqlthread parent "puts \"OPEN $::DB\""
86 for {set i 0} {$i < 100} {incr i} {
87 # Test that the invariant is true.
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))
96 # Add another row to the database.
97 execsql { INSERT INTO ab SELECT NULL, md5sum(a, b) FROM ab }
101 #sqlthread parent "puts \"CLOSE $::DB\""
104 #sqlthread parent "puts \"DONE\""
109 # Kick off $::NTHREAD threads:
112 for {set i 0} {$i < $::NTHREAD} {incr i} {
113 thread_spawn finished($i) $dbconfig $thread_procs $thread_program
116 # Wait for all threads to finish, then check they all returned "OK".
118 for {set i 0} {$i < $::NTHREAD} {incr i} {
119 if {![info exists finished($i)]} {
122 do_test thread001.$tn.4.$i {
127 # Check the database still looks Ok.
129 do_test thread001.$tn.5 {
130 execsql { SELECT count(*) FROM ab; }
131 } [expr {1 + $::NTHREAD*100}]
132 do_test thread001.$tn.6 {
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))
139 do_test thread001.$tn.7 {
140 execsql { PRAGMA integrity_check }
144 sqlite3_enable_shared_cache $::enable_shared_cache
146 set sqlite_open_file_count 0