danielk1977 | e9dcd5e | 2007-09-10 10:53:01 +0000 | [diff] [blame] | 1 | # 2007 September 10 |
| 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 | # This test attempts to deadlock SQLite in shared-cache mode. |
| 13 | # |
| 14 | # |
danielk1977 | 6d96100 | 2009-03-26 14:48:07 +0000 | [diff] [blame] | 15 | # $Id: thread002.test,v 1.9 2009/03/26 14:48:07 danielk1977 Exp $ |
danielk1977 | e9dcd5e | 2007-09-10 10:53:01 +0000 | [diff] [blame] | 16 | |
| 17 | set testdir [file dirname $argv0] |
| 18 | |
drh | 0ee469c | 2011-08-30 19:52:32 +0000 | [diff] [blame] | 19 | set do_not_use_codec 1 |
danielk1977 | e9dcd5e | 2007-09-10 10:53:01 +0000 | [diff] [blame] | 20 | source $testdir/tester.tcl |
danielk1977 | 6d96100 | 2009-03-26 14:48:07 +0000 | [diff] [blame] | 21 | if {[run_thread_tests]==0} { finish_test ; return } |
danielk1977 | e9dcd5e | 2007-09-10 10:53:01 +0000 | [diff] [blame] | 22 | |
drh | 0ee469c | 2011-08-30 19:52:32 +0000 | [diff] [blame] | 23 | |
danielk1977 | e9dcd5e | 2007-09-10 10:53:01 +0000 | [diff] [blame] | 24 | db close |
danielk1977 | 8a569e2 | 2009-02-04 11:57:46 +0000 | [diff] [blame] | 25 | set ::enable_shared_cache [sqlite3_enable_shared_cache 1] |
danielk1977 | e9dcd5e | 2007-09-10 10:53:01 +0000 | [diff] [blame] | 26 | |
| 27 | set ::NTHREAD 10 |
| 28 | |
| 29 | do_test thread002.1 { |
| 30 | # Create 3 databases with identical schemas: |
| 31 | for {set ii 0} {$ii < 3} {incr ii} { |
mistachkin | fda06be | 2011-08-02 00:57:34 +0000 | [diff] [blame] | 32 | forcedelete test${ii}.db |
danielk1977 | e9dcd5e | 2007-09-10 10:53:01 +0000 | [diff] [blame] | 33 | sqlite3 db test${ii}.db |
| 34 | execsql { |
| 35 | CREATE TABLE t1(k, v); |
| 36 | CREATE INDEX t1_i ON t1(v); |
| 37 | INSERT INTO t1(v) VALUES(1.0); |
| 38 | } |
| 39 | db close |
| 40 | } |
| 41 | } {} |
| 42 | |
| 43 | set thread_program { |
| 44 | set ::DB [sqlite3_open test.db] |
| 45 | for {set ii 1} {$ii <= 3} {incr ii} { |
| 46 | set T [lindex $order [expr $ii-1]] |
| 47 | execsql "ATTACH 'test${T}.db' AS aux${ii}" |
| 48 | } |
| 49 | |
| 50 | for {set ii 0} {$ii < 100} {incr ii} { |
| 51 | execsql { SELECT * FROM aux1.t1 } |
| 52 | execsql { INSERT INTO aux1.t1(v) SELECT sum(v) FROM aux2.t1 } |
| 53 | |
| 54 | execsql { SELECT * FROM aux2.t1 } |
| 55 | execsql { INSERT INTO aux2.t1(v) SELECT sum(v) FROM aux3.t1 } |
| 56 | |
| 57 | execsql { SELECT * FROM aux3.t1 } |
| 58 | execsql { INSERT INTO aux3.t1(v) SELECT sum(v) FROM aux1.t1 } |
| 59 | |
drh | 3ded8d6 | 2009-03-17 15:39:31 +0000 | [diff] [blame] | 60 | execsql { CREATE TABLE IF NOT EXISTS aux1.t2(a,b) } |
| 61 | execsql { DROP TABLE IF EXISTS aux1.t2 } |
danielk1977 | e9dcd5e | 2007-09-10 10:53:01 +0000 | [diff] [blame] | 62 | |
| 63 | # if {($ii%10)==0} {puts -nonewline . ; flush stdout} |
| 64 | puts -nonewline . ; flush stdout |
| 65 | } |
| 66 | |
| 67 | sqlite3_close $::DB |
| 68 | list OK |
| 69 | } |
| 70 | |
| 71 | set order_list [list {0 1 2} {0 2 1} {1 0 2} {1 2 0} {2 0 1} {2 1 0}] |
| 72 | |
| 73 | array unset finished |
| 74 | for {set ii 0} {$ii < $::NTHREAD} {incr ii} { |
| 75 | set order [lindex $order_list [expr $ii%6]] |
| 76 | thread_spawn finished($ii) $thread_procs "set order {$order}" $thread_program |
| 77 | } |
| 78 | |
| 79 | # Wait for all threads to finish, then check they all returned "OK". |
| 80 | # |
| 81 | for {set i 0} {$i < $::NTHREAD} {incr i} { |
| 82 | if {![info exists finished($i)]} { |
| 83 | vwait finished($i) |
| 84 | } |
drh | 1f4969a | 2009-03-16 17:07:57 +0000 | [diff] [blame] | 85 | do_test thread002.2.$i { |
danielk1977 | e9dcd5e | 2007-09-10 10:53:01 +0000 | [diff] [blame] | 86 | set ::finished($i) |
| 87 | } OK |
| 88 | } |
| 89 | |
| 90 | # Check all three databases are Ok. |
| 91 | for {set ii 0} {$ii < 3} {incr ii} { |
| 92 | do_test thread002.3.$ii { |
| 93 | sqlite3 db test${ii}.db |
| 94 | set res [list \ |
| 95 | [execsql {SELECT count(*) FROM t1}] \ |
| 96 | [execsql {PRAGMA integrity_check}] \ |
| 97 | ] |
| 98 | db close |
| 99 | set res |
| 100 | } [list [expr 1 + $::NTHREAD*100] ok] |
| 101 | } |
| 102 | |
danielk1977 | 8a569e2 | 2009-02-04 11:57:46 +0000 | [diff] [blame] | 103 | sqlite3_enable_shared_cache $::enable_shared_cache |
drh | a32e0d0 | 2009-02-12 17:06:41 +0000 | [diff] [blame] | 104 | set sqlite_open_file_count 0 |
danielk1977 | e9dcd5e | 2007-09-10 10:53:01 +0000 | [diff] [blame] | 105 | finish_test |