1 # 2010 October 29 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 13 set testdir [file dirname $argv0] 14 source $testdir/tester.tcl 15 source $testdir/malloc_common.tcl 16 17 set g_chunk_size [ expr ($::SQLITE_MAX_PAGE_SIZE*16384) ] 18 set g_max_chunks 32 19 20 # This handles appending the chunk number 21 # to the end of the filename. if 22 # SQLITE_MULTIPLEX_EXT_OVWR is defined, then 23 # it overwrites the last 2 bytes of the 24 # file name with the chunk number. 25 proc multiplex_name {name chunk} { 26 if {$chunk==0} { return $name } 27 set num [format "%02d" $chunk] 28 ifcapable {multiplex_ext_overwrite} { 29 set name [string range $name 0 [expr [string length $name]-2-1]] 30 } 31 return $name$num 32 } 33 34 # This saves off the parameters and calls the 35 # underlying sqlite3_multiplex_control() API. 36 proc multiplex_set {db name chunk_size max_chunks} { 37 global g_chunk_size 38 global g_max_chunks 39 set g_chunk_size [ expr (($chunk_size+($::SQLITE_MAX_PAGE_SIZE-1)) & ~($::SQLITE_MAX_PAGE_SIZE-1)) ] 40 set g_max_chunks $max_chunks 41 set rc [catch {sqlite3_multiplex_control $db $name chunk_size $chunk_size} msg] 42 if { $rc==0 } { 43 set rc [catch {sqlite3_multiplex_control $db $name max_chunks $max_chunks} msg] 44 } 45 list $msg 46 } 47 48 # This attempts to delete the base file and 49 # and files with the chunk extension. 50 proc multiplex_delete {name} { 51 global g_max_chunks 52 for {set i 0} {$i<$g_max_chunks} {incr i} { 53 forcedelete [multiplex_name $name $i] 54 forcedelete [multiplex_name $name-journal $i] 55 forcedelete [multiplex_name $name-wal $i] 56 } 57 } 58 59 db close 60 61 multiplex_delete test.db 62 multiplex_delete test2.db 63 64 #------------------------------------------------------------------------- 65 # multiplex-1.1.*: Test initialize and shutdown. 66 67 do_test multiplex-1.1 { sqlite3_multiplex_initialize nosuchvfs 1 } {SQLITE_ERROR} 68 do_test multiplex-1.2 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK} 69 do_test multiplex-1.3 { sqlite3_multiplex_initialize "" 1 } {SQLITE_MISUSE} 70 do_test multiplex-1.4 { sqlite3_multiplex_shutdown } {SQLITE_OK} 71 72 do_test multiplex-1.5 { sqlite3_multiplex_initialize "" 0 } {SQLITE_OK} 73 do_test multiplex-1.6 { sqlite3_multiplex_shutdown } {SQLITE_OK} 74 do_test multiplex-1.7 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK} 75 do_test multiplex-1.8 { sqlite3_multiplex_shutdown } {SQLITE_OK} 76 77 78 do_test multiplex-1.9.1 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK} 79 do_test multiplex-1.9.2 { sqlite3 db test.db } {} 80 do_test multiplex-1.9.3 { multiplex_set db main 32768 16 } {SQLITE_OK} 81 do_test multiplex-1.9.4 { multiplex_set db main 32768 -1 } {SQLITE_MISUSE} 82 do_test multiplex-1.9.5 { multiplex_set db main -1 16 } {SQLITE_MISUSE} 83 do_test multiplex-1.9.6 { multiplex_set db main 31 16 } {SQLITE_OK} 84 do_test multiplex-1.9.7 { multiplex_set db main 32768 100 } {SQLITE_MISUSE} 85 do_test multiplex-1.9.8 { multiplex_set db main 1073741824 1 } {SQLITE_OK} 86 do_test multiplex-1.9.9 { db close } {} 87 do_test multiplex-1.9.10 { sqlite3_multiplex_shutdown } {SQLITE_OK} 88 89 do_test multiplex-1.10.1 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK} 90 do_test multiplex-1.10.2 { sqlite3 db test.db } {} 91 do_test multiplex-1.10.3 { lindex [ catchsql { SELECT multiplex_control(2, 32768); } ] 0 } {0} 92 do_test multiplex-1.10.4 { lindex [ catchsql { SELECT multiplex_control(3, -1); } ] 0 } {1} 93 do_test multiplex-1.10.5 { lindex [ catchsql { SELECT multiplex_control(2, -1); } ] 0 } {1} 94 do_test multiplex-1.10.6 { lindex [ catchsql { SELECT multiplex_control(2, 31); } ] 0 } {0} 95 do_test multiplex-1.10.7 { lindex [ catchsql { SELECT multiplex_control(3, 100); } ] 0 } {1} 96 do_test multiplex-1.10.8 { lindex [ catchsql { SELECT multiplex_control(2, 1073741824); } ] 0 } {0} 97 do_test multiplex-1.10.9 { db close } {} 98 do_test multiplex-1.10.10 { sqlite3_multiplex_shutdown } {SQLITE_OK} 99 100 do_test multiplex-1.11.1 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK} 101 do_test multiplex-1.11.2 { sqlite3 db test.db } {} 102 do_test multiplex-1.11.3 { sqlite3_multiplex_control db main enable 0 } {SQLITE_OK} 103 do_test multiplex-1.11.4 { sqlite3_multiplex_control db main enable 1 } {SQLITE_OK} 104 do_test multiplex-1.11.5 { sqlite3_multiplex_control db main enable -1 } {SQLITE_OK} 105 do_test multiplex-1.11.6 { db close } {} 106 do_test multiplex-1.11.7 { sqlite3_multiplex_shutdown } {SQLITE_OK} 107 108 do_test multiplex-1.12.1 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK} 109 do_test multiplex-1.12.2 { sqlite3 db test.db } {} 110 do_test multiplex-1.12.3 { lindex [ catchsql { SELECT multiplex_control(1, 0); } ] 0 } {0} 111 do_test multiplex-1.12.4 { lindex [ catchsql { SELECT multiplex_control(1, 1); } ] 0 } {0} 112 do_test multiplex-1.12.5 { lindex [ catchsql { SELECT multiplex_control(1, -1); } ] 0 } {0} 113 do_test multiplex-1.12.6 { db close } {} 114 do_test multiplex-1.12.7 { sqlite3_multiplex_shutdown } {SQLITE_OK} 115 116 do_test multiplex-1.13.1 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK} 117 do_test multiplex-1.13.2 { sqlite3 db test.db } {} 118 do_test multiplex-1.13.3 { lindex [ catchsql { SELECT multiplex_control(-1, 0); } ] 0 } {1} 119 do_test multiplex-1.13.4 { lindex [ catchsql { SELECT multiplex_control(4, 1); } ] 0 } {1} 120 do_test multiplex-1.13.6 { db close } {} 121 do_test multiplex-1.13.7 { sqlite3_multiplex_shutdown } {SQLITE_OK} 122 123 #------------------------------------------------------------------------- 124 # Some simple warm-body tests with a single database file in rollback 125 # mode: 126 # 127 # multiplex-2.1.*: Test simple writing to a multiplex file. 128 # 129 # multiplex-2.2.*: More writing. 130 # 131 # multiplex-2.3.*: Open and close a second db. 132 # 133 # multiplex-2.4.*: Try to shutdown the multiplex system before closing the db 134 # file. Check that this fails and the multiplex system still works 135 # afterwards. Then close the database and successfully shut 136 # down the multiplex system. 137 # 138 # multiplex-2.5.*: More reading/writing. 139 # 140 # multiplex-2.6.*: More reading/writing with varying small chunk sizes, as 141 # well as varying journal mode. 142 # 143 # multiplex-2.7.*: Disable/enable tests. 144 # 145 146 sqlite3_multiplex_initialize "" 1 147 multiplex_set db main 32768 16 148 149 do_test multiplex-2.1.2 { 150 sqlite3 db test.db 151 execsql { 152 PRAGMA page_size=1024; 153 PRAGMA auto_vacuum=OFF; 154 PRAGMA journal_mode=DELETE; 155 } 156 execsql { 157 CREATE TABLE t1(a, b); 158 INSERT INTO t1 VALUES(1, randomblob(1100)); 159 INSERT INTO t1 VALUES(2, randomblob(1100)); 160 } 161 } {} 162 do_test multiplex-2.1.3 { file size [multiplex_name test.db 0] } {4096} 163 do_test multiplex-2.1.4 { 164 execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) } 165 } {} 166 167 do_test multiplex-2.2.1 { 168 execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) } 169 } {} 170 do_test multiplex-2.2.3 { file size [multiplex_name test.db 0] } {6144} 171 172 do_test multiplex-2.3.1 { 173 sqlite3 db2 test2.db 174 db2 close 175 } {} 176 177 178 do_test multiplex-2.4.1 { 179 sqlite3_multiplex_shutdown 180 } {SQLITE_MISUSE} 181 do_test multiplex-2.4.2 { 182 execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) } 183 } {} 184 do_test multiplex-2.4.4 { file size [multiplex_name test.db 0] } {7168} 185 do_test multiplex-2.4.99 { 186 db close 187 sqlite3_multiplex_shutdown 188 } {SQLITE_OK} 189 190 191 do_test multiplex-2.5.1 { 192 multiplex_delete test.db 193 sqlite3_multiplex_initialize "" 1 194 sqlite3 db test.db 195 multiplex_set db main 4096 16 196 } {SQLITE_OK} 197 198 do_test multiplex-2.5.2 { 199 execsql { 200 PRAGMA page_size = 1024; 201 PRAGMA journal_mode = delete; 202 PRAGMA auto_vacuum = off; 203 CREATE TABLE t1(a PRIMARY KEY, b); 204 } 205 } {delete} 206 207 do_test multiplex-2.5.3 { 208 execsql { 209 INSERT INTO t1 VALUES(1, 'one'); 210 INSERT INTO t1 VALUES(2, randomblob(4000)); 211 INSERT INTO t1 VALUES(3, 'three'); 212 INSERT INTO t1 VALUES(4, randomblob(4000)); 213 INSERT INTO t1 VALUES(5, 'five'); 214 INSERT INTO t1 VALUES(6, randomblob($g_chunk_size)); 215 INSERT INTO t1 VALUES(7, randomblob($g_chunk_size)); 216 } 217 } {} 218 219 do_test multiplex-2.5.4 { 220 db eval {SELECT * FROM t1 WHERE a=1} 221 } {1 one} 222 223 do_test multiplex-2.5.5 { 224 db eval {SELECT * FROM t1 WHERE a=3} 225 } {3 three} 226 227 do_test multiplex-2.5.6 { 228 db eval {SELECT * FROM t1 WHERE a=5} 229 } {5 five} 230 231 do_test multiplex-2.5.7 { 232 db eval {SELECT a,length(b) FROM t1 WHERE a=2} 233 } {2 4000} 234 235 do_test multiplex-2.5.8 { 236 db eval {SELECT a,length(b) FROM t1 WHERE a=4} 237 } {4 4000} 238 239 do_test multiplex-2.5.9 { file size [multiplex_name test.db 0] } [list $g_chunk_size] 240 do_test multiplex-2.5.10 { file size [multiplex_name test.db 1] } [list $g_chunk_size] 241 242 do_test multiplex-2.5.99 { 243 db close 244 sqlite3_multiplex_shutdown 245 } {SQLITE_OK} 246 247 248 set all_journal_modes {delete persist truncate memory off} 249 foreach jmode $all_journal_modes { 250 for {set sz 151} {$sz<8000} {set sz [expr $sz+419]} { 251 252 do_test multiplex-2.6.1.$sz.$jmode { 253 multiplex_delete test.db 254 sqlite3_multiplex_initialize "" 1 255 sqlite3 db test.db 256 multiplex_set db main $sz 32 257 } {SQLITE_OK} 258 259 do_test multiplex-2.6.2.$sz.$jmode { 260 db eval { 261 PRAGMA page_size = 1024; 262 PRAGMA auto_vacuum = off; 263 } 264 db eval "PRAGMA journal_mode = $jmode;" 265 } $jmode 266 267 do_test multiplex-2.6.3.$sz.$jmode { 268 execsql { 269 CREATE TABLE t1(a PRIMARY KEY, b); 270 INSERT INTO t1 VALUES(1, 'one'); 271 INSERT INTO t1 VALUES(2, randomblob($g_chunk_size)); 272 } 273 } {} 274 275 do_test multiplex-2.6.4.$sz.$jmode { 276 db eval {SELECT b FROM t1 WHERE a=1} 277 } {one} 278 279 do_test multiplex-2.6.5.$sz.$jmode { 280 db eval {SELECT length(b) FROM t1 WHERE a=2} 281 } [list $g_chunk_size] 282 283 do_test multiplex-2.6.6.$sz.$jmode { file size [multiplex_name test.db 0] } [list $g_chunk_size] 284 285 do_test multiplex-2.6.99.$sz.$jmode { 286 db close 287 sqlite3_multiplex_shutdown 288 } {SQLITE_OK} 289 290 } 291 } 292 293 do_test multiplex-2.7.1 { multiplex_delete test.db } {} 294 do_test multiplex-2.7.2 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK} 295 do_test multiplex-2.7.3 { sqlite3 db test.db } {} 296 do_test multiplex-2.7.4 { lindex [ catchsql { SELECT multiplex_control(2, 65536); } ] 0 } {0} 297 do_test multiplex-2.7.5 { lindex [ catchsql { SELECT multiplex_control(1, 0); } ] 0 } {0} 298 do_test multiplex-2.7.6 { 299 execsql { 300 CREATE TABLE t1(a PRIMARY KEY, b); 301 INSERT INTO t1 VALUES(1, randomblob(1000)); 302 } 303 } {} 304 # verify only one file, and file size is less than chunks size 305 do_test multiplex-2.7.7 { expr ([file size [multiplex_name test.db 0]] < 65536) } {1} 306 do_test multiplex-2.7.8 { file exists [multiplex_name test.db 1] } {0} 307 do_test multiplex-2.7.9 { 308 execsql { 309 INSERT INTO t1 VALUES(2, randomblob(65536)); 310 } 311 } {} 312 # verify only one file, and file size exceeds chunks size 313 do_test multiplex-2.7.10 { expr ([file size [multiplex_name test.db 0]] > 65536) } {1} 314 do_test multiplex-2.7.11 { file exists [multiplex_name test.db 1] } {0} 315 do_test multiplex-2.7.12 { db close } {} 316 do_test multiplex-2.7.13 { sqlite3_multiplex_shutdown } {SQLITE_OK} 317 318 #------------------------------------------------------------------------- 319 # Try some tests with more than one connection to a database file. Still 320 # in rollback mode. 321 # 322 # multiplex-3.1.*: Two connections to a single database file. 323 # 324 # multiplex-3.2.*: Two connections to each of several database files (that 325 # are in the same multiplex group). 326 # 327 do_test multiplex-3.1.1 { 328 multiplex_delete test.db 329 sqlite3_multiplex_initialize "" 1 330 sqlite3 db test.db 331 multiplex_set db main 32768 16 332 } {SQLITE_OK} 333 do_test multiplex-3.1.2 { 334 execsql { 335 PRAGMA page_size = 1024; 336 PRAGMA journal_mode = delete; 337 PRAGMA auto_vacuum = off; 338 CREATE TABLE t1(a PRIMARY KEY, b); 339 INSERT INTO t1 VALUES(1, 'one'); 340 } 341 file size [multiplex_name test.db 0] 342 } {3072} 343 do_test multiplex-3.1.3 { 344 sqlite3 db2 test.db 345 execsql { CREATE TABLE t2(a, b) } db2 346 } {} 347 do_test multiplex-3.1.4 { 348 execsql { CREATE TABLE t3(a, b) } 349 } {} 350 do_test multiplex-3.1.5 { 351 catchsql { CREATE TABLE t3(a, b) } 352 } {1 {table t3 already exists}} 353 do_test multiplex-3.1.6 { 354 db close 355 db2 close 356 } {} 357 358 do_test multiplex-3.2.1a { 359 360 multiplex_delete test.db 361 multiplex_delete test2.db 362 363 sqlite3 db1a test.db 364 sqlite3 db2a test2.db 365 366 foreach db {db1a db2a} { 367 execsql { 368 PRAGMA page_size = 1024; 369 PRAGMA journal_mode = delete; 370 PRAGMA auto_vacuum = off; 371 CREATE TABLE t1(a, b); 372 } $db 373 } 374 375 list [file size [multiplex_name test.db 0]] [file size [multiplex_name test2.db 0]] 376 } {2048 2048} 377 378 do_test multiplex-3.2.1b { 379 sqlite3 db1b test.db 380 sqlite3 db2b test2.db 381 } {} 382 383 do_test multiplex-3.2.2 { execsql { INSERT INTO t1 VALUES('x', 'y') } db1a } {} 384 do_test multiplex-3.2.3 { execsql { INSERT INTO t1 VALUES('v', 'w') } db1b } {} 385 do_test multiplex-3.2.4 { execsql { INSERT INTO t1 VALUES('t', 'u') } db2a } {} 386 do_test multiplex-3.2.5 { execsql { INSERT INTO t1 VALUES('r', 's') } db2b } {} 387 388 do_test multiplex-3.2.6 { 389 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1a 390 } {} 391 do_test multiplex-3.2.7 { 392 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1b 393 } {} 394 do_test multiplex-3.2.8 { 395 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2a 396 } {} 397 do_test multiplex-3.2.9 { 398 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2b 399 } {} 400 401 do_test multiplex-3.3.1 { 402 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1a 403 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1b 404 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2a 405 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2b 406 } {} 407 408 do_test multiplex-3.2.X { 409 foreach db {db1a db2a db2b db1b} { catch { $db close } } 410 } {} 411 412 #------------------------------------------------------------------------- 413 # 414 415 sqlite3_multiplex_initialize "" 1 416 multiplex_set db main 32768 16 417 418 # Return a list of all currently defined multiplexs. 419 proc multiplex_list {} { 420 set allq {} 421 foreach q [sqlite3_multiplex_dump] { 422 lappend allq [lindex $q 0] 423 } 424 return [lsort $allq] 425 } 426 427 do_test multiplex-4.1.6 { 428 multiplex_delete test2.db 429 sqlite3 db test2.db 430 db eval {CREATE TABLE t2(x); INSERT INTO t2 VALUES('tab-t2');} 431 set res [multiplex_list] 432 list [regexp {test2.db} $res] 433 } {1} 434 do_test multiplex-4.1.6a { 435 sqlite3 db2 test2.db 436 db2 eval {SELECT * FROM t2} 437 } {tab-t2} 438 do_test multiplex-4.1.7 { 439 execsql {INSERT INTO t2 VALUES(zeroblob(200000))} 440 } {} 441 do_test multiplex-4.1.8 { 442 sqlite3 db2 test2.db 443 db2 eval {SELECT count(*) FROM t2} 444 } {2} 445 do_test multiplex-4.1.8a { 446 db2 eval { DELETE FROM t2 WHERE x = 'tab-t2' } 447 } {} 448 do_test multiplex-4.1.8b { 449 sqlite3 db2 test2.db 450 db2 eval {SELECT count(*) FROM t2} 451 } {1} 452 453 454 do_test multiplex-4.1.9 { 455 execsql {INSERT INTO t2 VALUES(zeroblob(200000))} 456 } {} 457 do_test multiplex-4.1.10 { 458 set res [multiplex_list] 459 list [regexp {test2.db} $res] 460 } {1} 461 do_test multiplex-4.1.11 { 462 db2 close 463 set res [multiplex_list] 464 list [regexp {test2.db} $res] 465 } {1} 466 do_test multiplex-4.1.12 { 467 db close 468 multiplex_list 469 } {} 470 471 472 #------------------------------------------------------------------------- 473 # The following tests test that the multiplex VFS handles malloc and IO 474 # errors. 475 # 476 477 sqlite3_multiplex_initialize "" 1 478 multiplex_set db main 32768 16 479 480 do_faultsim_test multiplex-5.1 -prep { 481 catch {db close} 482 } -body { 483 sqlite3 db test2.db 484 } 485 do_faultsim_test multiplex-5.2 -prep { 486 catch {db close} 487 } -body { 488 sqlite3 db test.db 489 } 490 491 catch { db close } 492 multiplex_delete test.db 493 multiplex_delete test2.db 494 495 do_test multiplex-5.3.prep { 496 sqlite3 db test.db 497 execsql { 498 PRAGMA auto_vacuum = 1; 499 PRAGMA page_size = 1024; 500 CREATE TABLE t1(a, b); 501 INSERT INTO t1 VALUES(10, zeroblob(1200)); 502 } 503 faultsim_save_and_close 504 } {} 505 do_faultsim_test multiplex-5.3 -prep { 506 faultsim_restore_and_reopen 507 } -body { 508 execsql { DELETE FROM t1 } 509 } 510 511 do_test multiplex-5.4.1 { 512 catch { db close } 513 multiplex_delete test.db 514 file mkdir test.db 515 list [catch { sqlite3 db test.db } msg] $msg 516 } {1 {unable to open database file}} 517 catch { file delete test.db } 518 519 do_faultsim_test multiplex-5.5 -prep { 520 catch { sqlite3_multiplex_shutdown } 521 } -body { 522 sqlite3_multiplex_initialize "" 1 523 multiplex_set db main 32768 16 524 } 525 526 # test that mismatch filesize is detected 527 # 528 # Do not run this test if $::G(perm:presql) is set. If it is set, then the 529 # expected IO error will occur within the Tcl [sqlite3] wrapper, not within 530 # the first SQL statement executed below. This breaks the test case. 531 # 532 if {0==[info exists ::G(perm:presql)] || $::G(perm:presql) == ""} { 533 set all_journal_modes {delete persist truncate memory off} 534 foreach jmode $all_journal_modes { 535 do_test multiplex-5.6.1.$jmode { 536 sqlite3_multiplex_shutdown 537 multiplex_delete test.db 538 sqlite3 db test.db 539 db eval { 540 PRAGMA page_size = 1024; 541 PRAGMA auto_vacuum = off; 542 } 543 db eval "PRAGMA journal_mode = $jmode;" 544 } $jmode 545 do_test multiplex-5.6.2.$jmode { 546 execsql { 547 CREATE TABLE t1(a, b); 548 INSERT INTO t1 VALUES(1, randomblob(15000)); 549 INSERT INTO t1 VALUES(2, randomblob(15000)); 550 INSERT INTO t1 VALUES(3, randomblob(15000)); 551 INSERT INTO t1 VALUES(4, randomblob(15000)); 552 INSERT INTO t1 VALUES(5, randomblob(15000)); 553 } 554 db close 555 sqlite3_multiplex_initialize "" 1 556 sqlite3 db test.db 557 multiplex_set db main 4096 16 558 } {SQLITE_OK} 559 do_test multiplex-5.6.3.$jmode { 560 catchsql { 561 INSERT INTO t1 VALUES(6, randomblob(15000)); 562 } 563 } {1 {disk I/O error}} 564 do_test multiplex-5.6.4.$jmode { 565 db close 566 } {} 567 } 568 } 569 570 catch { sqlite3_multiplex_shutdown } 571 finish_test 572