| 1 |
# 2010 November 1 |
| 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 file is testing that WAL databases may be accessed without |
| 13 |
# using the xShm primitives if the connection is in exclusive-mode. |
| 14 |
# |
| 15 |
|
| 16 |
set testdir [file dirname $argv0] |
| 17 |
source $testdir/tester.tcl |
| 18 |
set testprefix walnoshm |
| 19 |
ifcapable !wal {finish_test ; return } |
| 20 |
|
| 21 |
db close |
| 22 |
testvfs tvfsshm |
| 23 |
testvfs tvfs -default 1 -iversion 1 |
| 24 |
sqlite3 db test.db |
| 25 |
|
| 26 |
#-------------------------------------------------------------------------- |
| 27 |
# Test that when using a version 1 VFS, a database can only be converted |
| 28 |
# to WAL mode after setting locking_mode=EXCLUSIVE. Also, test that if a |
| 29 |
# WAL database is opened using heap-memory for the WAL index, the connection |
| 30 |
# cannot change back to locking_mode=NORMAL while the database is still in |
| 31 |
# WAL mode. |
| 32 |
# |
| 33 |
do_execsql_test 1.1 { |
| 34 |
CREATE TABLE t1(x, y); |
| 35 |
INSERT INTO t1 VALUES(1, 2); |
| 36 |
} |
| 37 |
|
| 38 |
do_execsql_test 1.2 { |
| 39 |
PRAGMA journal_mode = WAL; |
| 40 |
SELECT * FROM t1; |
| 41 |
} {delete 1 2} |
| 42 |
do_test 1.3 { file exists test.db-wal } {0} |
| 43 |
|
| 44 |
do_execsql_test 1.4 { |
| 45 |
PRAGMA locking_mode = exclusive; |
| 46 |
PRAGMA journal_mode = WAL; |
| 47 |
SELECT * FROM t1; |
| 48 |
} {exclusive wal 1 2} |
| 49 |
do_test 1.5 { file exists test.db-wal } {1} |
| 50 |
|
| 51 |
do_execsql_test 1.6 { INSERT INTO t1 VALUES(3, 4) } |
| 52 |
|
| 53 |
do_execsql_test 1.7 { |
| 54 |
PRAGMA locking_mode = normal; |
| 55 |
} {exclusive} |
| 56 |
do_execsql_test 1.8 { |
| 57 |
PRAGMA journal_mode = delete; |
| 58 |
PRAGMA main.locking_mode; |
| 59 |
} {delete exclusive} |
| 60 |
do_execsql_test 1.9 { |
| 61 |
PRAGMA locking_mode = normal; |
| 62 |
} {normal} |
| 63 |
do_execsql_test 1.10 { |
| 64 |
SELECT * FROM t1; |
| 65 |
} {1 2 3 4} |
| 66 |
do_test 1.11 { file exists test.db-wal } {0} |
| 67 |
|
| 68 |
#------------------------------------------------------------------------- |
| 69 |
# |
| 70 |
# 2.1.*: Test that a connection using a version 1 VFS can open a WAL database |
| 71 |
# and convert it to rollback mode if it is set to use |
| 72 |
# locking_mode=exclusive. |
| 73 |
# |
| 74 |
# 2.2.*: Test that if the exclusive lock cannot be obtained while attempting |
| 75 |
# the above, the operation fails and the WAL file is not opened. |
| 76 |
# |
| 77 |
do_execsql_test 2.1.1 { |
| 78 |
CREATE TABLE t2(x, y); |
| 79 |
INSERT INTO t2 VALUES('a', 'b'); |
| 80 |
INSERT INTO t2 VALUES('c', 'd'); |
| 81 |
} |
| 82 |
do_execsql_test 2.1.2 { |
| 83 |
PRAGMA locking_mode = exclusive; |
| 84 |
PRAGMA journal_mode = WAL; |
| 85 |
INSERT INTO t2 VALUES('e', 'f'); |
| 86 |
INSERT INTO t2 VALUES('g', 'h'); |
| 87 |
} {exclusive wal} |
| 88 |
|
| 89 |
do_test 2.1.3 { |
| 90 |
forcecopy test.db test2.db |
| 91 |
forcecopy test.db-wal test2.db-wal |
| 92 |
sqlite3 db2 test2.db |
| 93 |
catchsql { SELECT * FROM t2 } db2 |
| 94 |
} {1 {unable to open database file}} |
| 95 |
do_test 2.1.4 { |
| 96 |
catchsql { PRAGMA journal_mode = delete } db2 |
| 97 |
} {1 {unable to open database file}} |
| 98 |
do_test 2.1.5 { |
| 99 |
execsql { |
| 100 |
PRAGMA locking_mode = exclusive; |
| 101 |
PRAGMA journal_mode = delete; |
| 102 |
SELECT * FROM t2; |
| 103 |
} db2 |
| 104 |
} {exclusive delete a b c d e f g h} |
| 105 |
|
| 106 |
do_test 2.2.1 { |
| 107 |
db2 close |
| 108 |
forcecopy test.db test2.db |
| 109 |
forcecopy test.db-wal test2.db-wal |
| 110 |
sqlite3 db3 test2.db -vfs tvfsshm |
| 111 |
sqlite3 db2 test2.db |
| 112 |
execsql { SELECT * FROM t2 } db3 |
| 113 |
} {a b c d e f g h} |
| 114 |
|
| 115 |
do_test 2.2.2 { |
| 116 |
execsql { PRAGMA locking_mode = exclusive } db2 |
| 117 |
catchsql { PRAGMA journal_mode = delete } db2 |
| 118 |
} {1 {database is locked}} |
| 119 |
|
| 120 |
do_test 2.2.3 { |
| 121 |
# This is to test that [db2] is not holding a PENDING lock (which can |
| 122 |
# happen when an attempt to obtain an EXCLUSIVE lock fails). |
| 123 |
sqlite3 db4 test2.db -vfs tvfsshm |
| 124 |
execsql { SELECT * FROM t2 } db4 |
| 125 |
} {a b c d e f g h} |
| 126 |
|
| 127 |
do_test 2.2.4 { |
| 128 |
catchsql { SELECT * FROM t2 } db2 |
| 129 |
} {1 {database is locked}} |
| 130 |
|
| 131 |
do_test 2.2.5 { |
| 132 |
db4 close |
| 133 |
sqlite3 db4 test2.db -vfs tvfsshm |
| 134 |
execsql { SELECT * FROM t2 } db4 |
| 135 |
} {a b c d e f g h} |
| 136 |
|
| 137 |
do_test 2.2.6 { |
| 138 |
db3 close |
| 139 |
db4 close |
| 140 |
execsql { SELECT * FROM t2 } db2 |
| 141 |
} {a b c d e f g h} |
| 142 |
|
| 143 |
db2 close |
| 144 |
db close |
| 145 |
|
| 146 |
#------------------------------------------------------------------------- |
| 147 |
# |
| 148 |
# 3.1: Test that if locking_mode=EXCLUSIVE is set after the wal file is |
| 149 |
# opened, it is possible to drop back to locking_mode=NORMAL. |
| 150 |
# |
| 151 |
# 3.2: Test that if locking_mode=EXCLUSIVE is set before the wal file is |
| 152 |
# opened, it is not. |
| 153 |
# |
| 154 |
do_test 3.1 { |
| 155 |
sqlite3 db test.db -vfs tvfsshm |
| 156 |
execsql { |
| 157 |
SELECT * FROM t1; |
| 158 |
PRAGMA locking_mode = EXCLUSIVE; |
| 159 |
INSERT INTO t1 VALUES(5, 6); |
| 160 |
PRAGMA locking_mode = NORMAL; |
| 161 |
INSERT INTO t1 VALUES(7, 8); |
| 162 |
} |
| 163 |
sqlite3 db2 test.db -vfs tvfsshm |
| 164 |
execsql { SELECT * FROM t1 } db2 |
| 165 |
} {1 2 3 4 5 6 7 8} |
| 166 |
db close |
| 167 |
db2 close |
| 168 |
do_test 3.2 { |
| 169 |
sqlite3 db test.db -vfs tvfsshm |
| 170 |
execsql { |
| 171 |
PRAGMA locking_mode = EXCLUSIVE; |
| 172 |
INSERT INTO t1 VALUES(9, 10); |
| 173 |
PRAGMA locking_mode = NORMAL; |
| 174 |
INSERT INTO t1 VALUES(11, 12); |
| 175 |
} |
| 176 |
sqlite3 db2 test.db -vfs tvfsshm |
| 177 |
catchsql { SELECT * FROM t1 } db2 |
| 178 |
} {1 {database is locked}} |
| 179 |
db close |
| 180 |
db2 close |
| 181 |
|
| 182 |
tvfs delete |
| 183 |
tvfsshm delete |
| 184 |
|
| 185 |
finish_test |