1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/thread1.test Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,172 @@
1.4 +# 2003 December 18
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: thread1.test,v 1.7 2004/06/19 00:16:31 drh Exp $
1.18 +
1.19 +
1.20 +set testdir [file dirname $argv0]
1.21 +source $testdir/tester.tcl
1.22 +
1.23 +# Skip this whole file if the thread testing code is not enabled
1.24 +#
1.25 +if {[llength [info command thread_step]]==0 || [sqlite3 -has-codec]} {
1.26 + finish_test
1.27 + return
1.28 +}
1.29 +
1.30 +# Create some data to work with
1.31 +#
1.32 +do_test thread1-1.1 {
1.33 + execsql {
1.34 + CREATE TABLE t1(a,b);
1.35 + INSERT INTO t1 VALUES(1,'abcdefgh');
1.36 + INSERT INTO t1 SELECT a+1, b||b FROM t1;
1.37 + INSERT INTO t1 SELECT a+2, b||b FROM t1;
1.38 + INSERT INTO t1 SELECT a+4, b||b FROM t1;
1.39 + SELECT count(*), max(length(b)) FROM t1;
1.40 + }
1.41 +} {8 64}
1.42 +
1.43 +# Interleave two threads on read access. Then make sure a third
1.44 +# thread can write the database. In other words:
1.45 +#
1.46 +# read-lock A
1.47 +# read-lock B
1.48 +# unlock A
1.49 +# unlock B
1.50 +# write-lock C
1.51 +#
1.52 +# At one point, the write-lock of C would fail on Linux.
1.53 +#
1.54 +do_test thread1-1.2 {
1.55 + thread_create A test.db
1.56 + thread_create B test.db
1.57 + thread_create C test.db
1.58 + thread_compile A {SELECT a FROM t1}
1.59 + thread_step A
1.60 + thread_result A
1.61 +} SQLITE_ROW
1.62 +do_test thread1-1.3 {
1.63 + thread_argc A
1.64 +} 1
1.65 +do_test thread1-1.4 {
1.66 + thread_argv A 0
1.67 +} 1
1.68 +do_test thread1-1.5 {
1.69 + thread_compile B {SELECT b FROM t1}
1.70 + thread_step B
1.71 + thread_result B
1.72 +} SQLITE_ROW
1.73 +do_test thread1-1.6 {
1.74 + thread_argc B
1.75 +} 1
1.76 +do_test thread1-1.7 {
1.77 + thread_argv B 0
1.78 +} abcdefgh
1.79 +do_test thread1-1.8 {
1.80 + thread_finalize A
1.81 + thread_result A
1.82 +} SQLITE_OK
1.83 +do_test thread1-1.9 {
1.84 + thread_finalize B
1.85 + thread_result B
1.86 +} SQLITE_OK
1.87 +do_test thread1-1.10 {
1.88 + thread_compile C {CREATE TABLE t2(x,y)}
1.89 + thread_step C
1.90 + thread_result C
1.91 +} SQLITE_DONE
1.92 +do_test thread1-1.11 {
1.93 + thread_finalize C
1.94 + thread_result C
1.95 +} SQLITE_OK
1.96 +do_test thread1-1.12 {
1.97 + catchsql {SELECT name FROM sqlite_master}
1.98 + execsql {SELECT name FROM sqlite_master}
1.99 +} {t1 t2}
1.100 +
1.101 +
1.102 +#
1.103 +# The following tests - thread1-2.* - test the following scenario:
1.104 +#
1.105 +# 1: Read-lock thread A
1.106 +# 2: Read-lock thread B
1.107 +# 3: Attempt to write in thread C -> SQLITE_BUSY
1.108 +# 4: Check db write failed from main thread.
1.109 +# 5: Unlock from thread A.
1.110 +# 6: Attempt to write in thread C -> SQLITE_BUSY
1.111 +# 7: Check db write failed from main thread.
1.112 +# 8: Unlock from thread B.
1.113 +# 9: Attempt to write in thread C -> SQLITE_DONE
1.114 +# 10: Finalize the write from thread C
1.115 +# 11: Check db write succeeded from main thread.
1.116 +#
1.117 +do_test thread1-2.1 {
1.118 + thread_halt *
1.119 + thread_create A test.db
1.120 + thread_compile A {SELECT a FROM t1}
1.121 + thread_step A
1.122 + thread_result A
1.123 +} SQLITE_ROW
1.124 +do_test thread1-2.2 {
1.125 + thread_create B test.db
1.126 + thread_compile B {SELECT b FROM t1}
1.127 + thread_step B
1.128 + thread_result B
1.129 +} SQLITE_ROW
1.130 +do_test thread1-2.3 {
1.131 + thread_create C test.db
1.132 + thread_compile C {INSERT INTO t2 VALUES(98,99)}
1.133 + thread_step C
1.134 + thread_result C
1.135 + thread_finalize C
1.136 + thread_result C
1.137 +} SQLITE_BUSY
1.138 +
1.139 +do_test thread1-2.4 {
1.140 + execsql {SELECT * FROM t2}
1.141 +} {}
1.142 +
1.143 +do_test thread1-2.5 {
1.144 + thread_finalize A
1.145 + thread_result A
1.146 +} SQLITE_OK
1.147 +do_test thread1-2.6 {
1.148 + thread_compile C {INSERT INTO t2 VALUES(98,99)}
1.149 + thread_step C
1.150 + thread_result C
1.151 + thread_finalize C
1.152 + thread_result C
1.153 +} SQLITE_BUSY
1.154 +do_test thread1-2.7 {
1.155 + execsql {SELECT * FROM t2}
1.156 +} {}
1.157 +do_test thread1-2.8 {
1.158 + thread_finalize B
1.159 + thread_result B
1.160 +} SQLITE_OK
1.161 +do_test thread1-2.9 {
1.162 + thread_compile C {INSERT INTO t2 VALUES(98,99)}
1.163 + thread_step C
1.164 + thread_result C
1.165 +} SQLITE_DONE
1.166 +do_test thread1-2.10 {
1.167 + thread_finalize C
1.168 + thread_result C
1.169 +} SQLITE_OK
1.170 +do_test thread1-2.11 {
1.171 + execsql {SELECT * FROM t2}
1.172 +} {98 99}
1.173 +
1.174 +thread_halt *
1.175 +finish_test