1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
# 2006 January 14
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library. The
# focus of this script is multithreading behavior
#
# $Id: thread2.test,v 1.3 2008/10/07 15:25:49 drh Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
ifcapable !mutex {
finish_test
return
}
# Skip this whole file if the thread testing code is not enabled
#
if {[llength [info command thread_step]]==0 || [sqlite3 -has-codec]} {
finish_test
return
}
if {![info exists threadsOverrideEachOthersLocks]} {
finish_test
return
}
# Create some data to work with
#
do_test thread1-1.1 {
execsql {
CREATE TABLE t1(a,b);
INSERT INTO t1 VALUES(1,'abcdefgh');
INSERT INTO t1 SELECT a+1, b||b FROM t1;
INSERT INTO t1 SELECT a+2, b||b FROM t1;
INSERT INTO t1 SELECT a+4, b||b FROM t1;
SELECT count(*), max(length(b)) FROM t1;
}
} {8 64}
# Use the thread_swap command to move the database connections between
# threads, then verify that they still work.
#
do_test thread2-1.2 {
db close
thread_create A test.db
thread_create B test.db
thread_swap A B
thread_compile A {SELECT a FROM t1 LIMIT 1}
thread_result A
} {SQLITE_OK}
do_test thread2-1.3 {
thread_step A
thread_result A
} {SQLITE_ROW}
do_test thread2-1.4 {
thread_argv A 0
} {1}
do_test thread2-1.5 {
thread_finalize A
thread_result A
} {SQLITE_OK}
do_test thread2-1.6 {
thread_compile B {SELECT a FROM t1 LIMIT 1}
thread_result B
} {SQLITE_OK}
do_test thread2-1.7 {
thread_step B
thread_result B
} {SQLITE_ROW}
do_test thread2-1.8 {
thread_argv B 0
} {1}
do_test thread2-1.9 {
thread_finalize B
thread_result B
} {SQLITE_OK}
# Swap them again.
#
do_test thread2-2.2 {
thread_swap A B
thread_compile A {SELECT a FROM t1 LIMIT 1}
thread_result A
} {SQLITE_OK}
do_test thread2-2.3 {
thread_step A
thread_result A
} {SQLITE_ROW}
do_test thread2-2.4 {
thread_argv A 0
} {1}
do_test thread2-2.5 {
thread_finalize A
thread_result A
} {SQLITE_OK}
do_test thread2-2.6 {
thread_compile B {SELECT a FROM t1 LIMIT 1}
thread_result B
} {SQLITE_OK}
do_test thread2-2.7 {
thread_step B
thread_result B
} {SQLITE_ROW}
do_test thread2-2.8 {
thread_argv B 0
} {1}
do_test thread2-2.9 {
thread_finalize B
thread_result B
} {SQLITE_OK}
thread_halt A
thread_halt B
# Save the original (correct) value of threadsOverrideEachOthersLocks
# so that it can be restored. If this value is left set incorrectly, lots
# of things will go wrong in future tests.
#
set orig_threadOverride $threadsOverrideEachOthersLocks
# Pretend we are on a system (like RedHat9) were threads do not
# override each others locks.
#
set threadsOverrideEachOthersLocks 0
# Verify that we can move database connections between threads as
# long as no locks are held.
#
do_test thread2-3.1 {
thread_create A test.db
set DB [thread_db_get A]
thread_halt A
} {}
do_test thread2-3.2 {
set STMT [sqlite3_prepare $DB {SELECT a FROM t1 LIMIT 1} -1 TAIL]
sqlite3_step $STMT
} SQLITE_ROW
do_test thread2-3.3 {
sqlite3_column_int $STMT 0
} 1
do_test thread2-3.4 {
sqlite3_finalize $STMT
} SQLITE_OK
do_test thread2-3.5 {
set STMT [sqlite3_prepare $DB {SELECT max(a) FROM t1} -1 TAIL]
sqlite3_step $STMT
} SQLITE_ROW
do_test thread2-3.6 {
sqlite3_column_int $STMT 0
} 8
do_test thread2-3.7 {
sqlite3_finalize $STMT
} SQLITE_OK
do_test thread2-3.8 {
sqlite3_close $DB
} {SQLITE_OK}
do_test thread2-3.10 {
thread_create A test.db
thread_compile A {SELECT a FROM t1 LIMIT 1}
thread_step A
thread_finalize A
set DB [thread_db_get A]
thread_halt A
} {}
do_test thread2-3.11 {
set STMT [sqlite3_prepare $DB {SELECT a FROM t1 LIMIT 1} -1 TAIL]
sqlite3_step $STMT
} SQLITE_ROW
do_test thread2-3.12 {
sqlite3_column_int $STMT 0
} 1
do_test thread2-3.13 {
sqlite3_finalize $STMT
} SQLITE_OK
do_test thread2-3.14 {
sqlite3_close $DB
} SQLITE_OK
do_test thread2-3.20 {
thread_create A test.db
thread_compile A {SELECT a FROM t1 LIMIT 3}
thread_step A
set STMT [thread_stmt_get A]
set DB [thread_db_get A]
sqlite3_step $STMT
} SQLITE_ROW
do_test thread2-3.22 {
sqlite3_column_int $STMT 0
} 2
do_test thread2-3.23 {
# The unlock fails here. But because we never check the return
# code from sqlite3OsUnlock (because we cannot do anything about it
# if it fails) we do not realize that an error has occurred.
breakpoint
sqlite3_finalize $STMT
} SQLITE_OK
do_test thread2-3.25 {
thread_db_put A $DB
thread_halt A
} {}
do_test thread2-3.30 {
thread_create A test.db
thread_compile A {BEGIN}
thread_step A
thread_finalize A
thread_compile A {SELECT a FROM t1 LIMIT 1}
thread_step A
thread_finalize A
set DB [thread_db_get A]
set STMT [sqlite3_prepare $DB {INSERT INTO t1 VALUES(99,'error')} -1 TAIL]
sqlite3_step $STMT
} SQLITE_ERROR
do_test thread2-3.32 {
sqlite3_finalize $STMT
} SQLITE_MISUSE
do_test thread2-3.33 {
thread_db_put A $DB
thread_halt A
} {}
# VERY important to set the override flag back to its true value.
#
set threadsOverrideEachOthersLocks $orig_threadOverride
# Also important to halt the worker threads, which are using spin
# locks and eating away CPU cycles.
#
thread_halt *
finish_test
|