sl@0: # 2003 December 18 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: thread1.test,v 1.7 2004/06/19 00:16:31 drh Exp $ sl@0: sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl 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: 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: # Interleave two threads on read access. Then make sure a third sl@0: # thread can write the database. In other words: sl@0: # sl@0: # read-lock A sl@0: # read-lock B sl@0: # unlock A sl@0: # unlock B sl@0: # write-lock C sl@0: # sl@0: # At one point, the write-lock of C would fail on Linux. sl@0: # sl@0: do_test thread1-1.2 { sl@0: thread_create A test.db sl@0: thread_create B test.db sl@0: thread_create C test.db sl@0: thread_compile A {SELECT a FROM t1} sl@0: thread_step A sl@0: thread_result A sl@0: } SQLITE_ROW sl@0: do_test thread1-1.3 { sl@0: thread_argc A sl@0: } 1 sl@0: do_test thread1-1.4 { sl@0: thread_argv A 0 sl@0: } 1 sl@0: do_test thread1-1.5 { sl@0: thread_compile B {SELECT b FROM t1} sl@0: thread_step B sl@0: thread_result B sl@0: } SQLITE_ROW sl@0: do_test thread1-1.6 { sl@0: thread_argc B sl@0: } 1 sl@0: do_test thread1-1.7 { sl@0: thread_argv B 0 sl@0: } abcdefgh sl@0: do_test thread1-1.8 { sl@0: thread_finalize A sl@0: thread_result A sl@0: } SQLITE_OK sl@0: do_test thread1-1.9 { sl@0: thread_finalize B sl@0: thread_result B sl@0: } SQLITE_OK sl@0: do_test thread1-1.10 { sl@0: thread_compile C {CREATE TABLE t2(x,y)} sl@0: thread_step C sl@0: thread_result C sl@0: } SQLITE_DONE sl@0: do_test thread1-1.11 { sl@0: thread_finalize C sl@0: thread_result C sl@0: } SQLITE_OK sl@0: do_test thread1-1.12 { sl@0: catchsql {SELECT name FROM sqlite_master} sl@0: execsql {SELECT name FROM sqlite_master} sl@0: } {t1 t2} sl@0: sl@0: sl@0: # sl@0: # The following tests - thread1-2.* - test the following scenario: sl@0: # sl@0: # 1: Read-lock thread A sl@0: # 2: Read-lock thread B sl@0: # 3: Attempt to write in thread C -> SQLITE_BUSY sl@0: # 4: Check db write failed from main thread. sl@0: # 5: Unlock from thread A. sl@0: # 6: Attempt to write in thread C -> SQLITE_BUSY sl@0: # 7: Check db write failed from main thread. sl@0: # 8: Unlock from thread B. sl@0: # 9: Attempt to write in thread C -> SQLITE_DONE sl@0: # 10: Finalize the write from thread C sl@0: # 11: Check db write succeeded from main thread. sl@0: # sl@0: do_test thread1-2.1 { sl@0: thread_halt * sl@0: thread_create A test.db sl@0: thread_compile A {SELECT a FROM t1} sl@0: thread_step A sl@0: thread_result A sl@0: } SQLITE_ROW sl@0: do_test thread1-2.2 { sl@0: thread_create B test.db sl@0: thread_compile B {SELECT b FROM t1} sl@0: thread_step B sl@0: thread_result B sl@0: } SQLITE_ROW sl@0: do_test thread1-2.3 { sl@0: thread_create C test.db sl@0: thread_compile C {INSERT INTO t2 VALUES(98,99)} sl@0: thread_step C sl@0: thread_result C sl@0: thread_finalize C sl@0: thread_result C sl@0: } SQLITE_BUSY sl@0: sl@0: do_test thread1-2.4 { sl@0: execsql {SELECT * FROM t2} sl@0: } {} sl@0: sl@0: do_test thread1-2.5 { sl@0: thread_finalize A sl@0: thread_result A sl@0: } SQLITE_OK sl@0: do_test thread1-2.6 { sl@0: thread_compile C {INSERT INTO t2 VALUES(98,99)} sl@0: thread_step C sl@0: thread_result C sl@0: thread_finalize C sl@0: thread_result C sl@0: } SQLITE_BUSY sl@0: do_test thread1-2.7 { sl@0: execsql {SELECT * FROM t2} sl@0: } {} sl@0: do_test thread1-2.8 { sl@0: thread_finalize B sl@0: thread_result B sl@0: } SQLITE_OK sl@0: do_test thread1-2.9 { sl@0: thread_compile C {INSERT INTO t2 VALUES(98,99)} sl@0: thread_step C sl@0: thread_result C sl@0: } SQLITE_DONE sl@0: do_test thread1-2.10 { sl@0: thread_finalize C sl@0: thread_result C sl@0: } SQLITE_OK sl@0: do_test thread1-2.11 { sl@0: execsql {SELECT * FROM t2} sl@0: } {98 99} sl@0: sl@0: thread_halt * sl@0: finish_test