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 }
19 set ::enable_shared_cache [sqlite3_enable_shared_cache]
23 # Run this test three times:
25 # 1) All threads use the same database handle.
26 # 2) All threads use their own database handles.
27 # 3) All threads use their own database handles, shared-cache is enabled.
31 foreach {tn same_db shared_cache} [list \
38 catchsql { DROP TABLE ab; }
40 do_test thread001.$tn.0 {
42 sqlite3_enable_shared_cache $shared_cache
43 sqlite3_enable_shared_cache $shared_cache
45 sqlite3 db test.db -fullmutex 1 -key xyzzy
49 set dbconfig [list set ::DB [sqlite3_connection_pointer db]]
52 # Set up a database and a schema. The database contains a single
53 # table with two columns. The first column ("a") is an INTEGER PRIMARY
54 # KEY. The second contains the md5sum of all rows in the table with
55 # a smaller value stored in column "a".
57 do_test thread001.$tn.1 {
59 CREATE TABLE ab(a INTEGER PRIMARY KEY, b);
60 CREATE INDEX ab_i ON ab(b);
61 INSERT INTO ab SELECT NULL, md5sum(a, b) FROM ab;
62 SELECT count(*) FROM ab;
65 do_test thread001.$tn.2 {
68 (SELECT md5sum(a, b) FROM ab WHERE a < (SELECT max(a) FROM ab)) ==
69 (SELECT b FROM ab WHERE a = (SELECT max(a) FROM ab))
72 do_test thread001.$tn.3 {
73 execsql { PRAGMA integrity_check }
77 #sqlthread parent {puts STARTING..}
79 if {![info exists ::DB]} {
80 set ::DB [sqlthread open test.db xyzzy]
81 #sqlthread parent "puts \"OPEN $::DB\""
85 for {set i 0} {$i < 100} {incr i} {
86 # Test that the invariant is true.
90 (SELECT md5sum(a, b) FROM ab WHERE +a < (SELECT max(a) FROM ab)) ==
91 (SELECT b FROM ab WHERE a = (SELECT max(a) FROM ab))
95 # Add another row to the database.
96 execsql { INSERT INTO ab SELECT NULL, md5sum(a, b) FROM ab }
100 #sqlthread parent "puts \"CLOSE $::DB\""
103 #sqlthread parent "puts \"DONE\""
108 # Kick off $::NTHREAD threads:
111 for {set i 0} {$i < $::NTHREAD} {incr i} {
112 thread_spawn finished($i) $dbconfig $thread_procs $thread_program
115 # Wait for all threads to finish, then check they all returned "OK".
117 for {set i 0} {$i < $::NTHREAD} {incr i} {
118 if {![info exists finished($i)]} {
121 do_test thread001.$tn.4.$i {
126 # Check the database still looks Ok.
128 do_test thread001.$tn.5 {
129 execsql { SELECT count(*) FROM ab; }
130 } [expr {1 + $::NTHREAD*100}]
131 do_test thread001.$tn.6 {
134 (SELECT md5sum(a, b) FROM ab WHERE +a < (SELECT max(a) FROM ab)) ==
135 (SELECT b FROM ab WHERE a = (SELECT max(a) FROM ab))
138 do_test thread001.$tn.7 {
139 execsql { PRAGMA integrity_check }
143 sqlite3_enable_shared_cache $::enable_shared_cache
145 set sqlite_open_file_count 0