blob: 2f1a02fb876c68c46428052ca4c3406528357286 [file] [log] [blame]
shaneh8a922f72010-11-04 20:50:27 +00001# 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
13set testdir [file dirname $argv0]
14source $testdir/tester.tcl
15source $testdir/malloc_common.tcl
16
drh658dd582011-12-13 15:25:06 +000017# The tests in this file assume that SQLite is compiled without
18# ENABLE_8_3_NAMES.
19#
20ifcapable 8_3_names {
21 puts -nonewline "SQLite compiled with SQLITE_ENABLE_8_3_NAMES. "
22 puts "Skipping tests multiplex-*."
23 finish_test
24 return
25}
26
shanehc27fa4b2011-03-31 15:11:53 +000027set g_chunk_size [ expr ($::SQLITE_MAX_PAGE_SIZE*16384) ]
shanehb5830292010-11-05 17:51:25 +000028set g_max_chunks 32
29
shaneh050d09a2010-11-08 19:16:16 +000030# 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.
35proc multiplex_name {name chunk} {
36 if {$chunk==0} { return $name }
drh658dd582011-12-13 15:25:06 +000037 set num [format "%03d" $chunk]
shaneh050d09a2010-11-08 19:16:16 +000038 ifcapable {multiplex_ext_overwrite} {
shanehf5913a22010-12-01 22:08:45 +000039 set name [string range $name 0 [expr [string length $name]-2-1]]
shaneh050d09a2010-11-08 19:16:16 +000040 }
41 return $name$num
42}
43
44# This saves off the parameters and calls the
shanehd50deee2011-03-29 05:06:46 +000045# underlying sqlite3_multiplex_control() API.
46proc multiplex_set {db name chunk_size max_chunks} {
shanehb5830292010-11-05 17:51:25 +000047 global g_chunk_size
48 global g_max_chunks
shaneh78c4de42011-03-31 05:31:24 +000049 set g_chunk_size [ expr (($chunk_size+($::SQLITE_MAX_PAGE_SIZE-1)) & ~($::SQLITE_MAX_PAGE_SIZE-1)) ]
shanehb5830292010-11-05 17:51:25 +000050 set g_max_chunks $max_chunks
shanehd50deee2011-03-29 05:06:46 +000051 set rc [catch {sqlite3_multiplex_control $db $name chunk_size $chunk_size} msg]
52 if { $rc==0 } {
53 set rc [catch {sqlite3_multiplex_control $db $name max_chunks $max_chunks} msg]
54 }
55 list $msg
shanehb5830292010-11-05 17:51:25 +000056}
57
shaneh050d09a2010-11-08 19:16:16 +000058# This attempts to delete the base file and
59# and files with the chunk extension.
shaneh8a922f72010-11-04 20:50:27 +000060proc multiplex_delete {name} {
shanehb5830292010-11-05 17:51:25 +000061 global g_max_chunks
drhf3717af2011-07-20 16:35:31 +000062 forcedelete $name
shaneh050d09a2010-11-08 19:16:16 +000063 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]
shaneh8a922f72010-11-04 20:50:27 +000067 }
68}
69
70db close
drha1a82982014-07-30 15:43:05 +000071sqlite3_shutdown
72test_sqlite3_log xLog
73proc xLog {error_code msg} {
74 lappend ::log $error_code $msg
75}
76unset -nocomplain log
shaneh8a922f72010-11-04 20:50:27 +000077
shanehf5913a22010-12-01 22:08:45 +000078multiplex_delete test.db
79multiplex_delete test2.db
80
shanehfd1552f2010-11-05 03:43:54 +000081#-------------------------------------------------------------------------
82# multiplex-1.1.*: Test initialize and shutdown.
83
shaneh8a922f72010-11-04 20:50:27 +000084do_test multiplex-1.1 { sqlite3_multiplex_initialize nosuchvfs 1 } {SQLITE_ERROR}
85do_test multiplex-1.2 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
86do_test multiplex-1.3 { sqlite3_multiplex_initialize "" 1 } {SQLITE_MISUSE}
87do_test multiplex-1.4 { sqlite3_multiplex_shutdown } {SQLITE_OK}
88
89do_test multiplex-1.5 { sqlite3_multiplex_initialize "" 0 } {SQLITE_OK}
90do_test multiplex-1.6 { sqlite3_multiplex_shutdown } {SQLITE_OK}
91do_test multiplex-1.7 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
92do_test multiplex-1.8 { sqlite3_multiplex_shutdown } {SQLITE_OK}
93
shaneh78c4de42011-03-31 05:31:24 +000094
shanehc27fa4b2011-03-31 15:11:53 +000095do_test multiplex-1.9.1 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
96do_test multiplex-1.9.2 { sqlite3 db test.db } {}
97do_test multiplex-1.9.3 { multiplex_set db main 32768 16 } {SQLITE_OK}
drhf3717af2011-07-20 16:35:31 +000098do_test multiplex-1.9.4 { multiplex_set db main 32768 -1 } {SQLITE_OK}
shanehc27fa4b2011-03-31 15:11:53 +000099do_test multiplex-1.9.6 { multiplex_set db main 31 16 } {SQLITE_OK}
drhf3717af2011-07-20 16:35:31 +0000100do_test multiplex-1.9.7 { multiplex_set db main 32768 100 } {SQLITE_OK}
shanehc27fa4b2011-03-31 15:11:53 +0000101do_test multiplex-1.9.8 { multiplex_set db main 1073741824 1 } {SQLITE_OK}
102do_test multiplex-1.9.9 { db close } {}
103do_test multiplex-1.9.10 { sqlite3_multiplex_shutdown } {SQLITE_OK}
104
shaneh3801b652011-04-01 14:22:46 +0000105do_test multiplex-1.10.1 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
106do_test multiplex-1.10.2 { sqlite3 db test.db } {}
107do_test multiplex-1.10.3 { lindex [ catchsql { SELECT multiplex_control(2, 32768); } ] 0 } {0}
drhf3717af2011-07-20 16:35:31 +0000108do_test multiplex-1.10.4 { lindex [ catchsql { SELECT multiplex_control(3, -1); } ] 0 } {0}
shaneh3801b652011-04-01 14:22:46 +0000109do_test multiplex-1.10.6 { lindex [ catchsql { SELECT multiplex_control(2, 31); } ] 0 } {0}
drhf3717af2011-07-20 16:35:31 +0000110do_test multiplex-1.10.7 { lindex [ catchsql { SELECT multiplex_control(3, 100); } ] 0 } {0}
shaneh3801b652011-04-01 14:22:46 +0000111do_test multiplex-1.10.8 { lindex [ catchsql { SELECT multiplex_control(2, 1073741824); } ] 0 } {0}
112do_test multiplex-1.10.9 { db close } {}
113do_test multiplex-1.10.10 { sqlite3_multiplex_shutdown } {SQLITE_OK}
shanehc27fa4b2011-03-31 15:11:53 +0000114
shaneh3801b652011-04-01 14:22:46 +0000115do_test multiplex-1.11.1 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
116do_test multiplex-1.11.2 { sqlite3 db test.db } {}
117do_test multiplex-1.11.3 { sqlite3_multiplex_control db main enable 0 } {SQLITE_OK}
118do_test multiplex-1.11.4 { sqlite3_multiplex_control db main enable 1 } {SQLITE_OK}
119do_test multiplex-1.11.5 { sqlite3_multiplex_control db main enable -1 } {SQLITE_OK}
120do_test multiplex-1.11.6 { db close } {}
121do_test multiplex-1.11.7 { sqlite3_multiplex_shutdown } {SQLITE_OK}
shanehc27fa4b2011-03-31 15:11:53 +0000122
shaneh3801b652011-04-01 14:22:46 +0000123do_test multiplex-1.12.1 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
124do_test multiplex-1.12.2 { sqlite3 db test.db } {}
125do_test multiplex-1.12.3 { lindex [ catchsql { SELECT multiplex_control(1, 0); } ] 0 } {0}
126do_test multiplex-1.12.4 { lindex [ catchsql { SELECT multiplex_control(1, 1); } ] 0 } {0}
127do_test multiplex-1.12.5 { lindex [ catchsql { SELECT multiplex_control(1, -1); } ] 0 } {0}
128do_test multiplex-1.12.6 { db close } {}
129do_test multiplex-1.12.7 { sqlite3_multiplex_shutdown } {SQLITE_OK}
130
131do_test multiplex-1.13.1 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
132do_test multiplex-1.13.2 { sqlite3 db test.db } {}
133do_test multiplex-1.13.3 { lindex [ catchsql { SELECT multiplex_control(-1, 0); } ] 0 } {1}
134do_test multiplex-1.13.4 { lindex [ catchsql { SELECT multiplex_control(4, 1); } ] 0 } {1}
135do_test multiplex-1.13.6 { db close } {}
136do_test multiplex-1.13.7 { sqlite3_multiplex_shutdown } {SQLITE_OK}
shanehe5a6ad62010-11-05 03:58:58 +0000137
shaneh8a922f72010-11-04 20:50:27 +0000138#-------------------------------------------------------------------------
139# Some simple warm-body tests with a single database file in rollback
140# mode:
141#
142# multiplex-2.1.*: Test simple writing to a multiplex file.
143#
144# multiplex-2.2.*: More writing.
145#
146# multiplex-2.3.*: Open and close a second db.
147#
shanehc27fa4b2011-03-31 15:11:53 +0000148# multiplex-2.4.*: Try to shutdown the multiplex system before closing the db
shaneh8a922f72010-11-04 20:50:27 +0000149# 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.
152#
shanehb5830292010-11-05 17:51:25 +0000153# multiplex-2.5.*: More reading/writing.
154#
shaneh0596bee2010-11-05 20:50:43 +0000155# multiplex-2.6.*: More reading/writing with varying small chunk sizes, as
156# well as varying journal mode.
shanehc27fa4b2011-03-31 15:11:53 +0000157#
158# multiplex-2.7.*: Disable/enable tests.
159#
shaneh8a922f72010-11-04 20:50:27 +0000160
161sqlite3_multiplex_initialize "" 1
shanehd50deee2011-03-29 05:06:46 +0000162multiplex_set db main 32768 16
shaneh8a922f72010-11-04 20:50:27 +0000163
mistachkinfda06be2011-08-02 00:57:34 +0000164forcedelete test.x
drhec0c7652012-01-09 13:41:59 +0000165foreach f [glob -nocomplain {test.x*[0-9][0-9][0-9]}] {
166 forcedelete $f
167}
shaneh8a922f72010-11-04 20:50:27 +0000168do_test multiplex-2.1.2 {
drhf3717af2011-07-20 16:35:31 +0000169 sqlite3 db test.x
shaneh8a922f72010-11-04 20:50:27 +0000170 execsql {
171 PRAGMA page_size=1024;
172 PRAGMA auto_vacuum=OFF;
173 PRAGMA journal_mode=DELETE;
174 }
175 execsql {
176 CREATE TABLE t1(a, b);
177 INSERT INTO t1 VALUES(1, randomblob(1100));
178 INSERT INTO t1 VALUES(2, randomblob(1100));
179 }
180} {}
drhf3717af2011-07-20 16:35:31 +0000181do_test multiplex-2.1.3 { file size [multiplex_name test.x 0] } {4096}
shaneh8a922f72010-11-04 20:50:27 +0000182do_test multiplex-2.1.4 {
183 execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
184} {}
drh37bbcb42021-10-29 12:29:22 +0000185do_execsql_test multiplex-2.1.5 {
186 PRAGMA multiplex_enabled;
187 PRAGMA multiplex_filecount;
188 PRAGMA multiplex_chunksize;
189} {1 1 2147418112}
shaneh8a922f72010-11-04 20:50:27 +0000190
191do_test multiplex-2.2.1 {
192 execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
193} {}
drhf3717af2011-07-20 16:35:31 +0000194do_test multiplex-2.2.3 { file size [multiplex_name test.x 0] } {6144}
shaneh8a922f72010-11-04 20:50:27 +0000195
196do_test multiplex-2.3.1 {
drhf3717af2011-07-20 16:35:31 +0000197 sqlite3 db2 test2.x
shaneh8a922f72010-11-04 20:50:27 +0000198 db2 close
199} {}
200
shanehd50deee2011-03-29 05:06:46 +0000201
drha1a82982014-07-30 15:43:05 +0000202unset -nocomplain ::log
dandadafa82016-10-27 14:51:02 +0000203#do_test multiplex-2.4.1 {
204# sqlite3_multiplex_shutdown
205#} {SQLITE_MISUSE}
shaneh8a922f72010-11-04 20:50:27 +0000206do_test multiplex-2.4.2 {
207 execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
208} {}
dandadafa82016-10-27 14:51:02 +0000209#do_test multiplex-2.4.3 {
210# set ::log
211#} {SQLITE_MISUSE {sqlite3_multiplex_shutdown() called while database connections are still open}}
212
drhf3717af2011-07-20 16:35:31 +0000213do_test multiplex-2.4.4 { file size [multiplex_name test.x 0] } {7168}
drhec0c7652012-01-09 13:41:59 +0000214do_test multiplex-2.4.5 {
shaneh8a922f72010-11-04 20:50:27 +0000215 db close
drhec0c7652012-01-09 13:41:59 +0000216 sqlite3 db test.x
217 db eval vacuum
218 db close
219 glob test.x*
220} {test.x}
221do_test multiplex-2.4.99 {
shaneh8a922f72010-11-04 20:50:27 +0000222 sqlite3_multiplex_shutdown
223} {SQLITE_OK}
224
shanehfd1552f2010-11-05 03:43:54 +0000225do_test multiplex-2.5.1 {
drhf3717af2011-07-20 16:35:31 +0000226 multiplex_delete test.x
shanehfd1552f2010-11-05 03:43:54 +0000227 sqlite3_multiplex_initialize "" 1
drhf3717af2011-07-20 16:35:31 +0000228 sqlite3 db test.x
shanehd50deee2011-03-29 05:06:46 +0000229 multiplex_set db main 4096 16
shanehfd1552f2010-11-05 03:43:54 +0000230} {SQLITE_OK}
231
232do_test multiplex-2.5.2 {
shanehfd1552f2010-11-05 03:43:54 +0000233 execsql {
234 PRAGMA page_size = 1024;
235 PRAGMA journal_mode = delete;
236 PRAGMA auto_vacuum = off;
237 CREATE TABLE t1(a PRIMARY KEY, b);
238 }
239} {delete}
240
241do_test multiplex-2.5.3 {
242 execsql {
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));
shaneh78c4de42011-03-31 05:31:24 +0000247 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));
shanehfd1552f2010-11-05 03:43:54 +0000250 }
251} {}
252
253do_test multiplex-2.5.4 {
254 db eval {SELECT * FROM t1 WHERE a=1}
255} {1 one}
256
257do_test multiplex-2.5.5 {
258 db eval {SELECT * FROM t1 WHERE a=3}
259} {3 three}
260
261do_test multiplex-2.5.6 {
262 db eval {SELECT * FROM t1 WHERE a=5}
263} {5 five}
264
265do_test multiplex-2.5.7 {
266 db eval {SELECT a,length(b) FROM t1 WHERE a=2}
267} {2 4000}
268
269do_test multiplex-2.5.8 {
270 db eval {SELECT a,length(b) FROM t1 WHERE a=4}
271} {4 4000}
272
drhf3717af2011-07-20 16:35:31 +0000273do_test multiplex-2.5.9 { file size [multiplex_name test.x 0] } [list $g_chunk_size]
274do_test multiplex-2.5.10 { file size [multiplex_name test.x 1] } [list $g_chunk_size]
drh37bbcb42021-10-29 12:29:22 +0000275do_execsql_test multiplex-2.5.11 {
276 PRAGMA multiplex_enabled;
277 PRAGMA multiplex_filecount;
278 PRAGMA multiplex_chunksize;
279} {1 3 65536}
280sqlite3 db test.x
281do_execsql_test multiplex-2.5.12 {
282 PRAGMA multiplex_filecount;
283 PRAGMA multiplex_chunksize;
284} {3 65536}
285
286
shanehb5830292010-11-05 17:51:25 +0000287
shanehfd1552f2010-11-05 03:43:54 +0000288do_test multiplex-2.5.99 {
289 db close
290 sqlite3_multiplex_shutdown
291} {SQLITE_OK}
292
shanehb5830292010-11-05 17:51:25 +0000293
shaneh0596bee2010-11-05 20:50:43 +0000294set all_journal_modes {delete persist truncate memory off}
295foreach jmode $all_journal_modes {
296 for {set sz 151} {$sz<8000} {set sz [expr $sz+419]} {
shanehb5830292010-11-05 17:51:25 +0000297
shaneh0596bee2010-11-05 20:50:43 +0000298 do_test multiplex-2.6.1.$sz.$jmode {
299 multiplex_delete test.db
300 sqlite3_multiplex_initialize "" 1
shanehac039682011-03-30 21:03:07 +0000301 sqlite3 db test.db
shanehd50deee2011-03-29 05:06:46 +0000302 multiplex_set db main $sz 32
shaneh0596bee2010-11-05 20:50:43 +0000303 } {SQLITE_OK}
shanehb5830292010-11-05 17:51:25 +0000304
shaneh0596bee2010-11-05 20:50:43 +0000305 do_test multiplex-2.6.2.$sz.$jmode {
shaneh0596bee2010-11-05 20:50:43 +0000306 db eval {
307 PRAGMA page_size = 1024;
308 PRAGMA auto_vacuum = off;
309 }
310 db eval "PRAGMA journal_mode = $jmode;"
311 } $jmode
shanehb5830292010-11-05 17:51:25 +0000312
shaneh0596bee2010-11-05 20:50:43 +0000313 do_test multiplex-2.6.3.$sz.$jmode {
314 execsql {
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));
318 }
319 } {}
shanehb5830292010-11-05 17:51:25 +0000320
shaneh0596bee2010-11-05 20:50:43 +0000321 do_test multiplex-2.6.4.$sz.$jmode {
322 db eval {SELECT b FROM t1 WHERE a=1}
323 } {one}
shanehb5830292010-11-05 17:51:25 +0000324
shaneh0596bee2010-11-05 20:50:43 +0000325 do_test multiplex-2.6.5.$sz.$jmode {
326 db eval {SELECT length(b) FROM t1 WHERE a=2}
327 } [list $g_chunk_size]
shanehb5830292010-11-05 17:51:25 +0000328
shaneh050d09a2010-11-08 19:16:16 +0000329 do_test multiplex-2.6.6.$sz.$jmode { file size [multiplex_name test.db 0] } [list $g_chunk_size]
shanehb5830292010-11-05 17:51:25 +0000330
shaneh0596bee2010-11-05 20:50:43 +0000331 do_test multiplex-2.6.99.$sz.$jmode {
332 db close
333 sqlite3_multiplex_shutdown
334 } {SQLITE_OK}
shanehb5830292010-11-05 17:51:25 +0000335
shaneh0596bee2010-11-05 20:50:43 +0000336 }
shanehb5830292010-11-05 17:51:25 +0000337}
338
shaneh3801b652011-04-01 14:22:46 +0000339do_test multiplex-2.7.1 { multiplex_delete test.db } {}
340do_test multiplex-2.7.2 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
341do_test multiplex-2.7.3 { sqlite3 db test.db } {}
342do_test multiplex-2.7.4 { lindex [ catchsql { SELECT multiplex_control(2, 65536); } ] 0 } {0}
343do_test multiplex-2.7.5 { lindex [ catchsql { SELECT multiplex_control(1, 0); } ] 0 } {0}
shanehc27fa4b2011-03-31 15:11:53 +0000344do_test multiplex-2.7.6 {
345 execsql {
346 CREATE TABLE t1(a PRIMARY KEY, b);
347 INSERT INTO t1 VALUES(1, randomblob(1000));
348 }
349} {}
350# verify only one file, and file size is less than chunks size
351do_test multiplex-2.7.7 { expr ([file size [multiplex_name test.db 0]] < 65536) } {1}
352do_test multiplex-2.7.8 { file exists [multiplex_name test.db 1] } {0}
353do_test multiplex-2.7.9 {
354 execsql {
shaneh3801b652011-04-01 14:22:46 +0000355 INSERT INTO t1 VALUES(2, randomblob(65536));
shanehc27fa4b2011-03-31 15:11:53 +0000356 }
357} {}
358# verify only one file, and file size exceeds chunks size
359do_test multiplex-2.7.10 { expr ([file size [multiplex_name test.db 0]] > 65536) } {1}
360do_test multiplex-2.7.11 { file exists [multiplex_name test.db 1] } {0}
361do_test multiplex-2.7.12 { db close } {}
362do_test multiplex-2.7.13 { sqlite3_multiplex_shutdown } {SQLITE_OK}
363
shaneh8a922f72010-11-04 20:50:27 +0000364#-------------------------------------------------------------------------
365# Try some tests with more than one connection to a database file. Still
366# in rollback mode.
367#
368# multiplex-3.1.*: Two connections to a single database file.
369#
370# multiplex-3.2.*: Two connections to each of several database files (that
371# are in the same multiplex group).
372#
373do_test multiplex-3.1.1 {
374 multiplex_delete test.db
375 sqlite3_multiplex_initialize "" 1
shanehac039682011-03-30 21:03:07 +0000376 sqlite3 db test.db
shanehd50deee2011-03-29 05:06:46 +0000377 multiplex_set db main 32768 16
shaneh8a922f72010-11-04 20:50:27 +0000378} {SQLITE_OK}
379do_test multiplex-3.1.2 {
shaneh8a922f72010-11-04 20:50:27 +0000380 execsql {
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');
386 }
shaneh050d09a2010-11-08 19:16:16 +0000387 file size [multiplex_name test.db 0]
shaneh8a922f72010-11-04 20:50:27 +0000388} {3072}
389do_test multiplex-3.1.3 {
390 sqlite3 db2 test.db
391 execsql { CREATE TABLE t2(a, b) } db2
392} {}
393do_test multiplex-3.1.4 {
394 execsql { CREATE TABLE t3(a, b) }
395} {}
396do_test multiplex-3.1.5 {
397 catchsql { CREATE TABLE t3(a, b) }
398} {1 {table t3 already exists}}
399do_test multiplex-3.1.6 {
400 db close
401 db2 close
402} {}
403
404do_test multiplex-3.2.1a {
405
406 multiplex_delete test.db
407 multiplex_delete test2.db
408
409 sqlite3 db1a test.db
410 sqlite3 db2a test2.db
411
412 foreach db {db1a db2a} {
413 execsql {
414 PRAGMA page_size = 1024;
415 PRAGMA journal_mode = delete;
416 PRAGMA auto_vacuum = off;
417 CREATE TABLE t1(a, b);
418 } $db
419 }
420
shaneh050d09a2010-11-08 19:16:16 +0000421 list [file size [multiplex_name test.db 0]] [file size [multiplex_name test2.db 0]]
shaneh8a922f72010-11-04 20:50:27 +0000422} {2048 2048}
423
424do_test multiplex-3.2.1b {
425 sqlite3 db1b test.db
426 sqlite3 db2b test2.db
427} {}
428
429do_test multiplex-3.2.2 { execsql { INSERT INTO t1 VALUES('x', 'y') } db1a } {}
430do_test multiplex-3.2.3 { execsql { INSERT INTO t1 VALUES('v', 'w') } db1b } {}
431do_test multiplex-3.2.4 { execsql { INSERT INTO t1 VALUES('t', 'u') } db2a } {}
432do_test multiplex-3.2.5 { execsql { INSERT INTO t1 VALUES('r', 's') } db2b } {}
433
434do_test multiplex-3.2.6 {
435 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1a
436} {}
437do_test multiplex-3.2.7 {
438 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1b
439} {}
440do_test multiplex-3.2.8 {
441 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2a
442} {}
443do_test multiplex-3.2.9 {
444 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2b
445} {}
446
447do_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
452} {}
453
454do_test multiplex-3.2.X {
455 foreach db {db1a db2a db2b db1b} { catch { $db close } }
456} {}
457
458#-------------------------------------------------------------------------
459#
460
461sqlite3_multiplex_initialize "" 1
shanehd50deee2011-03-29 05:06:46 +0000462multiplex_set db main 32768 16
shaneh8a922f72010-11-04 20:50:27 +0000463
464# Return a list of all currently defined multiplexs.
465proc multiplex_list {} {
dandadafa82016-10-27 14:51:02 +0000466 glob -nocomplain test2.db*
shaneh8a922f72010-11-04 20:50:27 +0000467}
468
469do_test multiplex-4.1.6 {
470 multiplex_delete test2.db
471 sqlite3 db 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]
475} {1}
476do_test multiplex-4.1.6a {
477 sqlite3 db2 test2.db
478 db2 eval {SELECT * FROM t2}
479} {tab-t2}
480do_test multiplex-4.1.7 {
481 execsql {INSERT INTO t2 VALUES(zeroblob(200000))}
482} {}
483do_test multiplex-4.1.8 {
484 sqlite3 db2 test2.db
485 db2 eval {SELECT count(*) FROM t2}
486} {2}
487do_test multiplex-4.1.8a {
488 db2 eval { DELETE FROM t2 WHERE x = 'tab-t2' }
489} {}
490do_test multiplex-4.1.8b {
491 sqlite3 db2 test2.db
492 db2 eval {SELECT count(*) FROM t2}
493} {1}
494
495
496do_test multiplex-4.1.9 {
497 execsql {INSERT INTO t2 VALUES(zeroblob(200000))}
498} {}
499do_test multiplex-4.1.10 {
500 set res [multiplex_list]
501 list [regexp {test2.db} $res]
502} {1}
503do_test multiplex-4.1.11 {
504 db2 close
505 set res [multiplex_list]
506 list [regexp {test2.db} $res]
507} {1}
508do_test multiplex-4.1.12 {
509 db close
510 multiplex_list
dandadafa82016-10-27 14:51:02 +0000511} {test2.db}
shaneh8a922f72010-11-04 20:50:27 +0000512
513
514#-------------------------------------------------------------------------
515# The following tests test that the multiplex VFS handles malloc and IO
516# errors.
517#
518
519sqlite3_multiplex_initialize "" 1
shanehd50deee2011-03-29 05:06:46 +0000520multiplex_set db main 32768 16
shaneh8a922f72010-11-04 20:50:27 +0000521
522do_faultsim_test multiplex-5.1 -prep {
523 catch {db close}
524} -body {
525 sqlite3 db test2.db
526}
527do_faultsim_test multiplex-5.2 -prep {
528 catch {db close}
529} -body {
530 sqlite3 db test.db
531}
532
533catch { db close }
534multiplex_delete test.db
shanehb5830292010-11-05 17:51:25 +0000535multiplex_delete test2.db
shaneh8a922f72010-11-04 20:50:27 +0000536
537do_test multiplex-5.3.prep {
538 sqlite3 db test.db
539 execsql {
540 PRAGMA auto_vacuum = 1;
541 PRAGMA page_size = 1024;
542 CREATE TABLE t1(a, b);
543 INSERT INTO t1 VALUES(10, zeroblob(1200));
544 }
545 faultsim_save_and_close
546} {}
547do_faultsim_test multiplex-5.3 -prep {
548 faultsim_restore_and_reopen
549} -body {
550 execsql { DELETE FROM t1 }
551}
552
553do_test multiplex-5.4.1 {
554 catch { db close }
555 multiplex_delete test.db
556 file mkdir test.db
557 list [catch { sqlite3 db test.db } msg] $msg
558} {1 {unable to open database file}}
mistachkinfda06be2011-08-02 00:57:34 +0000559catch { delete_file test.db }
shaneh8a922f72010-11-04 20:50:27 +0000560
561do_faultsim_test multiplex-5.5 -prep {
562 catch { sqlite3_multiplex_shutdown }
563} -body {
564 sqlite3_multiplex_initialize "" 1
shanehd50deee2011-03-29 05:06:46 +0000565 multiplex_set db main 32768 16
shaneh8a922f72010-11-04 20:50:27 +0000566}
567
shanehcc4e19b2011-05-18 02:22:41 +0000568#-------------------------------------------------------------------------
569# Test that you can vacuum a multiplex'ed DB.
570
571ifcapable vacuum {
572
drhbabb61f2011-06-15 23:34:51 +0000573sqlite3_multiplex_shutdown
shanehcc4e19b2011-05-18 02:22:41 +0000574do_test multiplex-6.0.0 {
575 multiplex_delete test.db
drhf3717af2011-07-20 16:35:31 +0000576 multiplex_delete test.x
shanehcc4e19b2011-05-18 02:22:41 +0000577 sqlite3_multiplex_initialize "" 1
drhf3717af2011-07-20 16:35:31 +0000578 sqlite3 db test.x
shanehcc4e19b2011-05-18 02:22:41 +0000579 multiplex_set db main 4096 16
580} {SQLITE_OK}
581
582do_test multiplex-6.1.0 {
583 execsql {
584 PRAGMA page_size=1024;
585 PRAGMA journal_mode=DELETE;
586 PRAGMA auto_vacuum=OFF;
587 }
588 execsql {
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));
592 }
593} {}
drhf3717af2011-07-20 16:35:31 +0000594do_test multiplex-6.2.1 { file size [multiplex_name test.x 0] } [list $g_chunk_size]
595do_test multiplex-6.2.2 { file size [multiplex_name test.x 1] } [list $g_chunk_size]
shanehcc4e19b2011-05-18 02:22:41 +0000596
597do_test multiplex-6.3.0 {
598 execsql { VACUUM }
599} {}
600
601do_test multiplex-6.99 {
602 db close
drhf3717af2011-07-20 16:35:31 +0000603 multiplex_delete test.x
shanehcc4e19b2011-05-18 02:22:41 +0000604 sqlite3_multiplex_shutdown
605} {SQLITE_OK}
606
607}
608
609
danf6296ca2014-07-31 18:14:37 +0000610catch { db close }
shaneh8a922f72010-11-04 20:50:27 +0000611catch { sqlite3_multiplex_shutdown }
danf6296ca2014-07-31 18:14:37 +0000612sqlite3_shutdown
613test_sqlite3_log
614sqlite3_initialize
shaneh8a922f72010-11-04 20:50:27 +0000615finish_test