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}}
208 do_test multiplex-2.4.4 { file size [multiplex_name test.x 0] } {7168}
209 do_test multiplex-2.4.5 {
216 do_test multiplex-2.4.99 {
217 sqlite3_multiplex_shutdown
220 do_test multiplex-2.5.1 {
221 multiplex_delete test.x
222 sqlite3_multiplex_initialize "" 1
224 multiplex_set db main 4096 16
227 do_test multiplex-2.5.2 {
229 PRAGMA page_size = 1024;
230 PRAGMA journal_mode = delete;
231 PRAGMA auto_vacuum = off;
232 CREATE TABLE t1(a PRIMARY KEY, b);
236 do_test multiplex-2.5.3 {
238 INSERT INTO t1 VALUES(1, 'one');
239 INSERT INTO t1 VALUES(2, randomblob(4000));
240 INSERT INTO t1 VALUES(3, 'three');
241 INSERT INTO t1 VALUES(4, randomblob(4000));
242 INSERT INTO t1 VALUES(5, 'five');
243 INSERT INTO t1 VALUES(6, randomblob($g_chunk_size));
244 INSERT INTO t1 VALUES(7, randomblob($g_chunk_size));
248 do_test multiplex-2.5.4 {
249 db eval {SELECT * FROM t1 WHERE a=1}
252 do_test multiplex-2.5.5 {
253 db eval {SELECT * FROM t1 WHERE a=3}
256 do_test multiplex-2.5.6 {
257 db eval {SELECT * FROM t1 WHERE a=5}
260 do_test multiplex-2.5.7 {
261 db eval {SELECT a,length(b) FROM t1 WHERE a=2}
264 do_test multiplex-2.5.8 {
265 db eval {SELECT a,length(b) FROM t1 WHERE a=4}
268 do_test multiplex-2.5.9 { file size [multiplex_name test.x 0] } [list $g_chunk_size]
269 do_test multiplex-2.5.10 { file size [multiplex_name test.x 1] } [list $g_chunk_size]
271 do_test multiplex-2.5.99 {
273 sqlite3_multiplex_shutdown
277 set all_journal_modes {delete persist truncate memory off}
278 foreach jmode $all_journal_modes {
279 for {set sz 151} {$sz<8000} {set sz [expr $sz+419]} {
281 do_test multiplex-2.6.1.$sz.$jmode {
282 multiplex_delete test.db
283 sqlite3_multiplex_initialize "" 1
285 multiplex_set db main $sz 32
288 do_test multiplex-2.6.2.$sz.$jmode {
290 PRAGMA page_size = 1024;
291 PRAGMA auto_vacuum = off;
293 db eval "PRAGMA journal_mode = $jmode;"
296 do_test multiplex-2.6.3.$sz.$jmode {
298 CREATE TABLE t1(a PRIMARY KEY, b);
299 INSERT INTO t1 VALUES(1, 'one');
300 INSERT INTO t1 VALUES(2, randomblob($g_chunk_size));
304 do_test multiplex-2.6.4.$sz.$jmode {
305 db eval {SELECT b FROM t1 WHERE a=1}
308 do_test multiplex-2.6.5.$sz.$jmode {
309 db eval {SELECT length(b) FROM t1 WHERE a=2}
310 } [list $g_chunk_size]
312 do_test multiplex-2.6.6.$sz.$jmode { file size [multiplex_name test.db 0] } [list $g_chunk_size]
314 do_test multiplex-2.6.99.$sz.$jmode {
316 sqlite3_multiplex_shutdown
322 do_test multiplex-2.7.1 { multiplex_delete test.db } {}
323 do_test multiplex-2.7.2 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
324 do_test multiplex-2.7.3 { sqlite3 db test.db } {}
325 do_test multiplex-2.7.4 { lindex [ catchsql { SELECT multiplex_control(2, 65536); } ] 0 } {0}
326 do_test multiplex-2.7.5 { lindex [ catchsql { SELECT multiplex_control(1, 0); } ] 0 } {0}
327 do_test multiplex-2.7.6 {
329 CREATE TABLE t1(a PRIMARY KEY, b);
330 INSERT INTO t1 VALUES(1, randomblob(1000));
333 # verify only one file, and file size is less than chunks size
334 do_test multiplex-2.7.7 { expr ([file size [multiplex_name test.db 0]] < 65536) } {1}
335 do_test multiplex-2.7.8 { file exists [multiplex_name test.db 1] } {0}
336 do_test multiplex-2.7.9 {
338 INSERT INTO t1 VALUES(2, randomblob(65536));
341 # verify only one file, and file size exceeds chunks size
342 do_test multiplex-2.7.10 { expr ([file size [multiplex_name test.db 0]] > 65536) } {1}
343 do_test multiplex-2.7.11 { file exists [multiplex_name test.db 1] } {0}
344 do_test multiplex-2.7.12 { db close } {}
345 do_test multiplex-2.7.13 { sqlite3_multiplex_shutdown } {SQLITE_OK}
347 #-------------------------------------------------------------------------
348 # Try some tests with more than one connection to a database file. Still
351 # multiplex-3.1.*: Two connections to a single database file.
353 # multiplex-3.2.*: Two connections to each of several database files (that
354 # are in the same multiplex group).
356 do_test multiplex-3.1.1 {
357 multiplex_delete test.db
358 sqlite3_multiplex_initialize "" 1
360 multiplex_set db main 32768 16
362 do_test multiplex-3.1.2 {
364 PRAGMA page_size = 1024;
365 PRAGMA journal_mode = delete;
366 PRAGMA auto_vacuum = off;
367 CREATE TABLE t1(a PRIMARY KEY, b);
368 INSERT INTO t1 VALUES(1, 'one');
370 file size [multiplex_name test.db 0]
372 do_test multiplex-3.1.3 {
374 execsql { CREATE TABLE t2(a, b) } db2
376 do_test multiplex-3.1.4 {
377 execsql { CREATE TABLE t3(a, b) }
379 do_test multiplex-3.1.5 {
380 catchsql { CREATE TABLE t3(a, b) }
381 } {1 {table t3 already exists}}
382 do_test multiplex-3.1.6 {
387 do_test multiplex-3.2.1a {
389 multiplex_delete test.db
390 multiplex_delete test2.db
393 sqlite3 db2a test2.db
395 foreach db {db1a db2a} {
397 PRAGMA page_size = 1024;
398 PRAGMA journal_mode = delete;
399 PRAGMA auto_vacuum = off;
400 CREATE TABLE t1(a, b);
404 list [file size [multiplex_name test.db 0]] [file size [multiplex_name test2.db 0]]
407 do_test multiplex-3.2.1b {
409 sqlite3 db2b test2.db
412 do_test multiplex-3.2.2 { execsql { INSERT INTO t1 VALUES('x', 'y') } db1a } {}
413 do_test multiplex-3.2.3 { execsql { INSERT INTO t1 VALUES('v', 'w') } db1b } {}
414 do_test multiplex-3.2.4 { execsql { INSERT INTO t1 VALUES('t', 'u') } db2a } {}
415 do_test multiplex-3.2.5 { execsql { INSERT INTO t1 VALUES('r', 's') } db2b } {}
417 do_test multiplex-3.2.6 {
418 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1a
420 do_test multiplex-3.2.7 {
421 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1b
423 do_test multiplex-3.2.8 {
424 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2a
426 do_test multiplex-3.2.9 {
427 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2b
430 do_test multiplex-3.3.1 {
431 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1a
432 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1b
433 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2a
434 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2b
437 do_test multiplex-3.2.X {
438 foreach db {db1a db2a db2b db1b} { catch { $db close } }
441 #-------------------------------------------------------------------------
444 sqlite3_multiplex_initialize "" 1
445 multiplex_set db main 32768 16
447 # Return a list of all currently defined multiplexs.
448 proc multiplex_list {} {
449 glob -nocomplain test2.db*
452 do_test multiplex-4.1.6 {
453 multiplex_delete test2.db
455 db eval {CREATE TABLE t2(x); INSERT INTO t2 VALUES('tab-t2');}
456 set res [multiplex_list]
457 list [regexp {test2.db} $res]
459 do_test multiplex-4.1.6a {
461 db2 eval {SELECT * FROM t2}
463 do_test multiplex-4.1.7 {
464 execsql {INSERT INTO t2 VALUES(zeroblob(200000))}
466 do_test multiplex-4.1.8 {
468 db2 eval {SELECT count(*) FROM t2}
470 do_test multiplex-4.1.8a {
471 db2 eval { DELETE FROM t2 WHERE x = 'tab-t2' }
473 do_test multiplex-4.1.8b {
475 db2 eval {SELECT count(*) FROM t2}
479 do_test multiplex-4.1.9 {
480 execsql {INSERT INTO t2 VALUES(zeroblob(200000))}
482 do_test multiplex-4.1.10 {
483 set res [multiplex_list]
484 list [regexp {test2.db} $res]
486 do_test multiplex-4.1.11 {
488 set res [multiplex_list]
489 list [regexp {test2.db} $res]
491 do_test multiplex-4.1.12 {
497 #-------------------------------------------------------------------------
498 # The following tests test that the multiplex VFS handles malloc and IO
502 sqlite3_multiplex_initialize "" 1
503 multiplex_set db main 32768 16
505 do_faultsim_test multiplex-5.1 -prep {
510 do_faultsim_test multiplex-5.2 -prep {
517 multiplex_delete test.db
518 multiplex_delete test2.db
520 do_test multiplex-5.3.prep {
523 PRAGMA auto_vacuum = 1;
524 PRAGMA page_size = 1024;
525 CREATE TABLE t1(a, b);
526 INSERT INTO t1 VALUES(10, zeroblob(1200));
528 faultsim_save_and_close
530 do_faultsim_test multiplex-5.3 -prep {
531 faultsim_restore_and_reopen
533 execsql { DELETE FROM t1 }
536 do_test multiplex-5.4.1 {
538 multiplex_delete test.db
540 list [catch { sqlite3 db test.db } msg] $msg
541 } {1 {unable to open database file}}
542 catch { delete_file test.db }
544 do_faultsim_test multiplex-5.5 -prep {
545 catch { sqlite3_multiplex_shutdown }
547 sqlite3_multiplex_initialize "" 1
548 multiplex_set db main 32768 16
551 #-------------------------------------------------------------------------
552 # Test that you can vacuum a multiplex'ed DB.
556 sqlite3_multiplex_shutdown
557 do_test multiplex-6.0.0 {
558 multiplex_delete test.db
559 multiplex_delete test.x
560 sqlite3_multiplex_initialize "" 1
562 multiplex_set db main 4096 16
565 do_test multiplex-6.1.0 {
567 PRAGMA page_size=1024;
568 PRAGMA journal_mode=DELETE;
569 PRAGMA auto_vacuum=OFF;
572 CREATE TABLE t1(a, b);
573 INSERT INTO t1 VALUES(1, randomblob($g_chunk_size));
574 INSERT INTO t1 VALUES(2, randomblob($g_chunk_size));
577 do_test multiplex-6.2.1 { file size [multiplex_name test.x 0] } [list $g_chunk_size]
578 do_test multiplex-6.2.2 { file size [multiplex_name test.x 1] } [list $g_chunk_size]
580 do_test multiplex-6.3.0 {
584 do_test multiplex-6.99 {
586 multiplex_delete test.x
587 sqlite3_multiplex_shutdown
594 catch { sqlite3_multiplex_shutdown }