sl@0: # 2006 January 14 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file implements regression tests for SQLite library. The sl@0: # focus of this script is multithreading behavior sl@0: # sl@0: # $Id: thread2.test,v 1.2 2006/01/18 18:33:42 danielk1977 Exp $ sl@0: sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: # This file swaps database connections between threads. This sl@0: # is illegal if memory-management is enabled, so skip this file sl@0: # in that case. sl@0: ifcapable memorymanage { sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: sl@0: # Skip this whole file if the thread testing code is not enabled sl@0: # sl@0: if {[llength [info command thread_step]]==0 || [sqlite3 -has-codec]} { sl@0: finish_test sl@0: return sl@0: } sl@0: if {![info exists threadsOverrideEachOthersLocks]} { sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: # Create some data to work with sl@0: # sl@0: do_test thread1-1.1 { sl@0: execsql { sl@0: CREATE TABLE t1(a,b); sl@0: INSERT INTO t1 VALUES(1,'abcdefgh'); sl@0: INSERT INTO t1 SELECT a+1, b||b FROM t1; sl@0: INSERT INTO t1 SELECT a+2, b||b FROM t1; sl@0: INSERT INTO t1 SELECT a+4, b||b FROM t1; sl@0: SELECT count(*), max(length(b)) FROM t1; sl@0: } sl@0: } {8 64} sl@0: sl@0: # Use the thread_swap command to move the database connections between sl@0: # threads, then verify that they still work. sl@0: # sl@0: do_test thread2-1.2 { sl@0: db close sl@0: thread_create A test.db sl@0: thread_create B test.db sl@0: thread_swap A B sl@0: thread_compile A {SELECT a FROM t1 LIMIT 1} sl@0: thread_result A sl@0: } {SQLITE_OK} sl@0: do_test thread2-1.3 { sl@0: thread_step A sl@0: thread_result A sl@0: } {SQLITE_ROW} sl@0: do_test thread2-1.4 { sl@0: thread_argv A 0 sl@0: } {1} sl@0: do_test thread2-1.5 { sl@0: thread_finalize A sl@0: thread_result A sl@0: } {SQLITE_OK} sl@0: do_test thread2-1.6 { sl@0: thread_compile B {SELECT a FROM t1 LIMIT 1} sl@0: thread_result B sl@0: } {SQLITE_OK} sl@0: do_test thread2-1.7 { sl@0: thread_step B sl@0: thread_result B sl@0: } {SQLITE_ROW} sl@0: do_test thread2-1.8 { sl@0: thread_argv B 0 sl@0: } {1} sl@0: do_test thread2-1.9 { sl@0: thread_finalize B sl@0: thread_result B sl@0: } {SQLITE_OK} sl@0: sl@0: # Swap them again. sl@0: # sl@0: do_test thread2-2.2 { sl@0: thread_swap A B sl@0: thread_compile A {SELECT a FROM t1 LIMIT 1} sl@0: thread_result A sl@0: } {SQLITE_OK} sl@0: do_test thread2-2.3 { sl@0: thread_step A sl@0: thread_result A sl@0: } {SQLITE_ROW} sl@0: do_test thread2-2.4 { sl@0: thread_argv A 0 sl@0: } {1} sl@0: do_test thread2-2.5 { sl@0: thread_finalize A sl@0: thread_result A sl@0: } {SQLITE_OK} sl@0: do_test thread2-2.6 { sl@0: thread_compile B {SELECT a FROM t1 LIMIT 1} sl@0: thread_result B sl@0: } {SQLITE_OK} sl@0: do_test thread2-2.7 { sl@0: thread_step B sl@0: thread_result B sl@0: } {SQLITE_ROW} sl@0: do_test thread2-2.8 { sl@0: thread_argv B 0 sl@0: } {1} sl@0: do_test thread2-2.9 { sl@0: thread_finalize B sl@0: thread_result B sl@0: } {SQLITE_OK} sl@0: thread_halt A sl@0: thread_halt B sl@0: sl@0: # Save the original (correct) value of threadsOverrideEachOthersLocks sl@0: # so that it can be restored. If this value is left set incorrectly, lots sl@0: # of things will go wrong in future tests. sl@0: # sl@0: set orig_threadOverride $threadsOverrideEachOthersLocks sl@0: sl@0: # Pretend we are on a system (like RedHat9) were threads do not sl@0: # override each others locks. sl@0: # sl@0: set threadsOverrideEachOthersLocks 0 sl@0: sl@0: # Verify that we can move database connections between threads as sl@0: # long as no locks are held. sl@0: # sl@0: do_test thread2-3.1 { sl@0: thread_create A test.db sl@0: set DB [thread_db_get A] sl@0: thread_halt A sl@0: } {} sl@0: do_test thread2-3.2 { sl@0: set STMT [sqlite3_prepare $DB {SELECT a FROM t1 LIMIT 1} -1 TAIL] sl@0: sqlite3_step $STMT sl@0: } SQLITE_ROW sl@0: do_test thread2-3.3 { sl@0: sqlite3_column_int $STMT 0 sl@0: } 1 sl@0: do_test thread2-3.4 { sl@0: sqlite3_finalize $STMT sl@0: } SQLITE_OK sl@0: do_test thread2-3.5 { sl@0: set STMT [sqlite3_prepare $DB {SELECT max(a) FROM t1} -1 TAIL] sl@0: sqlite3_step $STMT sl@0: } SQLITE_ROW sl@0: do_test thread2-3.6 { sl@0: sqlite3_column_int $STMT 0 sl@0: } 8 sl@0: do_test thread2-3.7 { sl@0: sqlite3_finalize $STMT sl@0: } SQLITE_OK sl@0: do_test thread2-3.8 { sl@0: sqlite3_close $DB sl@0: } {SQLITE_OK} sl@0: sl@0: do_test thread2-3.10 { sl@0: thread_create A test.db sl@0: thread_compile A {SELECT a FROM t1 LIMIT 1} sl@0: thread_step A sl@0: thread_finalize A sl@0: set DB [thread_db_get A] sl@0: thread_halt A sl@0: } {} sl@0: do_test thread2-3.11 { sl@0: set STMT [sqlite3_prepare $DB {SELECT a FROM t1 LIMIT 1} -1 TAIL] sl@0: sqlite3_step $STMT sl@0: } SQLITE_ROW sl@0: do_test thread2-3.12 { sl@0: sqlite3_column_int $STMT 0 sl@0: } 1 sl@0: do_test thread2-3.13 { sl@0: sqlite3_finalize $STMT sl@0: } SQLITE_OK sl@0: do_test thread2-3.14 { sl@0: sqlite3_close $DB sl@0: } SQLITE_OK sl@0: sl@0: do_test thread2-3.20 { sl@0: thread_create A test.db sl@0: thread_compile A {SELECT a FROM t1 LIMIT 3} sl@0: thread_step A sl@0: set STMT [thread_stmt_get A] sl@0: set DB [thread_db_get A] sl@0: thread_halt A sl@0: } {} sl@0: do_test thread2-3.21 { sl@0: sqlite3_step $STMT sl@0: } SQLITE_ROW sl@0: do_test thread2-3.22 { sl@0: sqlite3_column_int $STMT 0 sl@0: } 2 sl@0: do_test thread2-3.23 { sl@0: # The unlock fails here. But because we never check the return sl@0: # code from sqlite3OsUnlock (because we cannot do anything about it sl@0: # if it fails) we do not realize that an error has occurred. sl@0: sqlite3_finalize $STMT sl@0: } SQLITE_OK sl@0: do_test thread2-3.25 { sl@0: sqlite3_close $DB sl@0: } SQLITE_OK sl@0: sl@0: do_test thread2-3.30 { sl@0: thread_create A test.db sl@0: thread_compile A {BEGIN} sl@0: thread_step A sl@0: thread_finalize A sl@0: thread_compile A {SELECT a FROM t1 LIMIT 1} sl@0: thread_step A sl@0: thread_finalize A sl@0: set DB [thread_db_get A] sl@0: thread_halt A sl@0: } {} sl@0: do_test thread2-3.31 { sl@0: set STMT [sqlite3_prepare $DB {INSERT INTO t1 VALUES(99,'error')} -1 TAIL] sl@0: sqlite3_step $STMT sl@0: } SQLITE_ERROR sl@0: do_test thread2-3.32 { sl@0: sqlite3_finalize $STMT sl@0: } SQLITE_MISUSE sl@0: do_test thread2-3.33 { sl@0: sqlite3_close $DB sl@0: } SQLITE_OK sl@0: sl@0: # VERY important to set the override flag back to its true value. sl@0: # sl@0: set threadsOverrideEachOthersLocks $orig_threadOverride sl@0: sl@0: # Also important to halt the worker threads, which are using spin sl@0: # locks and eating away CPU cycles. sl@0: # sl@0: thread_halt * sl@0: finish_test