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)) }
185 do_execsql_test multiplex-2.1.5 {
186 PRAGMA multiplex_enabled;
187 PRAGMA multiplex_filecount;
188 PRAGMA multiplex_chunksize;
191 do_test multiplex-2.2.1 {
192 execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
194 do_test multiplex-2.2.3 { file size [multiplex_name test.x 0] } {6144}
196 do_test multiplex-2.3.1 {
202 unset -nocomplain ::log
203 #do_test multiplex-2.4.1 {
204 # sqlite3_multiplex_shutdown
206 do_test multiplex-2.4.2 {
207 execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
209 #do_test multiplex-2.4.3 {
211 #} {SQLITE_MISUSE {sqlite3_multiplex_shutdown() called while database connections are still open}}
213 do_test multiplex-2.4.4 { file size [multiplex_name test.x 0] } {7168}
214 do_test multiplex-2.4.5 {
221 do_test multiplex-2.4.99 {
222 sqlite3_multiplex_shutdown
225 do_test multiplex-2.5.1 {
226 multiplex_delete test.x
227 sqlite3_multiplex_initialize "" 1
229 multiplex_set db main 4096 16
232 do_test multiplex-2.5.2 {
234 PRAGMA page_size = 1024;
235 PRAGMA journal_mode = delete;
236 PRAGMA auto_vacuum = off;
237 CREATE TABLE t1(a PRIMARY KEY, b);
241 do_test multiplex-2.5.3 {
243 INSERT INTO t1 VALUES(1, 'one');
244 INSERT INTO t1 VALUES(2, randomblob(4000));
245 INSERT INTO t1 VALUES(3, 'three');
246 INSERT INTO t1 VALUES(4, randomblob(4000));
247 INSERT INTO t1 VALUES(5, 'five');
248 INSERT INTO t1 VALUES(6, randomblob($g_chunk_size));
249 INSERT INTO t1 VALUES(7, randomblob($g_chunk_size));
253 do_test multiplex-2.5.4 {
254 db eval {SELECT * FROM t1 WHERE a=1}
257 do_test multiplex-2.5.5 {
258 db eval {SELECT * FROM t1 WHERE a=3}
261 do_test multiplex-2.5.6 {
262 db eval {SELECT * FROM t1 WHERE a=5}
265 do_test multiplex-2.5.7 {
266 db eval {SELECT a,length(b) FROM t1 WHERE a=2}
269 do_test multiplex-2.5.8 {
270 db eval {SELECT a,length(b) FROM t1 WHERE a=4}
273 do_test multiplex-2.5.9 { file size [multiplex_name test.x 0] } [list $g_chunk_size]
274 do_test multiplex-2.5.10 { file size [multiplex_name test.x 1] } [list $g_chunk_size]
275 do_execsql_test multiplex-2.5.11 {
276 PRAGMA multiplex_enabled;
277 PRAGMA multiplex_filecount;
278 PRAGMA multiplex_chunksize;
281 do_execsql_test multiplex-2.5.12 {
282 PRAGMA multiplex_filecount;
283 PRAGMA multiplex_chunksize;
288 do_test multiplex-2.5.99 {
290 sqlite3_multiplex_shutdown
294 set all_journal_modes {delete persist truncate memory off}
295 foreach jmode $all_journal_modes {
296 for {set sz 151} {$sz<8000} {set sz [expr $sz+419]} {
298 do_test multiplex-2.6.1.$sz.$jmode {
299 multiplex_delete test.db
300 sqlite3_multiplex_initialize "" 1
302 multiplex_set db main $sz 32
305 do_test multiplex-2.6.2.$sz.$jmode {
307 PRAGMA page_size = 1024;
308 PRAGMA auto_vacuum = off;
310 db eval "PRAGMA journal_mode = $jmode;"
313 do_test multiplex-2.6.3.$sz.$jmode {
315 CREATE TABLE t1(a PRIMARY KEY, b);
316 INSERT INTO t1 VALUES(1, 'one');
317 INSERT INTO t1 VALUES(2, randomblob($g_chunk_size));
321 do_test multiplex-2.6.4.$sz.$jmode {
322 db eval {SELECT b FROM t1 WHERE a=1}
325 do_test multiplex-2.6.5.$sz.$jmode {
326 db eval {SELECT length(b) FROM t1 WHERE a=2}
327 } [list $g_chunk_size]
329 do_test multiplex-2.6.6.$sz.$jmode { file size [multiplex_name test.db 0] } [list $g_chunk_size]
331 do_test multiplex-2.6.99.$sz.$jmode {
333 sqlite3_multiplex_shutdown
339 do_test multiplex-2.7.1 { multiplex_delete test.db } {}
340 do_test multiplex-2.7.2 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
341 do_test multiplex-2.7.3 { sqlite3 db test.db } {}
342 do_test multiplex-2.7.4 { lindex [ catchsql { SELECT multiplex_control(2, 65536); } ] 0 } {0}
343 do_test multiplex-2.7.5 { lindex [ catchsql { SELECT multiplex_control(1, 0); } ] 0 } {0}
344 do_test multiplex-2.7.6 {
346 CREATE TABLE t1(a PRIMARY KEY, b);
347 INSERT INTO t1 VALUES(1, randomblob(1000));
350 # verify only one file, and file size is less than chunks size
351 do_test multiplex-2.7.7 { expr ([file size [multiplex_name test.db 0]] < 65536) } {1}
352 do_test multiplex-2.7.8 { file exists [multiplex_name test.db 1] } {0}
353 do_test multiplex-2.7.9 {
355 INSERT INTO t1 VALUES(2, randomblob(65536));
358 # verify only one file, and file size exceeds chunks size
359 do_test multiplex-2.7.10 { expr ([file size [multiplex_name test.db 0]] > 65536) } {1}
360 do_test multiplex-2.7.11 { file exists [multiplex_name test.db 1] } {0}
361 do_test multiplex-2.7.12 { db close } {}
362 do_test multiplex-2.7.13 { sqlite3_multiplex_shutdown } {SQLITE_OK}
364 #-------------------------------------------------------------------------
365 # Try some tests with more than one connection to a database file. Still
368 # multiplex-3.1.*: Two connections to a single database file.
370 # multiplex-3.2.*: Two connections to each of several database files (that
371 # are in the same multiplex group).
373 do_test multiplex-3.1.1 {
374 multiplex_delete test.db
375 sqlite3_multiplex_initialize "" 1
377 multiplex_set db main 32768 16
379 do_test multiplex-3.1.2 {
381 PRAGMA page_size = 1024;
382 PRAGMA journal_mode = delete;
383 PRAGMA auto_vacuum = off;
384 CREATE TABLE t1(a PRIMARY KEY, b);
385 INSERT INTO t1 VALUES(1, 'one');
387 file size [multiplex_name test.db 0]
389 do_test multiplex-3.1.3 {
391 execsql { CREATE TABLE t2(a, b) } db2
393 do_test multiplex-3.1.4 {
394 execsql { CREATE TABLE t3(a, b) }
396 do_test multiplex-3.1.5 {
397 catchsql { CREATE TABLE t3(a, b) }
398 } {1 {table t3 already exists}}
399 do_test multiplex-3.1.6 {
404 do_test multiplex-3.2.1a {
406 multiplex_delete test.db
407 multiplex_delete test2.db
410 sqlite3 db2a test2.db
412 foreach db {db1a db2a} {
414 PRAGMA page_size = 1024;
415 PRAGMA journal_mode = delete;
416 PRAGMA auto_vacuum = off;
417 CREATE TABLE t1(a, b);
421 list [file size [multiplex_name test.db 0]] [file size [multiplex_name test2.db 0]]
424 do_test multiplex-3.2.1b {
426 sqlite3 db2b test2.db
429 do_test multiplex-3.2.2 { execsql { INSERT INTO t1 VALUES('x', 'y') } db1a } {}
430 do_test multiplex-3.2.3 { execsql { INSERT INTO t1 VALUES('v', 'w') } db1b } {}
431 do_test multiplex-3.2.4 { execsql { INSERT INTO t1 VALUES('t', 'u') } db2a } {}
432 do_test multiplex-3.2.5 { execsql { INSERT INTO t1 VALUES('r', 's') } db2b } {}
434 do_test multiplex-3.2.6 {
435 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1a
437 do_test multiplex-3.2.7 {
438 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1b
440 do_test multiplex-3.2.8 {
441 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2a
443 do_test multiplex-3.2.9 {
444 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2b
447 do_test multiplex-3.3.1 {
448 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1a
449 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1b
450 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2a
451 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2b
454 do_test multiplex-3.2.X {
455 foreach db {db1a db2a db2b db1b} { catch { $db close } }
458 #-------------------------------------------------------------------------
461 sqlite3_multiplex_initialize "" 1
462 multiplex_set db main 32768 16
464 # Return a list of all currently defined multiplexs.
465 proc multiplex_list {} {
466 glob -nocomplain test2.db*
469 do_test multiplex-4.1.6 {
470 multiplex_delete test2.db
472 db eval {CREATE TABLE t2(x); INSERT INTO t2 VALUES('tab-t2');}
473 set res [multiplex_list]
474 list [regexp {test2.db} $res]
476 do_test multiplex-4.1.6a {
478 db2 eval {SELECT * FROM t2}
480 do_test multiplex-4.1.7 {
481 execsql {INSERT INTO t2 VALUES(zeroblob(200000))}
483 do_test multiplex-4.1.8 {
485 db2 eval {SELECT count(*) FROM t2}
487 do_test multiplex-4.1.8a {
488 db2 eval { DELETE FROM t2 WHERE x = 'tab-t2' }
490 do_test multiplex-4.1.8b {
492 db2 eval {SELECT count(*) FROM t2}
496 do_test multiplex-4.1.9 {
497 execsql {INSERT INTO t2 VALUES(zeroblob(200000))}
499 do_test multiplex-4.1.10 {
500 set res [multiplex_list]
501 list [regexp {test2.db} $res]
503 do_test multiplex-4.1.11 {
505 set res [multiplex_list]
506 list [regexp {test2.db} $res]
508 do_test multiplex-4.1.12 {
514 #-------------------------------------------------------------------------
515 # The following tests test that the multiplex VFS handles malloc and IO
519 sqlite3_multiplex_initialize "" 1
520 multiplex_set db main 32768 16
522 do_faultsim_test multiplex-5.1 -prep {
527 do_faultsim_test multiplex-5.2 -prep {
534 multiplex_delete test.db
535 multiplex_delete test2.db
537 do_test multiplex-5.3.prep {
540 PRAGMA auto_vacuum = 1;
541 PRAGMA page_size = 1024;
542 CREATE TABLE t1(a, b);
543 INSERT INTO t1 VALUES(10, zeroblob(1200));
545 faultsim_save_and_close
547 do_faultsim_test multiplex-5.3 -prep {
548 faultsim_restore_and_reopen
550 execsql { DELETE FROM t1 }
553 do_test multiplex-5.4.1 {
555 multiplex_delete test.db
557 list [catch { sqlite3 db test.db } msg] $msg
558 } {1 {unable to open database file}}
559 catch { delete_file test.db }
561 do_faultsim_test multiplex-5.5 -prep {
562 catch { sqlite3_multiplex_shutdown }
564 sqlite3_multiplex_initialize "" 1
565 multiplex_set db main 32768 16
568 #-------------------------------------------------------------------------
569 # Test that you can vacuum a multiplex'ed DB.
573 sqlite3_multiplex_shutdown
574 do_test multiplex-6.0.0 {
575 multiplex_delete test.db
576 multiplex_delete test.x
577 sqlite3_multiplex_initialize "" 1
579 multiplex_set db main 4096 16
582 do_test multiplex-6.1.0 {
584 PRAGMA page_size=1024;
585 PRAGMA journal_mode=DELETE;
586 PRAGMA auto_vacuum=OFF;
589 CREATE TABLE t1(a, b);
590 INSERT INTO t1 VALUES(1, randomblob($g_chunk_size));
591 INSERT INTO t1 VALUES(2, randomblob($g_chunk_size));
594 do_test multiplex-6.2.1 { file size [multiplex_name test.x 0] } [list $g_chunk_size]
595 do_test multiplex-6.2.2 { file size [multiplex_name test.x 1] } [list $g_chunk_size]
597 do_test multiplex-6.3.0 {
601 do_test multiplex-6.99 {
603 multiplex_delete test.x
604 sqlite3_multiplex_shutdown
611 catch { sqlite3_multiplex_shutdown }