danielk1977 | 44918fa | 2007-09-07 11:29:25 +0000 | [diff] [blame] | 1 | # 2007 September 7 |
| 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 | # |
danielk1977 | 6d96100 | 2009-03-26 14:48:07 +0000 | [diff] [blame] | 12 | # $Id: thread001.test,v 1.10 2009/03/26 14:48:07 danielk1977 Exp $ |
danielk1977 | 44918fa | 2007-09-07 11:29:25 +0000 | [diff] [blame] | 13 | |
| 14 | set testdir [file dirname $argv0] |
danielk1977 | 44918fa | 2007-09-07 11:29:25 +0000 | [diff] [blame] | 15 | |
danielk1977 | 49e439d | 2007-09-10 07:35:47 +0000 | [diff] [blame] | 16 | source $testdir/tester.tcl |
danielk1977 | 6d96100 | 2009-03-26 14:48:07 +0000 | [diff] [blame] | 17 | if {[run_thread_tests]==0} { finish_test ; return } |
danielk1977 | 44918fa | 2007-09-07 11:29:25 +0000 | [diff] [blame] | 18 | |
danielk1977 | 8a569e2 | 2009-02-04 11:57:46 +0000 | [diff] [blame] | 19 | set ::enable_shared_cache [sqlite3_enable_shared_cache] |
| 20 | |
danielk1977 | d9b5b11 | 2007-09-10 06:23:53 +0000 | [diff] [blame] | 21 | set ::NTHREAD 10 |
danielk1977 | 44918fa | 2007-09-07 11:29:25 +0000 | [diff] [blame] | 22 | |
danielk1977 | 49e439d | 2007-09-10 07:35:47 +0000 | [diff] [blame] | 23 | # Run this test three times: |
| 24 | # |
| 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. |
danielk1977 | 44918fa | 2007-09-07 11:29:25 +0000 | [diff] [blame] | 28 | # |
drh | b8613ab | 2009-01-19 17:40:12 +0000 | [diff] [blame] | 29 | # |
| 30 | # |
danielk1977 | 49e439d | 2007-09-10 07:35:47 +0000 | [diff] [blame] | 31 | foreach {tn same_db shared_cache} [list \ |
| 32 | 1 1 0 \ |
| 33 | 2 0 0 \ |
| 34 | 3 0 1 \ |
| 35 | ] { |
danielk1977 | d9b5b11 | 2007-09-10 06:23:53 +0000 | [diff] [blame] | 36 | # Empty the database. |
| 37 | # |
| 38 | catchsql { DROP TABLE ab; } |
| 39 | |
danielk1977 | 49e439d | 2007-09-10 07:35:47 +0000 | [diff] [blame] | 40 | do_test thread001.$tn.0 { |
| 41 | db close |
| 42 | sqlite3_enable_shared_cache $shared_cache |
| 43 | sqlite3_enable_shared_cache $shared_cache |
| 44 | } $shared_cache |
drh | 0ee469c | 2011-08-30 19:52:32 +0000 | [diff] [blame] | 45 | sqlite3 db test.db -fullmutex 1 -key xyzzy |
danielk1977 | 49e439d | 2007-09-10 07:35:47 +0000 | [diff] [blame] | 46 | |
| 47 | set dbconfig "" |
| 48 | if {$same_db} { |
| 49 | set dbconfig [list set ::DB [sqlite3_connection_pointer db]] |
| 50 | } |
| 51 | |
danielk1977 | d9b5b11 | 2007-09-10 06:23:53 +0000 | [diff] [blame] | 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". |
| 56 | # |
| 57 | do_test thread001.$tn.1 { |
| 58 | execsql { |
| 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; |
| 63 | } |
| 64 | } {1} |
| 65 | do_test thread001.$tn.2 { |
| 66 | execsql { |
| 67 | SELECT |
| 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)) |
| 70 | } |
| 71 | } {1} |
| 72 | do_test thread001.$tn.3 { |
| 73 | execsql { PRAGMA integrity_check } |
| 74 | } {ok} |
drh | b8613ab | 2009-01-19 17:40:12 +0000 | [diff] [blame] | 75 | |
danielk1977 | d9b5b11 | 2007-09-10 06:23:53 +0000 | [diff] [blame] | 76 | set thread_program { |
drh | b8613ab | 2009-01-19 17:40:12 +0000 | [diff] [blame] | 77 | #sqlthread parent {puts STARTING..} |
danielk1977 | d9b5b11 | 2007-09-10 06:23:53 +0000 | [diff] [blame] | 78 | set needToClose 0 |
| 79 | if {![info exists ::DB]} { |
drh | 0ee469c | 2011-08-30 19:52:32 +0000 | [diff] [blame] | 80 | set ::DB [sqlthread open test.db xyzzy] |
drh | b8613ab | 2009-01-19 17:40:12 +0000 | [diff] [blame] | 81 | #sqlthread parent "puts \"OPEN $::DB\"" |
danielk1977 | d9b5b11 | 2007-09-10 06:23:53 +0000 | [diff] [blame] | 82 | set needToClose 1 |
| 83 | } |
| 84 | |
| 85 | for {set i 0} {$i < 100} {incr i} { |
| 86 | # Test that the invariant is true. |
| 87 | do_test t1 { |
| 88 | execsql { |
| 89 | SELECT |
dan | 97305a7 | 2012-12-05 16:44:13 +0000 | [diff] [blame] | 90 | (SELECT md5sum(a, b) FROM ab WHERE +a < (SELECT max(a) FROM ab)) == |
danielk1977 | d9b5b11 | 2007-09-10 06:23:53 +0000 | [diff] [blame] | 91 | (SELECT b FROM ab WHERE a = (SELECT max(a) FROM ab)) |
| 92 | } |
| 93 | } {1} |
| 94 | |
| 95 | # Add another row to the database. |
| 96 | execsql { INSERT INTO ab SELECT NULL, md5sum(a, b) FROM ab } |
| 97 | } |
| 98 | |
| 99 | if {$needToClose} { |
drh | b8613ab | 2009-01-19 17:40:12 +0000 | [diff] [blame] | 100 | #sqlthread parent "puts \"CLOSE $::DB\"" |
danielk1977 | d9b5b11 | 2007-09-10 06:23:53 +0000 | [diff] [blame] | 101 | sqlite3_close $::DB |
| 102 | } |
drh | b8613ab | 2009-01-19 17:40:12 +0000 | [diff] [blame] | 103 | #sqlthread parent "puts \"DONE\"" |
danielk1977 | d9b5b11 | 2007-09-10 06:23:53 +0000 | [diff] [blame] | 104 | |
| 105 | list OK |
| 106 | } |
| 107 | |
| 108 | # Kick off $::NTHREAD threads: |
| 109 | # |
| 110 | array unset finished |
| 111 | for {set i 0} {$i < $::NTHREAD} {incr i} { |
| 112 | thread_spawn finished($i) $dbconfig $thread_procs $thread_program |
| 113 | } |
| 114 | |
| 115 | # Wait for all threads to finish, then check they all returned "OK". |
| 116 | # |
| 117 | for {set i 0} {$i < $::NTHREAD} {incr i} { |
| 118 | if {![info exists finished($i)]} { |
| 119 | vwait finished($i) |
| 120 | } |
| 121 | do_test thread001.$tn.4.$i { |
| 122 | set ::finished($i) |
| 123 | } OK |
| 124 | } |
| 125 | |
| 126 | # Check the database still looks Ok. |
| 127 | # |
| 128 | do_test thread001.$tn.5 { |
| 129 | execsql { SELECT count(*) FROM ab; } |
| 130 | } [expr {1 + $::NTHREAD*100}] |
| 131 | do_test thread001.$tn.6 { |
| 132 | execsql { |
| 133 | SELECT |
dan | 97305a7 | 2012-12-05 16:44:13 +0000 | [diff] [blame] | 134 | (SELECT md5sum(a, b) FROM ab WHERE +a < (SELECT max(a) FROM ab)) == |
danielk1977 | d9b5b11 | 2007-09-10 06:23:53 +0000 | [diff] [blame] | 135 | (SELECT b FROM ab WHERE a = (SELECT max(a) FROM ab)) |
| 136 | } |
| 137 | } {1} |
| 138 | do_test thread001.$tn.7 { |
| 139 | execsql { PRAGMA integrity_check } |
| 140 | } {ok} |
| 141 | } |
danielk1977 | 44918fa | 2007-09-07 11:29:25 +0000 | [diff] [blame] | 142 | |
danielk1977 | 8a569e2 | 2009-02-04 11:57:46 +0000 | [diff] [blame] | 143 | sqlite3_enable_shared_cache $::enable_shared_cache |
drh | a32e0d0 | 2009-02-12 17:06:41 +0000 | [diff] [blame] | 144 | set sqlite_open_file_count 0 |
danielk1977 | 44918fa | 2007-09-07 11:29:25 +0000 | [diff] [blame] | 145 | finish_test |