3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
15 source $testdir/malloc_common.tcl
17 # The tests in this file assume that SQLite is compiled without
21 puts -nonewline "SQLite compiled with SQLITE_ENABLE_8_3_NAMES. "
22 puts "Skipping tests multiplex-*."
27 set g_chunk_size [ expr ($::SQLITE_MAX_PAGE_SIZE*16384) ]
30 # This handles appending the chunk number
31 # to the end of the filename. if
32 # SQLITE_MULTIPLEX_EXT_OVWR is defined, then
33 # it overwrites the last 2 bytes of the
34 # file name with the chunk number.
35 proc multiplex_name {name chunk} {
36 if {$chunk==0} { return $name }
37 set num [format "%03d" $chunk]
38 ifcapable {multiplex_ext_overwrite} {
39 set name [string range $name 0 [expr [string length $name]-2-1]]
44 # This saves off the parameters and calls the
45 # underlying sqlite3_multiplex_control() API.
46 proc multiplex_set {db name chunk_size max_chunks} {
49 set g_chunk_size [ expr (($chunk_size+($::SQLITE_MAX_PAGE_SIZE-1)) & ~($::SQLITE_MAX_PAGE_SIZE-1)) ]
50 set g_max_chunks $max_chunks
51 set rc [catch {sqlite3_multiplex_control $db $name chunk_size $chunk_size} msg]
53 set rc [catch {sqlite3_multiplex_control $db $name max_chunks $max_chunks} msg]
58 # This attempts to delete the base file and
59 # and files with the chunk extension.
60 proc multiplex_delete {name} {
63 for {set i 0} {$i<$g_max_chunks} {incr i} {
64 forcedelete [multiplex_name $name $i]
65 forcedelete [multiplex_name $name-journal $i]
66 forcedelete [multiplex_name $name-wal $i]
73 proc xLog {error_code msg} {
74 lappend ::log $error_code $msg
78 multiplex_delete test.db
79 multiplex_delete test2.db
81 #-------------------------------------------------------------------------
82 # multiplex-1.1.*: Test initialize and shutdown.
84 do_test multiplex-1.1 { sqlite3_multiplex_initialize nosuchvfs 1 } {SQLITE_ERROR}
85 do_test multiplex-1.2 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
86 do_test multiplex-1.3 { sqlite3_multiplex_initialize "" 1 } {SQLITE_MISUSE}
87 do_test multiplex-1.4 { sqlite3_multiplex_shutdown } {SQLITE_OK}
89 do_test multiplex-1.5 { sqlite3_multiplex_initialize "" 0 } {SQLITE_OK}
90 do_test multiplex-1.6 { sqlite3_multiplex_shutdown } {SQLITE_OK}
91 do_test multiplex-1.7 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
92 do_test multiplex-1.8 { sqlite3_multiplex_shutdown } {SQLITE_OK}
95 do_test multiplex-1.9.1 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
96 do_test multiplex-1.9.2 { sqlite3 db test.db } {}
97 do_test multiplex-1.9.3 { multiplex_set db main 32768 16 } {SQLITE_OK}
98 do_test multiplex-1.9.4 { multiplex_set db main 32768 -1 } {SQLITE_OK}
99 do_test multiplex-1.9.6 { multiplex_set db main 31 16 } {SQLITE_OK}
100 do_test multiplex-1.9.7 { multiplex_set db main 32768 100 } {SQLITE_OK}
101 do_test multiplex-1.9.8 { multiplex_set db main 1073741824 1 } {SQLITE_OK}
102 do_test multiplex-1.9.9 { db close } {}
103 do_test multiplex-1.9.10 { sqlite3_multiplex_shutdown } {SQLITE_OK}
105 do_test multiplex-1.10.1 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
106 do_test multiplex-1.10.2 { sqlite3 db test.db } {}
107 do_test multiplex-1.10.3 { lindex [ catchsql { SELECT multiplex_control(2, 32768); } ] 0 } {0}
108 do_test multiplex-1.10.4 { lindex [ catchsql { SELECT multiplex_control(3, -1); } ] 0 } {0}
109 do_test multiplex-1.10.6 { lindex [ catchsql { SELECT multiplex_control(2, 31); } ] 0 } {0}
110 do_test multiplex-1.10.7 { lindex [ catchsql { SELECT multiplex_control(3, 100); } ] 0 } {0}
111 do_test multiplex-1.10.8 { lindex [ catchsql { SELECT multiplex_control(2, 1073741824); } ] 0 } {0}
112 do_test multiplex-1.10.9 { db close } {}
113 do_test multiplex-1.10.10 { sqlite3_multiplex_shutdown } {SQLITE_OK}
115 do_test multiplex-1.11.1 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
116 do_test multiplex-1.11.2 { sqlite3 db test.db } {}
117 do_test multiplex-1.11.3 { sqlite3_multiplex_control db main enable 0 } {SQLITE_OK}
118 do_test multiplex-1.11.4 { sqlite3_multiplex_control db main enable 1 } {SQLITE_OK}
119 do_test multiplex-1.11.5 { sqlite3_multiplex_control db main enable -1 } {SQLITE_OK}
120 do_test multiplex-1.11.6 { db close } {}
121 do_test multiplex-1.11.7 { sqlite3_multiplex_shutdown } {SQLITE_OK}
123 do_test multiplex-1.12.1 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
124 do_test multiplex-1.12.2 { sqlite3 db test.db } {}
125 do_test multiplex-1.12.3 { lindex [ catchsql { SELECT multiplex_control(1, 0); } ] 0 } {0}
126 do_test multiplex-1.12.4 { lindex [ catchsql { SELECT multiplex_control(1, 1); } ] 0 } {0}
127 do_test multiplex-1.12.5 { lindex [ catchsql { SELECT multiplex_control(1, -1); } ] 0 } {0}
128 do_test multiplex-1.12.6 { db close } {}
129 do_test multiplex-1.12.7 { sqlite3_multiplex_shutdown } {SQLITE_OK}
131 do_test multiplex-1.13.1 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
132 do_test multiplex-1.13.2 { sqlite3 db test.db } {}
133 do_test multiplex-1.13.3 { lindex [ catchsql { SELECT multiplex_control(-1, 0); } ] 0 } {1}
134 do_test multiplex-1.13.4 { lindex [ catchsql { SELECT multiplex_control(4, 1); } ] 0 } {1}
135 do_test multiplex-1.13.6 { db close } {}
136 do_test multiplex-1.13.7 { sqlite3_multiplex_shutdown } {SQLITE_OK}
138 #-------------------------------------------------------------------------
139 # Some simple warm-body tests with a single database file in rollback
142 # multiplex-2.1.*: Test simple writing to a multiplex file.
144 # multiplex-2.2.*: More writing.
146 # multiplex-2.3.*: Open and close a second db.
148 # multiplex-2.4.*: Try to shutdown the multiplex system before closing the db
149 # file. Check that this fails and the multiplex system still works
150 # afterwards. Then close the database and successfully shut
151 # down the multiplex system.
153 # multiplex-2.5.*: More reading/writing.
155 # multiplex-2.6.*: More reading/writing with varying small chunk sizes, as
156 # well as varying journal mode.
158 # multiplex-2.7.*: Disable/enable tests.
161 sqlite3_multiplex_initialize "" 1
162 multiplex_set db main 32768 16
165 foreach f [glob -nocomplain {test.x*[0-9][0-9][0-9]}] {
168 do_test multiplex-2.1.2 {
171 PRAGMA page_size=1024;
172 PRAGMA auto_vacuum=OFF;
173 PRAGMA journal_mode=DELETE;
176 CREATE TABLE t1(a, b);
177 INSERT INTO t1 VALUES(1, randomblob(1100));
178 INSERT INTO t1 VALUES(2, randomblob(1100));
181 do_test multiplex-2.1.3 { file size [multiplex_name test.x 0] } {4096}
182 do_test multiplex-2.1.4 {
183 execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
186 do_test multiplex-2.2.1 {
187 execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
189 do_test multiplex-2.2.3 { file size [multiplex_name test.x 0] } {6144}
191 do_test multiplex-2.3.1 {
197 unset -nocomplain ::log
198 do_test multiplex-2.4.1 {
199 sqlite3_multiplex_shutdown
201 do_test multiplex-2.4.2 {
202 execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
204 do_test multiplex-2.4.3 {
206 } {SQLITE_MISUSE {sqlite3_multiplex_shutdown() called while database connections are still open}}
207 do_test multiplex-2.4.4 { file size [multiplex_name test.x 0] } {7168}
208 do_test multiplex-2.4.5 {
215 do_test multiplex-2.4.99 {
216 sqlite3_multiplex_shutdown
219 do_test multiplex-2.5.1 {
220 multiplex_delete test.x
221 sqlite3_multiplex_initialize "" 1
223 multiplex_set db main 4096 16
226 do_test multiplex-2.5.2 {
228 PRAGMA page_size = 1024;
229 PRAGMA journal_mode = delete;
230 PRAGMA auto_vacuum = off;
231 CREATE TABLE t1(a PRIMARY KEY, b);
235 do_test multiplex-2.5.3 {
237 INSERT INTO t1 VALUES(1, 'one');
238 INSERT INTO t1 VALUES(2, randomblob(4000));
239 INSERT INTO t1 VALUES(3, 'three');
240 INSERT INTO t1 VALUES(4, randomblob(4000));
241 INSERT INTO t1 VALUES(5, 'five');
242 INSERT INTO t1 VALUES(6, randomblob($g_chunk_size));
243 INSERT INTO t1 VALUES(7, randomblob($g_chunk_size));
247 do_test multiplex-2.5.4 {
248 db eval {SELECT * FROM t1 WHERE a=1}
251 do_test multiplex-2.5.5 {
252 db eval {SELECT * FROM t1 WHERE a=3}
255 do_test multiplex-2.5.6 {
256 db eval {SELECT * FROM t1 WHERE a=5}
259 do_test multiplex-2.5.7 {
260 db eval {SELECT a,length(b) FROM t1 WHERE a=2}
263 do_test multiplex-2.5.8 {
264 db eval {SELECT a,length(b) FROM t1 WHERE a=4}
267 do_test multiplex-2.5.9 { file size [multiplex_name test.x 0] } [list $g_chunk_size]
268 do_test multiplex-2.5.10 { file size [multiplex_name test.x 1] } [list $g_chunk_size]
270 do_test multiplex-2.5.99 {
272 sqlite3_multiplex_shutdown
276 set all_journal_modes {delete persist truncate memory off}
277 foreach jmode $all_journal_modes {
278 for {set sz 151} {$sz<8000} {set sz [expr $sz+419]} {
280 do_test multiplex-2.6.1.$sz.$jmode {
281 multiplex_delete test.db
282 sqlite3_multiplex_initialize "" 1
284 multiplex_set db main $sz 32
287 do_test multiplex-2.6.2.$sz.$jmode {
289 PRAGMA page_size = 1024;
290 PRAGMA auto_vacuum = off;
292 db eval "PRAGMA journal_mode = $jmode;"
295 do_test multiplex-2.6.3.$sz.$jmode {
297 CREATE TABLE t1(a PRIMARY KEY, b);
298 INSERT INTO t1 VALUES(1, 'one');
299 INSERT INTO t1 VALUES(2, randomblob($g_chunk_size));
303 do_test multiplex-2.6.4.$sz.$jmode {
304 db eval {SELECT b FROM t1 WHERE a=1}
307 do_test multiplex-2.6.5.$sz.$jmode {
308 db eval {SELECT length(b) FROM t1 WHERE a=2}
309 } [list $g_chunk_size]
311 do_test multiplex-2.6.6.$sz.$jmode { file size [multiplex_name test.db 0] } [list $g_chunk_size]
313 do_test multiplex-2.6.99.$sz.$jmode {
315 sqlite3_multiplex_shutdown
321 do_test multiplex-2.7.1 { multiplex_delete test.db } {}
322 do_test multiplex-2.7.2 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
323 do_test multiplex-2.7.3 { sqlite3 db test.db } {}
324 do_test multiplex-2.7.4 { lindex [ catchsql { SELECT multiplex_control(2, 65536); } ] 0 } {0}
325 do_test multiplex-2.7.5 { lindex [ catchsql { SELECT multiplex_control(1, 0); } ] 0 } {0}
326 do_test multiplex-2.7.6 {
328 CREATE TABLE t1(a PRIMARY KEY, b);
329 INSERT INTO t1 VALUES(1, randomblob(1000));
332 # verify only one file, and file size is less than chunks size
333 do_test multiplex-2.7.7 { expr ([file size [multiplex_name test.db 0]] < 65536) } {1}
334 do_test multiplex-2.7.8 { file exists [multiplex_name test.db 1] } {0}
335 do_test multiplex-2.7.9 {
337 INSERT INTO t1 VALUES(2, randomblob(65536));
340 # verify only one file, and file size exceeds chunks size
341 do_test multiplex-2.7.10 { expr ([file size [multiplex_name test.db 0]] > 65536) } {1}
342 do_test multiplex-2.7.11 { file exists [multiplex_name test.db 1] } {0}
343 do_test multiplex-2.7.12 { db close } {}
344 do_test multiplex-2.7.13 { sqlite3_multiplex_shutdown } {SQLITE_OK}
346 #-------------------------------------------------------------------------
347 # Try some tests with more than one connection to a database file. Still
350 # multiplex-3.1.*: Two connections to a single database file.
352 # multiplex-3.2.*: Two connections to each of several database files (that
353 # are in the same multiplex group).
355 do_test multiplex-3.1.1 {
356 multiplex_delete test.db
357 sqlite3_multiplex_initialize "" 1
359 multiplex_set db main 32768 16
361 do_test multiplex-3.1.2 {
363 PRAGMA page_size = 1024;
364 PRAGMA journal_mode = delete;
365 PRAGMA auto_vacuum = off;
366 CREATE TABLE t1(a PRIMARY KEY, b);
367 INSERT INTO t1 VALUES(1, 'one');
369 file size [multiplex_name test.db 0]
371 do_test multiplex-3.1.3 {
373 execsql { CREATE TABLE t2(a, b) } db2
375 do_test multiplex-3.1.4 {
376 execsql { CREATE TABLE t3(a, b) }
378 do_test multiplex-3.1.5 {
379 catchsql { CREATE TABLE t3(a, b) }
380 } {1 {table t3 already exists}}
381 do_test multiplex-3.1.6 {
386 do_test multiplex-3.2.1a {
388 multiplex_delete test.db
389 multiplex_delete test2.db
392 sqlite3 db2a test2.db
394 foreach db {db1a db2a} {
396 PRAGMA page_size = 1024;
397 PRAGMA journal_mode = delete;
398 PRAGMA auto_vacuum = off;
399 CREATE TABLE t1(a, b);
403 list [file size [multiplex_name test.db 0]] [file size [multiplex_name test2.db 0]]
406 do_test multiplex-3.2.1b {
408 sqlite3 db2b test2.db
411 do_test multiplex-3.2.2 { execsql { INSERT INTO t1 VALUES('x', 'y') } db1a } {}
412 do_test multiplex-3.2.3 { execsql { INSERT INTO t1 VALUES('v', 'w') } db1b } {}
413 do_test multiplex-3.2.4 { execsql { INSERT INTO t1 VALUES('t', 'u') } db2a } {}
414 do_test multiplex-3.2.5 { execsql { INSERT INTO t1 VALUES('r', 's') } db2b } {}
416 do_test multiplex-3.2.6 {
417 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1a
419 do_test multiplex-3.2.7 {
420 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1b
422 do_test multiplex-3.2.8 {
423 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2a
425 do_test multiplex-3.2.9 {
426 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2b
429 do_test multiplex-3.3.1 {
430 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1a
431 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1b
432 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2a
433 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2b
436 do_test multiplex-3.2.X {
437 foreach db {db1a db2a db2b db1b} { catch { $db close } }
440 #-------------------------------------------------------------------------
443 sqlite3_multiplex_initialize "" 1
444 multiplex_set db main 32768 16
446 # Return a list of all currently defined multiplexs.
447 proc multiplex_list {} {
449 foreach q [sqlite3_multiplex_dump] {
450 lappend allq [lindex $q 0]
455 do_test multiplex-4.1.6 {
456 multiplex_delete test2.db
458 db eval {CREATE TABLE t2(x); INSERT INTO t2 VALUES('tab-t2');}
459 set res [multiplex_list]
460 list [regexp {test2.db} $res]
462 do_test multiplex-4.1.6a {
464 db2 eval {SELECT * FROM t2}
466 do_test multiplex-4.1.7 {
467 execsql {INSERT INTO t2 VALUES(zeroblob(200000))}
469 do_test multiplex-4.1.8 {
471 db2 eval {SELECT count(*) FROM t2}
473 do_test multiplex-4.1.8a {
474 db2 eval { DELETE FROM t2 WHERE x = 'tab-t2' }
476 do_test multiplex-4.1.8b {
478 db2 eval {SELECT count(*) FROM t2}
482 do_test multiplex-4.1.9 {
483 execsql {INSERT INTO t2 VALUES(zeroblob(200000))}
485 do_test multiplex-4.1.10 {
486 set res [multiplex_list]
487 list [regexp {test2.db} $res]
489 do_test multiplex-4.1.11 {
491 set res [multiplex_list]
492 list [regexp {test2.db} $res]
494 do_test multiplex-4.1.12 {
500 #-------------------------------------------------------------------------
501 # The following tests test that the multiplex VFS handles malloc and IO
505 sqlite3_multiplex_initialize "" 1
506 multiplex_set db main 32768 16
508 do_faultsim_test multiplex-5.1 -prep {
513 do_faultsim_test multiplex-5.2 -prep {
520 multiplex_delete test.db
521 multiplex_delete test2.db
523 do_test multiplex-5.3.prep {
526 PRAGMA auto_vacuum = 1;
527 PRAGMA page_size = 1024;
528 CREATE TABLE t1(a, b);
529 INSERT INTO t1 VALUES(10, zeroblob(1200));
531 faultsim_save_and_close
533 do_faultsim_test multiplex-5.3 -prep {
534 faultsim_restore_and_reopen
536 execsql { DELETE FROM t1 }
539 do_test multiplex-5.4.1 {
541 multiplex_delete test.db
543 list [catch { sqlite3 db test.db } msg] $msg
544 } {1 {unable to open database file}}
545 catch { delete_file test.db }
547 do_faultsim_test multiplex-5.5 -prep {
548 catch { sqlite3_multiplex_shutdown }
550 sqlite3_multiplex_initialize "" 1
551 multiplex_set db main 32768 16
554 #-------------------------------------------------------------------------
555 # Test that you can vacuum a multiplex'ed DB.
559 sqlite3_multiplex_shutdown
560 do_test multiplex-6.0.0 {
561 multiplex_delete test.db
562 multiplex_delete test.x
563 sqlite3_multiplex_initialize "" 1
565 multiplex_set db main 4096 16
568 do_test multiplex-6.1.0 {
570 PRAGMA page_size=1024;
571 PRAGMA journal_mode=DELETE;
572 PRAGMA auto_vacuum=OFF;
575 CREATE TABLE t1(a, b);
576 INSERT INTO t1 VALUES(1, randomblob($g_chunk_size));
577 INSERT INTO t1 VALUES(2, randomblob($g_chunk_size));
580 do_test multiplex-6.2.1 { file size [multiplex_name test.x 0] } [list $g_chunk_size]
581 do_test multiplex-6.2.2 { file size [multiplex_name test.x 1] } [list $g_chunk_size]
583 do_test multiplex-6.3.0 {
587 do_test multiplex-6.99 {
589 multiplex_delete test.x
590 sqlite3_multiplex_shutdown
597 catch { sqlite3_multiplex_shutdown }