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