sl@0
|
1 |
# 2007 September 10
|
sl@0
|
2 |
#
|
sl@0
|
3 |
# The author disclaims copyright to this source code. In place of
|
sl@0
|
4 |
# a legal notice, here is a blessing:
|
sl@0
|
5 |
#
|
sl@0
|
6 |
# May you do good and not evil.
|
sl@0
|
7 |
# May you find forgiveness for yourself and forgive others.
|
sl@0
|
8 |
# May you share freely, never taking more than you give.
|
sl@0
|
9 |
#
|
sl@0
|
10 |
#***********************************************************************
|
sl@0
|
11 |
#
|
sl@0
|
12 |
# This test attempts to deadlock SQLite in shared-cache mode.
|
sl@0
|
13 |
#
|
sl@0
|
14 |
#
|
sl@0
|
15 |
# $Id: thread002.test,v 1.3 2008/07/12 14:52:20 drh Exp $
|
sl@0
|
16 |
|
sl@0
|
17 |
set testdir [file dirname $argv0]
|
sl@0
|
18 |
|
sl@0
|
19 |
source $testdir/tester.tcl
|
sl@0
|
20 |
source $testdir/thread_common.tcl
|
sl@0
|
21 |
if {[info commands sqlthread] eq ""} {
|
sl@0
|
22 |
finish_test
|
sl@0
|
23 |
return
|
sl@0
|
24 |
}
|
sl@0
|
25 |
ifcapable !attach {
|
sl@0
|
26 |
finish_test
|
sl@0
|
27 |
return
|
sl@0
|
28 |
}
|
sl@0
|
29 |
|
sl@0
|
30 |
db close
|
sl@0
|
31 |
sqlite3_enable_shared_cache 1
|
sl@0
|
32 |
|
sl@0
|
33 |
set ::NTHREAD 10
|
sl@0
|
34 |
|
sl@0
|
35 |
do_test thread002.1 {
|
sl@0
|
36 |
# Create 3 databases with identical schemas:
|
sl@0
|
37 |
for {set ii 0} {$ii < 3} {incr ii} {
|
sl@0
|
38 |
file delete -force test${ii}.db
|
sl@0
|
39 |
sqlite3 db test${ii}.db
|
sl@0
|
40 |
execsql {
|
sl@0
|
41 |
CREATE TABLE t1(k, v);
|
sl@0
|
42 |
CREATE INDEX t1_i ON t1(v);
|
sl@0
|
43 |
INSERT INTO t1(v) VALUES(1.0);
|
sl@0
|
44 |
}
|
sl@0
|
45 |
db close
|
sl@0
|
46 |
}
|
sl@0
|
47 |
} {}
|
sl@0
|
48 |
|
sl@0
|
49 |
set thread_program {
|
sl@0
|
50 |
set ::DB [sqlite3_open test.db]
|
sl@0
|
51 |
for {set ii 1} {$ii <= 3} {incr ii} {
|
sl@0
|
52 |
set T [lindex $order [expr $ii-1]]
|
sl@0
|
53 |
execsql "ATTACH 'test${T}.db' AS aux${ii}"
|
sl@0
|
54 |
}
|
sl@0
|
55 |
|
sl@0
|
56 |
for {set ii 0} {$ii < 100} {incr ii} {
|
sl@0
|
57 |
execsql { SELECT * FROM aux1.t1 }
|
sl@0
|
58 |
execsql { INSERT INTO aux1.t1(v) SELECT sum(v) FROM aux2.t1 }
|
sl@0
|
59 |
|
sl@0
|
60 |
execsql { SELECT * FROM aux2.t1 }
|
sl@0
|
61 |
execsql { INSERT INTO aux2.t1(v) SELECT sum(v) FROM aux3.t1 }
|
sl@0
|
62 |
|
sl@0
|
63 |
execsql { SELECT * FROM aux3.t1 }
|
sl@0
|
64 |
execsql { INSERT INTO aux3.t1(v) SELECT sum(v) FROM aux1.t1 }
|
sl@0
|
65 |
|
sl@0
|
66 |
execsql { CREATE TABLE aux1.t2(a,b) }
|
sl@0
|
67 |
execsql { DROP TABLE aux1.t2 }
|
sl@0
|
68 |
|
sl@0
|
69 |
# if {($ii%10)==0} {puts -nonewline . ; flush stdout}
|
sl@0
|
70 |
puts -nonewline . ; flush stdout
|
sl@0
|
71 |
}
|
sl@0
|
72 |
|
sl@0
|
73 |
sqlite3_close $::DB
|
sl@0
|
74 |
list OK
|
sl@0
|
75 |
}
|
sl@0
|
76 |
|
sl@0
|
77 |
set order_list [list {0 1 2} {0 2 1} {1 0 2} {1 2 0} {2 0 1} {2 1 0}]
|
sl@0
|
78 |
|
sl@0
|
79 |
array unset finished
|
sl@0
|
80 |
for {set ii 0} {$ii < $::NTHREAD} {incr ii} {
|
sl@0
|
81 |
set order [lindex $order_list [expr $ii%6]]
|
sl@0
|
82 |
thread_spawn finished($ii) $thread_procs "set order {$order}" $thread_program
|
sl@0
|
83 |
}
|
sl@0
|
84 |
|
sl@0
|
85 |
# Wait for all threads to finish, then check they all returned "OK".
|
sl@0
|
86 |
#
|
sl@0
|
87 |
for {set i 0} {$i < $::NTHREAD} {incr i} {
|
sl@0
|
88 |
if {![info exists finished($i)]} {
|
sl@0
|
89 |
vwait finished($i)
|
sl@0
|
90 |
}
|
sl@0
|
91 |
do_test thread001.2.$i {
|
sl@0
|
92 |
set ::finished($i)
|
sl@0
|
93 |
} OK
|
sl@0
|
94 |
}
|
sl@0
|
95 |
|
sl@0
|
96 |
# Check all three databases are Ok.
|
sl@0
|
97 |
for {set ii 0} {$ii < 3} {incr ii} {
|
sl@0
|
98 |
do_test thread002.3.$ii {
|
sl@0
|
99 |
sqlite3 db test${ii}.db
|
sl@0
|
100 |
set res [list \
|
sl@0
|
101 |
[execsql {SELECT count(*) FROM t1}] \
|
sl@0
|
102 |
[execsql {PRAGMA integrity_check}] \
|
sl@0
|
103 |
]
|
sl@0
|
104 |
db close
|
sl@0
|
105 |
set res
|
sl@0
|
106 |
} [list [expr 1 + $::NTHREAD*100] ok]
|
sl@0
|
107 |
}
|
sl@0
|
108 |
|
sl@0
|
109 |
finish_test
|