danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 1 | # 2005 December 30 |
| 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 | # |
| 12 | # The focus of the tests in this file are IO errors that occur in a shared |
| 13 | # cache context. What happens to connection B if one connection A encounters |
| 14 | # an IO-error whilst reading or writing the file-system? |
| 15 | # |
danielk1977 | 7eaabcd | 2008-07-07 14:56:56 +0000 | [diff] [blame] | 16 | # $Id: shared_err.test,v 1.20 2008/07/07 14:56:57 danielk1977 Exp $ |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 17 | |
| 18 | proc skip {args} {} |
| 19 | |
| 20 | |
| 21 | set testdir [file dirname $argv0] |
| 22 | source $testdir/tester.tcl |
danielk1977 | 222a757 | 2007-08-25 13:37:48 +0000 | [diff] [blame] | 23 | source $testdir/malloc_common.tcl |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 24 | db close |
| 25 | |
| 26 | ifcapable !shared_cache||!subquery { |
| 27 | finish_test |
| 28 | return |
| 29 | } |
danielk1977 | 222a757 | 2007-08-25 13:37:48 +0000 | [diff] [blame] | 30 | |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 31 | set ::enable_shared_cache [sqlite3_enable_shared_cache 1] |
| 32 | |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 33 | do_ioerr_test shared_ioerr-1 -tclprep { |
| 34 | sqlite3 db2 test.db |
| 35 | execsql { |
| 36 | PRAGMA read_uncommitted = 1; |
| 37 | CREATE TABLE t1(a,b,c); |
| 38 | BEGIN; |
| 39 | SELECT * FROM sqlite_master; |
| 40 | } db2 |
| 41 | } -sqlbody { |
| 42 | SELECT * FROM sqlite_master; |
| 43 | INSERT INTO t1 VALUES(1,2,3); |
| 44 | BEGIN TRANSACTION; |
| 45 | INSERT INTO t1 VALUES(1,2,3); |
| 46 | INSERT INTO t1 VALUES(4,5,6); |
| 47 | ROLLBACK; |
| 48 | SELECT * FROM t1; |
| 49 | BEGIN TRANSACTION; |
| 50 | INSERT INTO t1 VALUES(1,2,3); |
| 51 | INSERT INTO t1 VALUES(4,5,6); |
| 52 | COMMIT; |
| 53 | SELECT * FROM t1; |
| 54 | DELETE FROM t1 WHERE a<100; |
| 55 | } -cleanup { |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 56 | do_test shared_ioerr-1.$n.cleanup.1 { |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 57 | set res [catchsql { |
| 58 | SELECT * FROM t1; |
| 59 | } db2] |
| 60 | set possible_results [list \ |
| 61 | "1 {disk I/O error}" \ |
| 62 | "0 {1 2 3}" \ |
| 63 | "0 {1 2 3 1 2 3 4 5 6}" \ |
| 64 | "0 {1 2 3 1 2 3 4 5 6 1 2 3 4 5 6}" \ |
| 65 | "0 {}" \ |
| 66 | ] |
| 67 | set rc [expr [lsearch -exact $possible_results $res] >= 0] |
| 68 | if {$rc != 1} { |
| 69 | puts "" |
| 70 | puts "Result: $res" |
| 71 | } |
| 72 | set rc |
| 73 | } {1} |
| 74 | db2 close |
| 75 | } |
| 76 | |
| 77 | do_ioerr_test shared_ioerr-2 -tclprep { |
| 78 | sqlite3 db2 test.db |
| 79 | execsql { |
| 80 | PRAGMA read_uncommitted = 1; |
| 81 | BEGIN; |
| 82 | CREATE TABLE t1(a, b); |
| 83 | INSERT INTO t1(oid) VALUES(NULL); |
| 84 | INSERT INTO t1(oid) SELECT NULL FROM t1; |
| 85 | INSERT INTO t1(oid) SELECT NULL FROM t1; |
| 86 | INSERT INTO t1(oid) SELECT NULL FROM t1; |
| 87 | INSERT INTO t1(oid) SELECT NULL FROM t1; |
| 88 | INSERT INTO t1(oid) SELECT NULL FROM t1; |
| 89 | INSERT INTO t1(oid) SELECT NULL FROM t1; |
| 90 | INSERT INTO t1(oid) SELECT NULL FROM t1; |
| 91 | INSERT INTO t1(oid) SELECT NULL FROM t1; |
| 92 | INSERT INTO t1(oid) SELECT NULL FROM t1; |
| 93 | INSERT INTO t1(oid) SELECT NULL FROM t1; |
| 94 | UPDATE t1 set a = oid, b = 'abcdefghijklmnopqrstuvwxyz0123456789'; |
| 95 | CREATE INDEX i1 ON t1(a); |
| 96 | COMMIT; |
| 97 | BEGIN; |
| 98 | SELECT * FROM sqlite_master; |
| 99 | } db2 |
| 100 | } -tclbody { |
| 101 | set ::residx 0 |
| 102 | execsql {DELETE FROM t1 WHERE 0 = (a % 2);} |
| 103 | incr ::residx |
| 104 | |
| 105 | # When this transaction begins the table contains 512 entries. The |
| 106 | # two statements together add 512+146 more if it succeeds. |
| 107 | # (1024/7==146) |
| 108 | execsql {BEGIN;} |
| 109 | execsql {INSERT INTO t1 SELECT a+1, b FROM t1;} |
| 110 | execsql {INSERT INTO t1 SELECT 'string' || a, b FROM t1 WHERE 0 = (a%7);} |
| 111 | execsql {COMMIT;} |
| 112 | |
| 113 | incr ::residx |
| 114 | } -cleanup { |
drh | 1527ff4 | 2008-01-18 17:03:32 +0000 | [diff] [blame] | 115 | catchsql ROLLBACK |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 116 | do_test shared_ioerr-2.$n.cleanup.1 { |
| 117 | set res [catchsql { |
| 118 | SELECT max(a), min(a), count(*) FROM (SELECT a FROM t1 order by a); |
| 119 | } db2] |
| 120 | set possible_results [list \ |
| 121 | {0 {1024 1 1024}} \ |
| 122 | {0 {1023 1 512}} \ |
| 123 | {0 {string994 1 1170}} \ |
| 124 | ] |
| 125 | set idx [lsearch -exact $possible_results $res] |
| 126 | set success [expr {$idx==$::residx || $res=="1 {disk I/O error}"}] |
| 127 | if {!$success} { |
| 128 | puts "" |
| 129 | puts "Result: \"$res\" ($::residx)" |
| 130 | } |
| 131 | set success |
| 132 | } {1} |
| 133 | db2 close |
| 134 | } |
| 135 | |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 136 | # This test is designed to provoke an IO error when a cursor position is |
| 137 | # "saved" (because another cursor is going to modify the underlying table). |
| 138 | # |
| 139 | do_ioerr_test shared_ioerr-3 -tclprep { |
| 140 | sqlite3 db2 test.db |
| 141 | execsql { |
| 142 | PRAGMA read_uncommitted = 1; |
danielk1977 | c4da5b9 | 2006-01-21 12:08:54 +0000 | [diff] [blame] | 143 | PRAGMA cache_size = 10; |
| 144 | BEGIN; |
| 145 | CREATE TABLE t1(a, b, UNIQUE(a, b)); |
| 146 | } db2 |
| 147 | for {set i 0} {$i < 200} {incr i} { |
| 148 | set a [string range [string repeat "[format %03d $i]." 5] 0 end-1] |
| 149 | |
| 150 | set b [string repeat $i 2000] |
| 151 | execsql {INSERT INTO t1 VALUES($a, $b)} db2 |
| 152 | } |
| 153 | execsql {COMMIT} db2 |
| 154 | set ::DB2 [sqlite3_connection_pointer db2] |
| 155 | set ::STMT [sqlite3_prepare $::DB2 "SELECT a FROM t1 ORDER BY a" -1 DUMMY] |
| 156 | sqlite3_step $::STMT ;# Cursor points at 000.000.000.000 |
| 157 | sqlite3_step $::STMT ;# Cursor points at 001.001.001.001 |
| 158 | |
| 159 | } -tclbody { |
| 160 | execsql { |
danielk1977 | 75bab7d | 2006-01-23 13:09:45 +0000 | [diff] [blame] | 161 | BEGIN; |
danielk1977 | c4da5b9 | 2006-01-21 12:08:54 +0000 | [diff] [blame] | 162 | INSERT INTO t1 VALUES('201.201.201.201.201', NULL); |
danielk1977 | 75bab7d | 2006-01-23 13:09:45 +0000 | [diff] [blame] | 163 | UPDATE t1 SET a = '202.202.202.202.202' WHERE a LIKE '201%'; |
| 164 | COMMIT; |
danielk1977 | c4da5b9 | 2006-01-21 12:08:54 +0000 | [diff] [blame] | 165 | } |
| 166 | } -cleanup { |
danielk1977 | b94bf85 | 2007-03-19 13:53:37 +0000 | [diff] [blame] | 167 | set ::steprc [sqlite3_step $::STMT] |
| 168 | set ::column [sqlite3_column_text $::STMT 0] |
| 169 | set ::finalrc [sqlite3_finalize $::STMT] |
| 170 | |
| 171 | # There are three possible outcomes here (assuming persistent IO errors): |
| 172 | # |
| 173 | # 1. If the [sqlite3_step] did not require any IO (required pages in |
| 174 | # the cache), then the next row ("002...") may be retrieved |
| 175 | # successfully. |
| 176 | # |
| 177 | # 2. If the [sqlite3_step] does require IO, then [sqlite3_step] returns |
| 178 | # SQLITE_ERROR and [sqlite3_finalize] returns IOERR. |
| 179 | # |
| 180 | # 3. If, after the initial IO error, SQLite tried to rollback the |
| 181 | # active transaction and a second IO error was encountered, then |
| 182 | # statement $::STMT will have been aborted. This means [sqlite3_stmt] |
| 183 | # returns SQLITE_ABORT, and the statement cursor does not move. i.e. |
| 184 | # [sqlite3_column] still returns the current row ("001...") and |
| 185 | # [sqlite3_finalize] returns SQLITE_OK. |
| 186 | # |
| 187 | |
danielk1977 | c4da5b9 | 2006-01-21 12:08:54 +0000 | [diff] [blame] | 188 | do_test shared_ioerr-3.$n.cleanup.1 { |
danielk1977 | b94bf85 | 2007-03-19 13:53:37 +0000 | [diff] [blame] | 189 | expr { |
| 190 | $::steprc eq "SQLITE_ROW" || |
| 191 | $::steprc eq "SQLITE_ERROR" || |
| 192 | $::steprc eq "SQLITE_ABORT" |
| 193 | } |
| 194 | } {1} |
danielk1977 | c4da5b9 | 2006-01-21 12:08:54 +0000 | [diff] [blame] | 195 | do_test shared_ioerr-3.$n.cleanup.2 { |
danielk1977 | b94bf85 | 2007-03-19 13:53:37 +0000 | [diff] [blame] | 196 | expr { |
| 197 | ($::steprc eq "SQLITE_ROW" && $::column eq "002.002.002.002.002") || |
| 198 | ($::steprc eq "SQLITE_ERROR" && $::column eq "") || |
| 199 | ($::steprc eq "SQLITE_ABORT" && $::column eq "001.001.001.001.001") |
| 200 | } |
| 201 | } {1} |
danielk1977 | c4da5b9 | 2006-01-21 12:08:54 +0000 | [diff] [blame] | 202 | do_test shared_ioerr-3.$n.cleanup.3 { |
danielk1977 | b94bf85 | 2007-03-19 13:53:37 +0000 | [diff] [blame] | 203 | expr { |
| 204 | ($::steprc eq "SQLITE_ROW" && $::finalrc eq "SQLITE_OK") || |
| 205 | ($::steprc eq "SQLITE_ERROR" && $::finalrc eq "SQLITE_IOERR") || |
drh | c1a7f94 | 2007-08-30 02:26:53 +0000 | [diff] [blame] | 206 | ($::steprc eq "SQLITE_ERROR" && $::finalrc eq "SQLITE_ABORT") |
danielk1977 | b94bf85 | 2007-03-19 13:53:37 +0000 | [diff] [blame] | 207 | } |
| 208 | } {1} |
| 209 | |
danielk1977 | c4da5b9 | 2006-01-21 12:08:54 +0000 | [diff] [blame] | 210 | # db2 eval {select * from sqlite_master} |
| 211 | db2 close |
| 212 | } |
| 213 | |
drh | 77a2a5e | 2007-04-06 01:04:39 +0000 | [diff] [blame] | 214 | # This is a repeat of the previous test except that this time we |
| 215 | # are doing a reverse-order scan of the table when the cursor is |
| 216 | # "saved". |
| 217 | # |
| 218 | do_ioerr_test shared_ioerr-3rev -tclprep { |
| 219 | sqlite3 db2 test.db |
| 220 | execsql { |
| 221 | PRAGMA read_uncommitted = 1; |
| 222 | PRAGMA cache_size = 10; |
| 223 | BEGIN; |
| 224 | CREATE TABLE t1(a, b, UNIQUE(a, b)); |
| 225 | } db2 |
| 226 | for {set i 0} {$i < 200} {incr i} { |
| 227 | set a [string range [string repeat "[format %03d $i]." 5] 0 end-1] |
| 228 | |
| 229 | set b [string repeat $i 2000] |
| 230 | execsql {INSERT INTO t1 VALUES($a, $b)} db2 |
| 231 | } |
| 232 | execsql {COMMIT} db2 |
| 233 | set ::DB2 [sqlite3_connection_pointer db2] |
| 234 | set ::STMT [sqlite3_prepare $::DB2 \ |
| 235 | "SELECT a FROM t1 ORDER BY a DESC" -1 DUMMY] |
| 236 | sqlite3_step $::STMT ;# Cursor points at 199.199.199.199.199 |
| 237 | sqlite3_step $::STMT ;# Cursor points at 198.198.198.198.198 |
| 238 | |
| 239 | } -tclbody { |
| 240 | execsql { |
| 241 | BEGIN; |
| 242 | INSERT INTO t1 VALUES('201.201.201.201.201', NULL); |
| 243 | UPDATE t1 SET a = '202.202.202.202.202' WHERE a LIKE '201%'; |
| 244 | COMMIT; |
| 245 | } |
| 246 | } -cleanup { |
| 247 | set ::steprc [sqlite3_step $::STMT] |
| 248 | set ::column [sqlite3_column_text $::STMT 0] |
| 249 | set ::finalrc [sqlite3_finalize $::STMT] |
| 250 | |
| 251 | # There are three possible outcomes here (assuming persistent IO errors): |
| 252 | # |
| 253 | # 1. If the [sqlite3_step] did not require any IO (required pages in |
| 254 | # the cache), then the next row ("002...") may be retrieved |
| 255 | # successfully. |
| 256 | # |
| 257 | # 2. If the [sqlite3_step] does require IO, then [sqlite3_step] returns |
| 258 | # SQLITE_ERROR and [sqlite3_finalize] returns IOERR. |
| 259 | # |
| 260 | # 3. If, after the initial IO error, SQLite tried to rollback the |
| 261 | # active transaction and a second IO error was encountered, then |
| 262 | # statement $::STMT will have been aborted. This means [sqlite3_stmt] |
| 263 | # returns SQLITE_ABORT, and the statement cursor does not move. i.e. |
| 264 | # [sqlite3_column] still returns the current row ("001...") and |
| 265 | # [sqlite3_finalize] returns SQLITE_OK. |
| 266 | # |
| 267 | |
| 268 | do_test shared_ioerr-3rev.$n.cleanup.1 { |
| 269 | expr { |
| 270 | $::steprc eq "SQLITE_ROW" || |
| 271 | $::steprc eq "SQLITE_ERROR" || |
| 272 | $::steprc eq "SQLITE_ABORT" |
| 273 | } |
| 274 | } {1} |
| 275 | do_test shared_ioerr-3rev.$n.cleanup.2 { |
| 276 | expr { |
| 277 | ($::steprc eq "SQLITE_ROW" && $::column eq "197.197.197.197.197") || |
| 278 | ($::steprc eq "SQLITE_ERROR" && $::column eq "") || |
| 279 | ($::steprc eq "SQLITE_ABORT" && $::column eq "198.198.198.198.198") |
| 280 | } |
| 281 | } {1} |
| 282 | do_test shared_ioerr-3rev.$n.cleanup.3 { |
| 283 | expr { |
| 284 | ($::steprc eq "SQLITE_ROW" && $::finalrc eq "SQLITE_OK") || |
| 285 | ($::steprc eq "SQLITE_ERROR" && $::finalrc eq "SQLITE_IOERR") || |
drh | c1a7f94 | 2007-08-30 02:26:53 +0000 | [diff] [blame] | 286 | ($::steprc eq "SQLITE_ERROR" && $::finalrc eq "SQLITE_ABORT") |
drh | 77a2a5e | 2007-04-06 01:04:39 +0000 | [diff] [blame] | 287 | } |
| 288 | } {1} |
| 289 | |
| 290 | # db2 eval {select * from sqlite_master} |
| 291 | db2 close |
| 292 | } |
| 293 | |
drh | 7b3822b | 2006-01-23 23:49:34 +0000 | [diff] [blame] | 294 | # Only run these tests if memory debugging is turned on. |
| 295 | # |
drh | c1a7f94 | 2007-08-30 02:26:53 +0000 | [diff] [blame] | 296 | ifcapable !memdebug { |
drh | 5a3032b | 2007-09-03 16:12:09 +0000 | [diff] [blame] | 297 | puts "Skipping tests shared_err-4 through -9:\ |
| 298 | not compiled with -DSQLITE_MEMDEBUG..." |
drh | 80d5682 | 2006-01-24 00:15:15 +0000 | [diff] [blame] | 299 | db close |
| 300 | sqlite3_enable_shared_cache $::enable_shared_cache |
drh | 7b3822b | 2006-01-23 23:49:34 +0000 | [diff] [blame] | 301 | finish_test |
| 302 | return |
| 303 | } |
| 304 | |
danielk1977 | c4da5b9 | 2006-01-21 12:08:54 +0000 | [diff] [blame] | 305 | # Provoke a malloc() failure when a cursor position is being saved. This |
| 306 | # only happens with index cursors (because they malloc() space to save the |
| 307 | # current key value). It does not happen with tables, because an integer |
| 308 | # key does not require a malloc() to store. |
| 309 | # |
| 310 | # The library should return an SQLITE_NOMEM to the caller. The query that |
| 311 | # owns the cursor (the one for which the position is not saved) should |
| 312 | # continue unaffected. |
| 313 | # |
drh | c1a7f94 | 2007-08-30 02:26:53 +0000 | [diff] [blame] | 314 | do_malloc_test shared_err-4 -tclprep { |
danielk1977 | c4da5b9 | 2006-01-21 12:08:54 +0000 | [diff] [blame] | 315 | sqlite3 db2 test.db |
| 316 | execsql { |
| 317 | PRAGMA read_uncommitted = 1; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 318 | BEGIN; |
| 319 | CREATE TABLE t1(a, b, UNIQUE(a, b)); |
| 320 | } db2 |
| 321 | for {set i 0} {$i < 5} {incr i} { |
| 322 | set a [string repeat $i 10] |
| 323 | set b [string repeat $i 2000] |
| 324 | execsql {INSERT INTO t1 VALUES($a, $b)} db2 |
| 325 | } |
| 326 | execsql {COMMIT} db2 |
| 327 | set ::DB2 [sqlite3_connection_pointer db2] |
| 328 | set ::STMT [sqlite3_prepare $::DB2 "SELECT a FROM t1 ORDER BY a" -1 DUMMY] |
| 329 | sqlite3_step $::STMT ;# Cursor points at 0000000000 |
| 330 | sqlite3_step $::STMT ;# Cursor points at 1111111111 |
| 331 | } -tclbody { |
| 332 | execsql { |
| 333 | INSERT INTO t1 VALUES(6, NULL); |
| 334 | } |
| 335 | } -cleanup { |
danielk1977 | c4da5b9 | 2006-01-21 12:08:54 +0000 | [diff] [blame] | 336 | do_test shared_malloc-4.$::n.cleanup.1 { |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 337 | set ::rc [sqlite3_step $::STMT] |
drh | c1a7f94 | 2007-08-30 02:26:53 +0000 | [diff] [blame] | 338 | expr {$::rc=="SQLITE_ROW" || $::rc=="SQLITE_ERROR"} |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 339 | } {1} |
| 340 | if {$::rc=="SQLITE_ROW"} { |
| 341 | do_test shared_malloc-4.$::n.cleanup.2 { |
| 342 | sqlite3_column_text $::STMT 0 |
| 343 | } {2222222222} |
| 344 | } |
danielk1977 | c4da5b9 | 2006-01-21 12:08:54 +0000 | [diff] [blame] | 345 | do_test shared_malloc-4.$::n.cleanup.3 { |
drh | c1a7f94 | 2007-08-30 02:26:53 +0000 | [diff] [blame] | 346 | set rc [sqlite3_finalize $::STMT] |
drh | 1527ff4 | 2008-01-18 17:03:32 +0000 | [diff] [blame] | 347 | expr {$rc=="SQLITE_OK" || $rc=="SQLITE_ABORT" || |
| 348 | $rc=="SQLITE_NOMEM" || $rc=="SQLITE_IOERR"} |
drh | c1a7f94 | 2007-08-30 02:26:53 +0000 | [diff] [blame] | 349 | } {1} |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 350 | # db2 eval {select * from sqlite_master} |
| 351 | db2 close |
| 352 | } |
| 353 | |
drh | c1a7f94 | 2007-08-30 02:26:53 +0000 | [diff] [blame] | 354 | do_malloc_test shared_err-5 -tclbody { |
| 355 | db close |
danielk1977 | 4b202ae | 2006-01-23 05:50:58 +0000 | [diff] [blame] | 356 | sqlite3 dbX test.db |
| 357 | sqlite3 dbY test.db |
| 358 | dbX close |
| 359 | dbY close |
| 360 | } -cleanup { |
| 361 | catch {dbX close} |
| 362 | catch {dbY close} |
| 363 | } |
| 364 | |
drh | c1a7f94 | 2007-08-30 02:26:53 +0000 | [diff] [blame] | 365 | do_malloc_test shared_err-6 -tclbody { |
danielk1977 | 7246f5b | 2006-01-24 11:30:27 +0000 | [diff] [blame] | 366 | catch {db close} |
| 367 | sqlite3_thread_cleanup |
| 368 | sqlite3_enable_shared_cache 0 |
| 369 | } -cleanup { |
| 370 | sqlite3_enable_shared_cache 1 |
| 371 | } |
| 372 | |
drh | c1a7f94 | 2007-08-30 02:26:53 +0000 | [diff] [blame] | 373 | # As of 3.5.0, sqlite3_enable_shared_cache can be called at |
| 374 | # any time and from any thread |
| 375 | #do_test shared_err-misuse-7.1 { |
| 376 | # sqlite3 db test.db |
| 377 | # catch { |
| 378 | # sqlite3_enable_shared_cache 0 |
| 379 | # } msg |
| 380 | # set msg |
| 381 | #} {library routine called out of sequence} |
danielk1977 | 7246f5b | 2006-01-24 11:30:27 +0000 | [diff] [blame] | 382 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 383 | # Again provoke a malloc() failure when a cursor position is being saved, |
| 384 | # this time during a ROLLBACK operation by some other handle. |
| 385 | # |
| 386 | # The library should return an SQLITE_NOMEM to the caller. The query that |
| 387 | # owns the cursor (the one for which the position is not saved) should |
| 388 | # be aborted. |
| 389 | # |
| 390 | set ::aborted 0 |
drh | c1a7f94 | 2007-08-30 02:26:53 +0000 | [diff] [blame] | 391 | do_malloc_test shared_err-8 -tclprep { |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 392 | sqlite3 db2 test.db |
| 393 | execsql { |
| 394 | PRAGMA read_uncommitted = 1; |
| 395 | BEGIN; |
| 396 | CREATE TABLE t1(a, b, UNIQUE(a, b)); |
| 397 | } db2 |
| 398 | for {set i 0} {$i < 2} {incr i} { |
| 399 | set a [string repeat $i 10] |
| 400 | set b [string repeat $i 2000] |
| 401 | execsql {INSERT INTO t1 VALUES($a, $b)} db2 |
| 402 | } |
| 403 | execsql {COMMIT} db2 |
| 404 | set ::DB2 [sqlite3_connection_pointer db2] |
| 405 | set ::STMT [sqlite3_prepare $::DB2 "SELECT a FROM t1 ORDER BY a" -1 DUMMY] |
| 406 | sqlite3_step $::STMT ;# Cursor points at 0000000000 |
| 407 | sqlite3_step $::STMT ;# Cursor points at 1111111111 |
| 408 | } -tclbody { |
| 409 | execsql { |
| 410 | BEGIN; |
| 411 | INSERT INTO t1 VALUES(6, NULL); |
| 412 | ROLLBACK; |
| 413 | } |
| 414 | } -cleanup { |
| 415 | do_test shared_malloc-8.$::n.cleanup.1 { |
drh | 1527ff4 | 2008-01-18 17:03:32 +0000 | [diff] [blame] | 416 | set res [catchsql {SELECT a FROM t1} db2] |
| 417 | set ans [lindex $res 1] |
| 418 | if {[lindex $res 0]} { |
| 419 | set r [expr {$ans=="disk I/O error"}] |
| 420 | } else { |
| 421 | set r [expr {[lrange $ans 0 1]=="0000000000 1111111111"}] |
| 422 | } |
| 423 | } {1} |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 424 | do_test shared_malloc-8.$::n.cleanup.2 { |
| 425 | set rc1 [sqlite3_step $::STMT] |
| 426 | set rc2 [sqlite3_finalize $::STMT] |
drh | c1a7f94 | 2007-08-30 02:26:53 +0000 | [diff] [blame] | 427 | if {$rc2=="SQLITE_ABORT"} { |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 428 | incr ::aborted |
| 429 | } |
| 430 | expr { |
| 431 | ($rc1=="SQLITE_DONE" && $rc2=="SQLITE_OK") || |
drh | c1a7f94 | 2007-08-30 02:26:53 +0000 | [diff] [blame] | 432 | ($rc1=="SQLITE_ERROR" && $rc2=="SQLITE_ABORT") || |
drh | 1527ff4 | 2008-01-18 17:03:32 +0000 | [diff] [blame] | 433 | ($rc1=="SQLITE_ERROR" && $rc2=="SQLITE_NOMEM") || |
| 434 | ($rc1=="SQLITE_ERROR" && $rc2=="SQLITE_IOERR") |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 435 | } |
| 436 | } {1} |
| 437 | db2 close |
| 438 | } |
| 439 | do_test shared_malloc-8.X { |
| 440 | # Test that one or more queries were aborted due to the malloc() failure. |
| 441 | expr $::aborted>=1 |
| 442 | } {1} |
| 443 | |
danielk1977 | 131c8bc | 2008-05-22 13:56:17 +0000 | [diff] [blame] | 444 | |
danielk1977 | 992772c | 2007-08-30 10:07:38 +0000 | [diff] [blame] | 445 | # This test is designed to catch a specific bug that was present during |
| 446 | # development of 3.5.0. If a malloc() failed while setting the page-size, |
| 447 | # a buffer (Pager.pTmpSpace) was being freed. This could cause a seg-fault |
| 448 | # later if another connection tried to use the pager. |
| 449 | # |
| 450 | # This test will crash 3.4.2. |
| 451 | # |
| 452 | do_malloc_test shared_err-9 -tclprep { |
| 453 | sqlite3 db2 test.db |
| 454 | } -sqlbody { |
| 455 | PRAGMA page_size = 4096; |
| 456 | PRAGMA page_size = 1024; |
| 457 | } -cleanup { |
| 458 | db2 eval { |
| 459 | CREATE TABLE abc(a, b, c); |
| 460 | BEGIN; |
| 461 | INSERT INTO abc VALUES(1, 2, 3); |
| 462 | ROLLBACK; |
| 463 | } |
| 464 | db2 close |
| 465 | } |
| 466 | |
danielk1977 | 131c8bc | 2008-05-22 13:56:17 +0000 | [diff] [blame] | 467 | catch {db close} |
| 468 | catch {db2 close} |
| 469 | do_malloc_test shared_err-10 -tclprep { |
| 470 | sqlite3 db test.db |
| 471 | sqlite3 db2 test.db |
| 472 | |
| 473 | db eval { SELECT * FROM sqlite_master } |
| 474 | db2 eval { |
| 475 | BEGIN; |
| 476 | CREATE TABLE abc(a, b, c); |
| 477 | } |
| 478 | } -tclbody { |
| 479 | catch {db eval {SELECT * FROM sqlite_master}} |
| 480 | error 1 |
| 481 | } -cleanup { |
| 482 | execsql { SELECT * FROM sqlite_master } |
| 483 | } |
| 484 | |
| 485 | do_malloc_test shared_err-11 -tclprep { |
| 486 | sqlite3 db test.db |
| 487 | sqlite3 db2 test.db |
| 488 | |
| 489 | db eval { SELECT * FROM sqlite_master } |
| 490 | db2 eval { |
| 491 | BEGIN; |
| 492 | CREATE TABLE abc(a, b, c); |
| 493 | } |
| 494 | } -tclbody { |
| 495 | catch {db eval {SELECT * FROM sqlite_master}} |
| 496 | catch {sqlite3_errmsg16 db} |
| 497 | error 1 |
| 498 | } -cleanup { |
| 499 | execsql { SELECT * FROM sqlite_master } |
| 500 | } |
| 501 | |
danielk1977 | 7eaabcd | 2008-07-07 14:56:56 +0000 | [diff] [blame] | 502 | do_malloc_test shared_err-12 -sqlbody { |
| 503 | CREATE TABLE abc(a, b, c); |
| 504 | INSERT INTO abc VALUES(1, 2, 3); |
| 505 | } |
danielk1977 | 992772c | 2007-08-30 10:07:38 +0000 | [diff] [blame] | 506 | |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 507 | catch {db close} |
danielk1977 | 992772c | 2007-08-30 10:07:38 +0000 | [diff] [blame] | 508 | catch {db2 close} |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 509 | sqlite3_enable_shared_cache $::enable_shared_cache |
| 510 | finish_test |