1# 2003 December 18 2# 3# The author disclaims copyright to this source code. In place of 4# a legal notice, here is a blessing: 5# 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. 9# 10#*********************************************************************** 11# This file implements regression tests for SQLite library. The 12# focus of this script is multithreading behavior 13# 14# $Id: thread1.test,v 1.8 2008/10/07 15:25:49 drh Exp $ 15 16 17set testdir [file dirname $argv0] 18source $testdir/tester.tcl 19 20# Skip this whole file if the thread testing code is not enabled 21# 22ifcapable !mutex { 23 finish_test 24 return 25} 26if {[llength [info command thread_step]]==0 || [sqlite3 -has-codec]} { 27 finish_test 28 return 29} 30 31# Create some data to work with 32# 33do_test thread1-1.1 { 34 execsql { 35 CREATE TABLE t1(a,b); 36 INSERT INTO t1 VALUES(1,'abcdefgh'); 37 INSERT INTO t1 SELECT a+1, b||b FROM t1; 38 INSERT INTO t1 SELECT a+2, b||b FROM t1; 39 INSERT INTO t1 SELECT a+4, b||b FROM t1; 40 SELECT count(*), max(length(b)) FROM t1; 41 } 42} {8 64} 43 44# Interleave two threads on read access. Then make sure a third 45# thread can write the database. In other words: 46# 47# read-lock A 48# read-lock B 49# unlock A 50# unlock B 51# write-lock C 52# 53# At one point, the write-lock of C would fail on Linux. 54# 55do_test thread1-1.2 { 56 thread_create A test.db 57 thread_create B test.db 58 thread_create C test.db 59 thread_compile A {SELECT a FROM t1} 60 thread_step A 61 thread_result A 62} SQLITE_ROW 63do_test thread1-1.3 { 64 thread_argc A 65} 1 66do_test thread1-1.4 { 67 thread_argv A 0 68} 1 69do_test thread1-1.5 { 70 thread_compile B {SELECT b FROM t1} 71 thread_step B 72 thread_result B 73} SQLITE_ROW 74do_test thread1-1.6 { 75 thread_argc B 76} 1 77do_test thread1-1.7 { 78 thread_argv B 0 79} abcdefgh 80do_test thread1-1.8 { 81 thread_finalize A 82 thread_result A 83} SQLITE_OK 84do_test thread1-1.9 { 85 thread_finalize B 86 thread_result B 87} SQLITE_OK 88do_test thread1-1.10 { 89 thread_compile C {CREATE TABLE t2(x,y)} 90 thread_step C 91 thread_result C 92} SQLITE_DONE 93do_test thread1-1.11 { 94 thread_finalize C 95 thread_result C 96} SQLITE_OK 97do_test thread1-1.12 { 98 catchsql {SELECT name FROM sqlite_master} 99 execsql {SELECT name FROM sqlite_master} 100} {t1 t2} 101 102 103# 104# The following tests - thread1-2.* - test the following scenario: 105# 106# 1: Read-lock thread A 107# 2: Read-lock thread B 108# 3: Attempt to write in thread C -> SQLITE_BUSY 109# 4: Check db write failed from main thread. 110# 5: Unlock from thread A. 111# 6: Attempt to write in thread C -> SQLITE_BUSY 112# 7: Check db write failed from main thread. 113# 8: Unlock from thread B. 114# 9: Attempt to write in thread C -> SQLITE_DONE 115# 10: Finalize the write from thread C 116# 11: Check db write succeeded from main thread. 117# 118do_test thread1-2.1 { 119 thread_halt * 120 thread_create A test.db 121 thread_compile A {SELECT a FROM t1} 122 thread_step A 123 thread_result A 124} SQLITE_ROW 125do_test thread1-2.2 { 126 thread_create B test.db 127 thread_compile B {SELECT b FROM t1} 128 thread_step B 129 thread_result B 130} SQLITE_ROW 131do_test thread1-2.3 { 132 thread_create C test.db 133 thread_compile C {INSERT INTO t2 VALUES(98,99)} 134 thread_step C 135 thread_result C 136 thread_finalize C 137 thread_result C 138} SQLITE_BUSY 139 140do_test thread1-2.4 { 141 execsql {SELECT * FROM t2} 142} {} 143 144do_test thread1-2.5 { 145 thread_finalize A 146 thread_result A 147} SQLITE_OK 148do_test thread1-2.6 { 149 thread_compile C {INSERT INTO t2 VALUES(98,99)} 150 thread_step C 151 thread_result C 152 thread_finalize C 153 thread_result C 154} SQLITE_BUSY 155do_test thread1-2.7 { 156 execsql {SELECT * FROM t2} 157} {} 158do_test thread1-2.8 { 159 thread_finalize B 160 thread_result B 161} SQLITE_OK 162do_test thread1-2.9 { 163 thread_compile C {INSERT INTO t2 VALUES(98,99)} 164 thread_step C 165 thread_result C 166} SQLITE_DONE 167do_test thread1-2.10 { 168 thread_finalize C 169 thread_result C 170} SQLITE_OK 171do_test thread1-2.11 { 172 execsql {SELECT * FROM t2} 173} {98 99} 174 175thread_halt * 176finish_test 177