drh | 7910e76 | 2006-01-09 23:50:11 +0000 | [diff] [blame] | 1 | # 2006 January 09 |
| 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 testing the server mode of SQLite. |
| 13 | # |
| 14 | # This file is derived from thread1.test |
| 15 | # |
drh | df12a9b | 2007-08-29 18:20:16 +0000 | [diff] [blame] | 16 | # $Id: server1.test,v 1.5 2007/08/29 18:20:17 drh Exp $ |
drh | 7910e76 | 2006-01-09 23:50:11 +0000 | [diff] [blame] | 17 | |
| 18 | |
| 19 | set testdir [file dirname $argv0] |
| 20 | source $testdir/tester.tcl |
| 21 | |
| 22 | # Skip this whole file if the server testing code is not enabled |
| 23 | # |
| 24 | if {[llength [info command client_step]]==0 || [sqlite3 -has-codec]} { |
| 25 | finish_test |
| 26 | return |
| 27 | } |
| 28 | |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 29 | # The sample server implementation does not work right when memory |
| 30 | # management is enabled. |
| 31 | # |
drh | 1b1e8a8 | 2011-08-31 19:40:58 +0000 | [diff] [blame] | 32 | ifcapable (memorymanage||mutex_noop) { |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 33 | finish_test |
| 34 | return |
| 35 | } |
| 36 | |
drh | 7910e76 | 2006-01-09 23:50:11 +0000 | [diff] [blame] | 37 | # Create some data to work with |
| 38 | # |
| 39 | do_test server1-1.1 { |
| 40 | execsql { |
| 41 | CREATE TABLE t1(a,b); |
| 42 | INSERT INTO t1 VALUES(1,'abcdefgh'); |
| 43 | INSERT INTO t1 SELECT a+1, b||b FROM t1; |
| 44 | INSERT INTO t1 SELECT a+2, b||b FROM t1; |
| 45 | INSERT INTO t1 SELECT a+4, b||b FROM t1; |
| 46 | SELECT count(*), max(length(b)) FROM t1; |
| 47 | } |
| 48 | } {8 64} |
| 49 | |
| 50 | # Interleave two threads on read access. Then make sure a third |
| 51 | # thread can write the database. In other words: |
| 52 | # |
| 53 | # read-lock A |
| 54 | # read-lock B |
| 55 | # unlock A |
| 56 | # unlock B |
| 57 | # write-lock C |
| 58 | # |
| 59 | do_test server1-1.2 { |
| 60 | client_create A test.db |
| 61 | client_create B test.db |
| 62 | client_create C test.db |
| 63 | client_compile A {SELECT a FROM t1} |
| 64 | client_step A |
| 65 | client_result A |
| 66 | } SQLITE_ROW |
| 67 | do_test server1-1.3 { |
| 68 | client_argc A |
| 69 | } 1 |
| 70 | do_test server1-1.4 { |
| 71 | client_argv A 0 |
| 72 | } 1 |
| 73 | do_test server1-1.5 { |
| 74 | client_compile B {SELECT b FROM t1} |
| 75 | client_step B |
| 76 | client_result B |
| 77 | } SQLITE_ROW |
| 78 | do_test server1-1.6 { |
| 79 | client_argc B |
| 80 | } 1 |
| 81 | do_test server1-1.7 { |
| 82 | client_argv B 0 |
| 83 | } abcdefgh |
| 84 | do_test server1-1.8 { |
| 85 | client_finalize A |
| 86 | client_result A |
| 87 | } SQLITE_OK |
| 88 | do_test server1-1.9 { |
| 89 | client_finalize B |
| 90 | client_result B |
| 91 | } SQLITE_OK |
| 92 | do_test server1-1.10 { |
| 93 | client_compile C {CREATE TABLE t2(x,y)} |
| 94 | client_step C |
| 95 | client_result C |
| 96 | } SQLITE_DONE |
| 97 | do_test server1-1.11 { |
| 98 | client_finalize C |
| 99 | client_result C |
| 100 | } SQLITE_OK |
| 101 | do_test server1-1.12 { |
| 102 | catchsql {SELECT name FROM sqlite_master} |
| 103 | execsql {SELECT name FROM sqlite_master} |
| 104 | } {t1 t2} |
| 105 | |
| 106 | |
drh | bdd6da2 | 2006-01-10 02:30:33 +0000 | [diff] [blame] | 107 | # Read from table t1. Do not finalize the statement. This |
| 108 | # will leave the lock pending. |
drh | 7910e76 | 2006-01-09 23:50:11 +0000 | [diff] [blame] | 109 | # |
| 110 | do_test server1-2.1 { |
| 111 | client_halt * |
| 112 | client_create A test.db |
| 113 | client_compile A {SELECT a FROM t1} |
| 114 | client_step A |
| 115 | client_result A |
| 116 | } SQLITE_ROW |
drh | bdd6da2 | 2006-01-10 02:30:33 +0000 | [diff] [blame] | 117 | |
| 118 | # Read from the same table from another thread. This is allows. |
| 119 | # |
drh | 7910e76 | 2006-01-09 23:50:11 +0000 | [diff] [blame] | 120 | do_test server1-2.2 { |
| 121 | client_create B test.db |
| 122 | client_compile B {SELECT b FROM t1} |
| 123 | client_step B |
| 124 | client_result B |
| 125 | } SQLITE_ROW |
drh | bdd6da2 | 2006-01-10 02:30:33 +0000 | [diff] [blame] | 126 | |
| 127 | # Write to a different table from another thread. This is allowed |
drh | df12a9b | 2007-08-29 18:20:16 +0000 | [diff] [blame] | 128 | # because in server mode with a shared cache we have table-level locking. |
drh | bdd6da2 | 2006-01-10 02:30:33 +0000 | [diff] [blame] | 129 | # |
drh | 7910e76 | 2006-01-09 23:50:11 +0000 | [diff] [blame] | 130 | do_test server1-2.3 { |
| 131 | client_create C test.db |
| 132 | client_compile C {INSERT INTO t2 VALUES(98,99)} |
| 133 | client_step C |
| 134 | client_result C |
| 135 | client_finalize C |
| 136 | client_result C |
drh | bdd6da2 | 2006-01-10 02:30:33 +0000 | [diff] [blame] | 137 | } SQLITE_OK |
drh | 7910e76 | 2006-01-09 23:50:11 +0000 | [diff] [blame] | 138 | |
drh | bdd6da2 | 2006-01-10 02:30:33 +0000 | [diff] [blame] | 139 | # But we cannot insert into table t1 because threads A and B have it locked. |
| 140 | # |
drh | 7910e76 | 2006-01-09 23:50:11 +0000 | [diff] [blame] | 141 | do_test server1-2.4 { |
drh | bdd6da2 | 2006-01-10 02:30:33 +0000 | [diff] [blame] | 142 | client_compile C {INSERT INTO t1 VALUES(98,99)} |
| 143 | client_step C |
| 144 | client_result C |
| 145 | client_finalize C |
| 146 | client_result C |
| 147 | } SQLITE_LOCKED |
drh | 7910e76 | 2006-01-09 23:50:11 +0000 | [diff] [blame] | 148 | do_test server1-2.5 { |
drh | 7910e76 | 2006-01-09 23:50:11 +0000 | [diff] [blame] | 149 | client_finalize B |
drh | 02d9eca | 2006-01-10 20:36:39 +0000 | [diff] [blame] | 150 | client_wait B |
drh | bdd6da2 | 2006-01-10 02:30:33 +0000 | [diff] [blame] | 151 | client_compile C {INSERT INTO t1 VALUES(98,99)} |
drh | 7910e76 | 2006-01-09 23:50:11 +0000 | [diff] [blame] | 152 | client_step C |
| 153 | client_result C |
drh | bdd6da2 | 2006-01-10 02:30:33 +0000 | [diff] [blame] | 154 | client_finalize C |
| 155 | client_result C |
| 156 | } SQLITE_LOCKED |
| 157 | |
| 158 | # Insert into t1 is successful after finishing the other two threads. |
| 159 | do_test server1-2.6 { |
| 160 | client_finalize A |
drh | 02d9eca | 2006-01-10 20:36:39 +0000 | [diff] [blame] | 161 | client_wait A |
drh | bdd6da2 | 2006-01-10 02:30:33 +0000 | [diff] [blame] | 162 | client_compile C {INSERT INTO t1 VALUES(98,99)} |
| 163 | client_step C |
| 164 | client_result C |
drh | 7910e76 | 2006-01-09 23:50:11 +0000 | [diff] [blame] | 165 | client_finalize C |
| 166 | client_result C |
| 167 | } SQLITE_OK |
drh | 7910e76 | 2006-01-09 23:50:11 +0000 | [diff] [blame] | 168 | |
| 169 | client_halt * |
drh | df12a9b | 2007-08-29 18:20:16 +0000 | [diff] [blame] | 170 | sqlite3_enable_shared_cache 0 |
drh | 7910e76 | 2006-01-09 23:50:11 +0000 | [diff] [blame] | 171 | finish_test |