blob: b1e5f6368e87aedfc49e3cfac0954f04b784631c [file] [log] [blame]
danielk1977e9dcd5e2007-09-10 10:53:01 +00001# 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#
danielk19776d961002009-03-26 14:48:07 +000015# $Id: thread002.test,v 1.9 2009/03/26 14:48:07 danielk1977 Exp $
danielk1977e9dcd5e2007-09-10 10:53:01 +000016
17set testdir [file dirname $argv0]
18
19source $testdir/tester.tcl
danielk19776d961002009-03-26 14:48:07 +000020if {[run_thread_tests]==0} { finish_test ; return }
danielk1977e9dcd5e2007-09-10 10:53:01 +000021
22db close
danielk19778a569e22009-02-04 11:57:46 +000023set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
danielk1977e9dcd5e2007-09-10 10:53:01 +000024
25set ::NTHREAD 10
26
27do_test thread002.1 {
28 # Create 3 databases with identical schemas:
29 for {set ii 0} {$ii < 3} {incr ii} {
30 file delete -force test${ii}.db
31 sqlite3 db test${ii}.db
32 execsql {
33 CREATE TABLE t1(k, v);
34 CREATE INDEX t1_i ON t1(v);
35 INSERT INTO t1(v) VALUES(1.0);
36 }
37 db close
38 }
39} {}
40
41set thread_program {
42 set ::DB [sqlite3_open test.db]
43 for {set ii 1} {$ii <= 3} {incr ii} {
44 set T [lindex $order [expr $ii-1]]
45 execsql "ATTACH 'test${T}.db' AS aux${ii}"
46 }
47
48 for {set ii 0} {$ii < 100} {incr ii} {
49 execsql { SELECT * FROM aux1.t1 }
50 execsql { INSERT INTO aux1.t1(v) SELECT sum(v) FROM aux2.t1 }
51
52 execsql { SELECT * FROM aux2.t1 }
53 execsql { INSERT INTO aux2.t1(v) SELECT sum(v) FROM aux3.t1 }
54
55 execsql { SELECT * FROM aux3.t1 }
56 execsql { INSERT INTO aux3.t1(v) SELECT sum(v) FROM aux1.t1 }
57
drh3ded8d62009-03-17 15:39:31 +000058 execsql { CREATE TABLE IF NOT EXISTS aux1.t2(a,b) }
59 execsql { DROP TABLE IF EXISTS aux1.t2 }
danielk1977e9dcd5e2007-09-10 10:53:01 +000060
61 # if {($ii%10)==0} {puts -nonewline . ; flush stdout}
62 puts -nonewline . ; flush stdout
63 }
64
65 sqlite3_close $::DB
66 list OK
67}
68
69set order_list [list {0 1 2} {0 2 1} {1 0 2} {1 2 0} {2 0 1} {2 1 0}]
70
71array unset finished
72for {set ii 0} {$ii < $::NTHREAD} {incr ii} {
73 set order [lindex $order_list [expr $ii%6]]
74 thread_spawn finished($ii) $thread_procs "set order {$order}" $thread_program
75}
76
77# Wait for all threads to finish, then check they all returned "OK".
78#
79for {set i 0} {$i < $::NTHREAD} {incr i} {
80 if {![info exists finished($i)]} {
81 vwait finished($i)
82 }
drh1f4969a2009-03-16 17:07:57 +000083 do_test thread002.2.$i {
danielk1977e9dcd5e2007-09-10 10:53:01 +000084 set ::finished($i)
85 } OK
86}
87
88# Check all three databases are Ok.
89for {set ii 0} {$ii < 3} {incr ii} {
90 do_test thread002.3.$ii {
91 sqlite3 db test${ii}.db
92 set res [list \
93 [execsql {SELECT count(*) FROM t1}] \
94 [execsql {PRAGMA integrity_check}] \
95 ]
96 db close
97 set res
98 } [list [expr 1 + $::NTHREAD*100] ok]
99}
100
danielk19778a569e22009-02-04 11:57:46 +0000101sqlite3_enable_shared_cache $::enable_shared_cache
drha32e0d02009-02-12 17:06:41 +0000102set sqlite_open_file_count 0
danielk1977e9dcd5e2007-09-10 10:53:01 +0000103finish_test