1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/thread2.test Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,246 @@
1.4 +# 2006 January 14
1.5 +#
1.6 +# The author disclaims copyright to this source code. In place of
1.7 +# a legal notice, here is a blessing:
1.8 +#
1.9 +# May you do good and not evil.
1.10 +# May you find forgiveness for yourself and forgive others.
1.11 +# May you share freely, never taking more than you give.
1.12 +#
1.13 +#***********************************************************************
1.14 +# This file implements regression tests for SQLite library. The
1.15 +# focus of this script is multithreading behavior
1.16 +#
1.17 +# $Id: thread2.test,v 1.2 2006/01/18 18:33:42 danielk1977 Exp $
1.18 +
1.19 +
1.20 +set testdir [file dirname $argv0]
1.21 +source $testdir/tester.tcl
1.22 +
1.23 +# This file swaps database connections between threads. This
1.24 +# is illegal if memory-management is enabled, so skip this file
1.25 +# in that case.
1.26 +ifcapable memorymanage {
1.27 + finish_test
1.28 + return
1.29 +}
1.30 +
1.31 +
1.32 +# Skip this whole file if the thread testing code is not enabled
1.33 +#
1.34 +if {[llength [info command thread_step]]==0 || [sqlite3 -has-codec]} {
1.35 + finish_test
1.36 + return
1.37 +}
1.38 +if {![info exists threadsOverrideEachOthersLocks]} {
1.39 + finish_test
1.40 + return
1.41 +}
1.42 +
1.43 +# Create some data to work with
1.44 +#
1.45 +do_test thread1-1.1 {
1.46 + execsql {
1.47 + CREATE TABLE t1(a,b);
1.48 + INSERT INTO t1 VALUES(1,'abcdefgh');
1.49 + INSERT INTO t1 SELECT a+1, b||b FROM t1;
1.50 + INSERT INTO t1 SELECT a+2, b||b FROM t1;
1.51 + INSERT INTO t1 SELECT a+4, b||b FROM t1;
1.52 + SELECT count(*), max(length(b)) FROM t1;
1.53 + }
1.54 +} {8 64}
1.55 +
1.56 +# Use the thread_swap command to move the database connections between
1.57 +# threads, then verify that they still work.
1.58 +#
1.59 +do_test thread2-1.2 {
1.60 + db close
1.61 + thread_create A test.db
1.62 + thread_create B test.db
1.63 + thread_swap A B
1.64 + thread_compile A {SELECT a FROM t1 LIMIT 1}
1.65 + thread_result A
1.66 +} {SQLITE_OK}
1.67 +do_test thread2-1.3 {
1.68 + thread_step A
1.69 + thread_result A
1.70 +} {SQLITE_ROW}
1.71 +do_test thread2-1.4 {
1.72 + thread_argv A 0
1.73 +} {1}
1.74 +do_test thread2-1.5 {
1.75 + thread_finalize A
1.76 + thread_result A
1.77 +} {SQLITE_OK}
1.78 +do_test thread2-1.6 {
1.79 + thread_compile B {SELECT a FROM t1 LIMIT 1}
1.80 + thread_result B
1.81 +} {SQLITE_OK}
1.82 +do_test thread2-1.7 {
1.83 + thread_step B
1.84 + thread_result B
1.85 +} {SQLITE_ROW}
1.86 +do_test thread2-1.8 {
1.87 + thread_argv B 0
1.88 +} {1}
1.89 +do_test thread2-1.9 {
1.90 + thread_finalize B
1.91 + thread_result B
1.92 +} {SQLITE_OK}
1.93 +
1.94 +# Swap them again.
1.95 +#
1.96 +do_test thread2-2.2 {
1.97 + thread_swap A B
1.98 + thread_compile A {SELECT a FROM t1 LIMIT 1}
1.99 + thread_result A
1.100 +} {SQLITE_OK}
1.101 +do_test thread2-2.3 {
1.102 + thread_step A
1.103 + thread_result A
1.104 +} {SQLITE_ROW}
1.105 +do_test thread2-2.4 {
1.106 + thread_argv A 0
1.107 +} {1}
1.108 +do_test thread2-2.5 {
1.109 + thread_finalize A
1.110 + thread_result A
1.111 +} {SQLITE_OK}
1.112 +do_test thread2-2.6 {
1.113 + thread_compile B {SELECT a FROM t1 LIMIT 1}
1.114 + thread_result B
1.115 +} {SQLITE_OK}
1.116 +do_test thread2-2.7 {
1.117 + thread_step B
1.118 + thread_result B
1.119 +} {SQLITE_ROW}
1.120 +do_test thread2-2.8 {
1.121 + thread_argv B 0
1.122 +} {1}
1.123 +do_test thread2-2.9 {
1.124 + thread_finalize B
1.125 + thread_result B
1.126 +} {SQLITE_OK}
1.127 +thread_halt A
1.128 +thread_halt B
1.129 +
1.130 +# Save the original (correct) value of threadsOverrideEachOthersLocks
1.131 +# so that it can be restored. If this value is left set incorrectly, lots
1.132 +# of things will go wrong in future tests.
1.133 +#
1.134 +set orig_threadOverride $threadsOverrideEachOthersLocks
1.135 +
1.136 +# Pretend we are on a system (like RedHat9) were threads do not
1.137 +# override each others locks.
1.138 +#
1.139 +set threadsOverrideEachOthersLocks 0
1.140 +
1.141 +# Verify that we can move database connections between threads as
1.142 +# long as no locks are held.
1.143 +#
1.144 +do_test thread2-3.1 {
1.145 + thread_create A test.db
1.146 + set DB [thread_db_get A]
1.147 + thread_halt A
1.148 +} {}
1.149 +do_test thread2-3.2 {
1.150 + set STMT [sqlite3_prepare $DB {SELECT a FROM t1 LIMIT 1} -1 TAIL]
1.151 + sqlite3_step $STMT
1.152 +} SQLITE_ROW
1.153 +do_test thread2-3.3 {
1.154 + sqlite3_column_int $STMT 0
1.155 +} 1
1.156 +do_test thread2-3.4 {
1.157 + sqlite3_finalize $STMT
1.158 +} SQLITE_OK
1.159 +do_test thread2-3.5 {
1.160 + set STMT [sqlite3_prepare $DB {SELECT max(a) FROM t1} -1 TAIL]
1.161 + sqlite3_step $STMT
1.162 +} SQLITE_ROW
1.163 +do_test thread2-3.6 {
1.164 + sqlite3_column_int $STMT 0
1.165 +} 8
1.166 +do_test thread2-3.7 {
1.167 + sqlite3_finalize $STMT
1.168 +} SQLITE_OK
1.169 +do_test thread2-3.8 {
1.170 + sqlite3_close $DB
1.171 +} {SQLITE_OK}
1.172 +
1.173 +do_test thread2-3.10 {
1.174 + thread_create A test.db
1.175 + thread_compile A {SELECT a FROM t1 LIMIT 1}
1.176 + thread_step A
1.177 + thread_finalize A
1.178 + set DB [thread_db_get A]
1.179 + thread_halt A
1.180 +} {}
1.181 +do_test thread2-3.11 {
1.182 + set STMT [sqlite3_prepare $DB {SELECT a FROM t1 LIMIT 1} -1 TAIL]
1.183 + sqlite3_step $STMT
1.184 +} SQLITE_ROW
1.185 +do_test thread2-3.12 {
1.186 + sqlite3_column_int $STMT 0
1.187 +} 1
1.188 +do_test thread2-3.13 {
1.189 + sqlite3_finalize $STMT
1.190 +} SQLITE_OK
1.191 +do_test thread2-3.14 {
1.192 + sqlite3_close $DB
1.193 +} SQLITE_OK
1.194 +
1.195 +do_test thread2-3.20 {
1.196 + thread_create A test.db
1.197 + thread_compile A {SELECT a FROM t1 LIMIT 3}
1.198 + thread_step A
1.199 + set STMT [thread_stmt_get A]
1.200 + set DB [thread_db_get A]
1.201 + thread_halt A
1.202 +} {}
1.203 +do_test thread2-3.21 {
1.204 + sqlite3_step $STMT
1.205 +} SQLITE_ROW
1.206 +do_test thread2-3.22 {
1.207 + sqlite3_column_int $STMT 0
1.208 +} 2
1.209 +do_test thread2-3.23 {
1.210 + # The unlock fails here. But because we never check the return
1.211 + # code from sqlite3OsUnlock (because we cannot do anything about it
1.212 + # if it fails) we do not realize that an error has occurred.
1.213 + sqlite3_finalize $STMT
1.214 +} SQLITE_OK
1.215 +do_test thread2-3.25 {
1.216 + sqlite3_close $DB
1.217 +} SQLITE_OK
1.218 +
1.219 +do_test thread2-3.30 {
1.220 + thread_create A test.db
1.221 + thread_compile A {BEGIN}
1.222 + thread_step A
1.223 + thread_finalize A
1.224 + thread_compile A {SELECT a FROM t1 LIMIT 1}
1.225 + thread_step A
1.226 + thread_finalize A
1.227 + set DB [thread_db_get A]
1.228 + thread_halt A
1.229 +} {}
1.230 +do_test thread2-3.31 {
1.231 + set STMT [sqlite3_prepare $DB {INSERT INTO t1 VALUES(99,'error')} -1 TAIL]
1.232 + sqlite3_step $STMT
1.233 +} SQLITE_ERROR
1.234 +do_test thread2-3.32 {
1.235 + sqlite3_finalize $STMT
1.236 +} SQLITE_MISUSE
1.237 +do_test thread2-3.33 {
1.238 + sqlite3_close $DB
1.239 +} SQLITE_OK
1.240 +
1.241 +# VERY important to set the override flag back to its true value.
1.242 +#
1.243 +set threadsOverrideEachOthersLocks $orig_threadOverride
1.244 +
1.245 +# Also important to halt the worker threads, which are using spin
1.246 +# locks and eating away CPU cycles.
1.247 +#
1.248 +thread_halt *
1.249 +finish_test