drh | a6064dc | 2003-12-19 02:52:05 +0000 | [diff] [blame] | 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 | # |
drh | 18472fa | 2008-10-07 15:25:48 +0000 | [diff] [blame] | 14 | # $Id: thread1.test,v 1.8 2008/10/07 15:25:49 drh Exp $ |
drh | a6064dc | 2003-12-19 02:52:05 +0000 | [diff] [blame] | 15 | |
| 16 | |
| 17 | set testdir [file dirname $argv0] |
| 18 | source $testdir/tester.tcl |
| 19 | |
| 20 | # Skip this whole file if the thread testing code is not enabled |
| 21 | # |
drh | 18472fa | 2008-10-07 15:25:48 +0000 | [diff] [blame] | 22 | ifcapable !mutex { |
| 23 | finish_test |
| 24 | return |
| 25 | } |
drh | ef4ac8f | 2004-06-19 00:16:31 +0000 | [diff] [blame] | 26 | if {[llength [info command thread_step]]==0 || [sqlite3 -has-codec]} { |
drh | a6064dc | 2003-12-19 02:52:05 +0000 | [diff] [blame] | 27 | finish_test |
| 28 | return |
| 29 | } |
| 30 | |
| 31 | # Create some data to work with |
| 32 | # |
| 33 | do_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 |
drh | acf01e7 | 2003-12-19 08:40:22 +0000 | [diff] [blame] | 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. |
drh | a6064dc | 2003-12-19 02:52:05 +0000 | [diff] [blame] | 54 | # |
| 55 | do_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 |
| 63 | do_test thread1-1.3 { |
| 64 | thread_argc A |
| 65 | } 1 |
| 66 | do_test thread1-1.4 { |
| 67 | thread_argv A 0 |
| 68 | } 1 |
| 69 | do_test thread1-1.5 { |
| 70 | thread_compile B {SELECT b FROM t1} |
| 71 | thread_step B |
| 72 | thread_result B |
| 73 | } SQLITE_ROW |
| 74 | do_test thread1-1.6 { |
| 75 | thread_argc B |
| 76 | } 1 |
| 77 | do_test thread1-1.7 { |
| 78 | thread_argv B 0 |
| 79 | } abcdefgh |
| 80 | do_test thread1-1.8 { |
| 81 | thread_finalize A |
| 82 | thread_result A |
| 83 | } SQLITE_OK |
| 84 | do_test thread1-1.9 { |
| 85 | thread_finalize B |
| 86 | thread_result B |
| 87 | } SQLITE_OK |
| 88 | do_test thread1-1.10 { |
| 89 | thread_compile C {CREATE TABLE t2(x,y)} |
| 90 | thread_step C |
| 91 | thread_result C |
| 92 | } SQLITE_DONE |
| 93 | do_test thread1-1.11 { |
| 94 | thread_finalize C |
| 95 | thread_result C |
| 96 | } SQLITE_OK |
drh | acf01e7 | 2003-12-19 08:40:22 +0000 | [diff] [blame] | 97 | do_test thread1-1.12 { |
| 98 | catchsql {SELECT name FROM sqlite_master} |
| 99 | execsql {SELECT name FROM sqlite_master} |
| 100 | } {t1 t2} |
drh | a6064dc | 2003-12-19 02:52:05 +0000 | [diff] [blame] | 101 | |
| 102 | |
drh | acf01e7 | 2003-12-19 08:40:22 +0000 | [diff] [blame] | 103 | # |
danielk1977 | f9d19a6 | 2004-06-14 08:26:35 +0000 | [diff] [blame] | 104 | # The following tests - thread1-2.* - test the following scenario: |
drh | acf01e7 | 2003-12-19 08:40:22 +0000 | [diff] [blame] | 105 | # |
danielk1977 | f9d19a6 | 2004-06-14 08:26:35 +0000 | [diff] [blame] | 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. |
drh | acf01e7 | 2003-12-19 08:40:22 +0000 | [diff] [blame] | 117 | # |
| 118 | do_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 |
| 125 | do_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 |
| 131 | do_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 |
danielk1977 | 0de0bb3 | 2004-06-10 05:59:24 +0000 | [diff] [blame] | 136 | thread_finalize C |
| 137 | thread_result C |
drh | acf01e7 | 2003-12-19 08:40:22 +0000 | [diff] [blame] | 138 | } SQLITE_BUSY |
danielk1977 | 0de0bb3 | 2004-06-10 05:59:24 +0000 | [diff] [blame] | 139 | |
drh | acf01e7 | 2003-12-19 08:40:22 +0000 | [diff] [blame] | 140 | do_test thread1-2.4 { |
| 141 | execsql {SELECT * FROM t2} |
| 142 | } {} |
danielk1977 | 0de0bb3 | 2004-06-10 05:59:24 +0000 | [diff] [blame] | 143 | |
drh | acf01e7 | 2003-12-19 08:40:22 +0000 | [diff] [blame] | 144 | do_test thread1-2.5 { |
| 145 | thread_finalize A |
| 146 | thread_result A |
| 147 | } SQLITE_OK |
| 148 | do_test thread1-2.6 { |
danielk1977 | 0de0bb3 | 2004-06-10 05:59:24 +0000 | [diff] [blame] | 149 | thread_compile C {INSERT INTO t2 VALUES(98,99)} |
drh | acf01e7 | 2003-12-19 08:40:22 +0000 | [diff] [blame] | 150 | thread_step C |
| 151 | thread_result C |
danielk1977 | 0de0bb3 | 2004-06-10 05:59:24 +0000 | [diff] [blame] | 152 | thread_finalize C |
| 153 | thread_result C |
drh | acf01e7 | 2003-12-19 08:40:22 +0000 | [diff] [blame] | 154 | } SQLITE_BUSY |
| 155 | do_test thread1-2.7 { |
| 156 | execsql {SELECT * FROM t2} |
| 157 | } {} |
| 158 | do_test thread1-2.8 { |
| 159 | thread_finalize B |
| 160 | thread_result B |
| 161 | } SQLITE_OK |
| 162 | do_test thread1-2.9 { |
danielk1977 | 0de0bb3 | 2004-06-10 05:59:24 +0000 | [diff] [blame] | 163 | thread_compile C {INSERT INTO t2 VALUES(98,99)} |
drh | acf01e7 | 2003-12-19 08:40:22 +0000 | [diff] [blame] | 164 | thread_step C |
| 165 | thread_result C |
| 166 | } SQLITE_DONE |
| 167 | do_test thread1-2.10 { |
drh | acf01e7 | 2003-12-19 08:40:22 +0000 | [diff] [blame] | 168 | thread_finalize C |
| 169 | thread_result C |
| 170 | } SQLITE_OK |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 171 | do_test thread1-2.11 { |
| 172 | execsql {SELECT * FROM t2} |
| 173 | } {98 99} |
drh | acf01e7 | 2003-12-19 08:40:22 +0000 | [diff] [blame] | 174 | |
drh | a6064dc | 2003-12-19 02:52:05 +0000 | [diff] [blame] | 175 | thread_halt * |
| 176 | finish_test |