3
# The author disclaims copyright to this source code. In place of
4
# a legal notice, here is a blessing:
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.
10
#***********************************************************************
11
# This file implements regression tests for SQLite library. The
12
# focus of this script is multithreading behavior
14
# $Id: thread2.test,v 1.3 2008/10/07 15:25:49 drh Exp $
17
set testdir [file dirname $argv0]
18
source $testdir/tester.tcl
26
# Skip this whole file if the thread testing code is not enabled
28
if {[llength [info command thread_step]]==0 || [sqlite3 -has-codec]} {
32
if {![info exists threadsOverrideEachOthersLocks]} {
37
# Create some data to work with
42
INSERT INTO t1 VALUES(1,'abcdefgh');
43
INSERT INTO t1 SELECT a+1, b||b FROM t1;
44
INSERT INTO t1 SELECT a+2, b||b FROM t1;
45
INSERT INTO t1 SELECT a+4, b||b FROM t1;
46
SELECT count(*), max(length(b)) FROM t1;
50
# Use the thread_swap command to move the database connections between
51
# threads, then verify that they still work.
55
thread_create A test.db
56
thread_create B test.db
58
thread_compile A {SELECT a FROM t1 LIMIT 1}
73
thread_compile B {SELECT a FROM t1 LIMIT 1}
92
thread_compile A {SELECT a FROM t1 LIMIT 1}
102
do_test thread2-2.5 {
106
do_test thread2-2.6 {
107
thread_compile B {SELECT a FROM t1 LIMIT 1}
110
do_test thread2-2.7 {
114
do_test thread2-2.8 {
117
do_test thread2-2.9 {
124
# Save the original (correct) value of threadsOverrideEachOthersLocks
125
# so that it can be restored. If this value is left set incorrectly, lots
126
# of things will go wrong in future tests.
128
set orig_threadOverride $threadsOverrideEachOthersLocks
130
# Pretend we are on a system (like RedHat9) were threads do not
131
# override each others locks.
133
set threadsOverrideEachOthersLocks 0
135
# Verify that we can move database connections between threads as
136
# long as no locks are held.
138
do_test thread2-3.1 {
139
thread_create A test.db
140
set DB [thread_db_get A]
143
do_test thread2-3.2 {
144
set STMT [sqlite3_prepare $DB {SELECT a FROM t1 LIMIT 1} -1 TAIL]
147
do_test thread2-3.3 {
148
sqlite3_column_int $STMT 0
150
do_test thread2-3.4 {
151
sqlite3_finalize $STMT
153
do_test thread2-3.5 {
154
set STMT [sqlite3_prepare $DB {SELECT max(a) FROM t1} -1 TAIL]
157
do_test thread2-3.6 {
158
sqlite3_column_int $STMT 0
160
do_test thread2-3.7 {
161
sqlite3_finalize $STMT
163
do_test thread2-3.8 {
167
do_test thread2-3.10 {
168
thread_create A test.db
169
thread_compile A {SELECT a FROM t1 LIMIT 1}
172
set DB [thread_db_get A]
175
do_test thread2-3.11 {
176
set STMT [sqlite3_prepare $DB {SELECT a FROM t1 LIMIT 1} -1 TAIL]
179
do_test thread2-3.12 {
180
sqlite3_column_int $STMT 0
182
do_test thread2-3.13 {
183
sqlite3_finalize $STMT
185
do_test thread2-3.14 {
189
do_test thread2-3.20 {
190
thread_create A test.db
191
thread_compile A {SELECT a FROM t1 LIMIT 3}
193
set STMT [thread_stmt_get A]
194
set DB [thread_db_get A]
197
do_test thread2-3.22 {
198
sqlite3_column_int $STMT 0
200
do_test thread2-3.23 {
201
# The unlock fails here. But because we never check the return
202
# code from sqlite3OsUnlock (because we cannot do anything about it
203
# if it fails) we do not realize that an error has occurred.
205
sqlite3_finalize $STMT
207
do_test thread2-3.25 {
212
do_test thread2-3.30 {
213
thread_create A test.db
214
thread_compile A {BEGIN}
217
thread_compile A {SELECT a FROM t1 LIMIT 1}
220
set DB [thread_db_get A]
221
set STMT [sqlite3_prepare $DB {INSERT INTO t1 VALUES(99,'error')} -1 TAIL]
224
do_test thread2-3.32 {
225
sqlite3_finalize $STMT
227
do_test thread2-3.33 {
232
# VERY important to set the override flag back to its true value.
234
set threadsOverrideEachOthersLocks $orig_threadOverride
236
# Also important to halt the worker threads, which are using spin
237
# locks and eating away CPU cycles.