blob: 32c87d9a52ac92915d2962ce4e6904500c8c661a [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
71
shanehf5913a22010-12-01 22:08:45 +000072multiplex_delete test.db
73multiplex_delete test2.db
74
shanehfd1552f2010-11-05 03:43:54 +000075#-------------------------------------------------------------------------
76# multiplex-1.1.*: Test initialize and shutdown.
77
shaneh8a922f72010-11-04 20:50:27 +000078do_test multiplex-1.1 { sqlite3_multiplex_initialize nosuchvfs 1 } {SQLITE_ERROR}
79do_test multiplex-1.2 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
80do_test multiplex-1.3 { sqlite3_multiplex_initialize "" 1 } {SQLITE_MISUSE}
81do_test multiplex-1.4 { sqlite3_multiplex_shutdown } {SQLITE_OK}
82
83do_test multiplex-1.5 { sqlite3_multiplex_initialize "" 0 } {SQLITE_OK}
84do_test multiplex-1.6 { sqlite3_multiplex_shutdown } {SQLITE_OK}
85do_test multiplex-1.7 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
86do_test multiplex-1.8 { sqlite3_multiplex_shutdown } {SQLITE_OK}
87
shaneh78c4de42011-03-31 05:31:24 +000088
shanehc27fa4b2011-03-31 15:11:53 +000089do_test multiplex-1.9.1 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
90do_test multiplex-1.9.2 { sqlite3 db test.db } {}
91do_test multiplex-1.9.3 { multiplex_set db main 32768 16 } {SQLITE_OK}
drhf3717af2011-07-20 16:35:31 +000092do_test multiplex-1.9.4 { multiplex_set db main 32768 -1 } {SQLITE_OK}
shanehc27fa4b2011-03-31 15:11:53 +000093do_test multiplex-1.9.6 { multiplex_set db main 31 16 } {SQLITE_OK}
drhf3717af2011-07-20 16:35:31 +000094do_test multiplex-1.9.7 { multiplex_set db main 32768 100 } {SQLITE_OK}
shanehc27fa4b2011-03-31 15:11:53 +000095do_test multiplex-1.9.8 { multiplex_set db main 1073741824 1 } {SQLITE_OK}
96do_test multiplex-1.9.9 { db close } {}
97do_test multiplex-1.9.10 { sqlite3_multiplex_shutdown } {SQLITE_OK}
98
shaneh3801b652011-04-01 14:22:46 +000099do_test multiplex-1.10.1 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
100do_test multiplex-1.10.2 { sqlite3 db test.db } {}
101do_test multiplex-1.10.3 { lindex [ catchsql { SELECT multiplex_control(2, 32768); } ] 0 } {0}
drhf3717af2011-07-20 16:35:31 +0000102do_test multiplex-1.10.4 { lindex [ catchsql { SELECT multiplex_control(3, -1); } ] 0 } {0}
shaneh3801b652011-04-01 14:22:46 +0000103do_test multiplex-1.10.6 { lindex [ catchsql { SELECT multiplex_control(2, 31); } ] 0 } {0}
drhf3717af2011-07-20 16:35:31 +0000104do_test multiplex-1.10.7 { lindex [ catchsql { SELECT multiplex_control(3, 100); } ] 0 } {0}
shaneh3801b652011-04-01 14:22:46 +0000105do_test multiplex-1.10.8 { lindex [ catchsql { SELECT multiplex_control(2, 1073741824); } ] 0 } {0}
106do_test multiplex-1.10.9 { db close } {}
107do_test multiplex-1.10.10 { sqlite3_multiplex_shutdown } {SQLITE_OK}
shanehc27fa4b2011-03-31 15:11:53 +0000108
shaneh3801b652011-04-01 14:22:46 +0000109do_test multiplex-1.11.1 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
110do_test multiplex-1.11.2 { sqlite3 db test.db } {}
111do_test multiplex-1.11.3 { sqlite3_multiplex_control db main enable 0 } {SQLITE_OK}
112do_test multiplex-1.11.4 { sqlite3_multiplex_control db main enable 1 } {SQLITE_OK}
113do_test multiplex-1.11.5 { sqlite3_multiplex_control db main enable -1 } {SQLITE_OK}
114do_test multiplex-1.11.6 { db close } {}
115do_test multiplex-1.11.7 { sqlite3_multiplex_shutdown } {SQLITE_OK}
shanehc27fa4b2011-03-31 15:11:53 +0000116
shaneh3801b652011-04-01 14:22:46 +0000117do_test multiplex-1.12.1 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
118do_test multiplex-1.12.2 { sqlite3 db test.db } {}
119do_test multiplex-1.12.3 { lindex [ catchsql { SELECT multiplex_control(1, 0); } ] 0 } {0}
120do_test multiplex-1.12.4 { lindex [ catchsql { SELECT multiplex_control(1, 1); } ] 0 } {0}
121do_test multiplex-1.12.5 { lindex [ catchsql { SELECT multiplex_control(1, -1); } ] 0 } {0}
122do_test multiplex-1.12.6 { db close } {}
123do_test multiplex-1.12.7 { sqlite3_multiplex_shutdown } {SQLITE_OK}
124
125do_test multiplex-1.13.1 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
126do_test multiplex-1.13.2 { sqlite3 db test.db } {}
127do_test multiplex-1.13.3 { lindex [ catchsql { SELECT multiplex_control(-1, 0); } ] 0 } {1}
128do_test multiplex-1.13.4 { lindex [ catchsql { SELECT multiplex_control(4, 1); } ] 0 } {1}
129do_test multiplex-1.13.6 { db close } {}
130do_test multiplex-1.13.7 { sqlite3_multiplex_shutdown } {SQLITE_OK}
shanehe5a6ad62010-11-05 03:58:58 +0000131
shaneh8a922f72010-11-04 20:50:27 +0000132#-------------------------------------------------------------------------
133# Some simple warm-body tests with a single database file in rollback
134# mode:
135#
136# multiplex-2.1.*: Test simple writing to a multiplex file.
137#
138# multiplex-2.2.*: More writing.
139#
140# multiplex-2.3.*: Open and close a second db.
141#
shanehc27fa4b2011-03-31 15:11:53 +0000142# multiplex-2.4.*: Try to shutdown the multiplex system before closing the db
shaneh8a922f72010-11-04 20:50:27 +0000143# file. Check that this fails and the multiplex system still works
144# afterwards. Then close the database and successfully shut
145# down the multiplex system.
146#
shanehb5830292010-11-05 17:51:25 +0000147# multiplex-2.5.*: More reading/writing.
148#
shaneh0596bee2010-11-05 20:50:43 +0000149# multiplex-2.6.*: More reading/writing with varying small chunk sizes, as
150# well as varying journal mode.
shanehc27fa4b2011-03-31 15:11:53 +0000151#
152# multiplex-2.7.*: Disable/enable tests.
153#
shaneh8a922f72010-11-04 20:50:27 +0000154
155sqlite3_multiplex_initialize "" 1
shanehd50deee2011-03-29 05:06:46 +0000156multiplex_set db main 32768 16
shaneh8a922f72010-11-04 20:50:27 +0000157
mistachkinfda06be2011-08-02 00:57:34 +0000158forcedelete test.x
drhec0c7652012-01-09 13:41:59 +0000159foreach f [glob -nocomplain {test.x*[0-9][0-9][0-9]}] {
160 forcedelete $f
161}
shaneh8a922f72010-11-04 20:50:27 +0000162do_test multiplex-2.1.2 {
drhf3717af2011-07-20 16:35:31 +0000163 sqlite3 db test.x
shaneh8a922f72010-11-04 20:50:27 +0000164 execsql {
165 PRAGMA page_size=1024;
166 PRAGMA auto_vacuum=OFF;
167 PRAGMA journal_mode=DELETE;
168 }
169 execsql {
170 CREATE TABLE t1(a, b);
171 INSERT INTO t1 VALUES(1, randomblob(1100));
172 INSERT INTO t1 VALUES(2, randomblob(1100));
173 }
174} {}
drhf3717af2011-07-20 16:35:31 +0000175do_test multiplex-2.1.3 { file size [multiplex_name test.x 0] } {4096}
shaneh8a922f72010-11-04 20:50:27 +0000176do_test multiplex-2.1.4 {
177 execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
178} {}
179
180do_test multiplex-2.2.1 {
181 execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
182} {}
drhf3717af2011-07-20 16:35:31 +0000183do_test multiplex-2.2.3 { file size [multiplex_name test.x 0] } {6144}
shaneh8a922f72010-11-04 20:50:27 +0000184
185do_test multiplex-2.3.1 {
drhf3717af2011-07-20 16:35:31 +0000186 sqlite3 db2 test2.x
shaneh8a922f72010-11-04 20:50:27 +0000187 db2 close
188} {}
189
shanehd50deee2011-03-29 05:06:46 +0000190
shaneh8a922f72010-11-04 20:50:27 +0000191do_test multiplex-2.4.1 {
192 sqlite3_multiplex_shutdown
193} {SQLITE_MISUSE}
194do_test multiplex-2.4.2 {
195 execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
196} {}
drhf3717af2011-07-20 16:35:31 +0000197do_test multiplex-2.4.4 { file size [multiplex_name test.x 0] } {7168}
drhec0c7652012-01-09 13:41:59 +0000198do_test multiplex-2.4.5 {
shaneh8a922f72010-11-04 20:50:27 +0000199 db close
drhec0c7652012-01-09 13:41:59 +0000200 sqlite3 db test.x
201 db eval vacuum
202 db close
203 glob test.x*
204} {test.x}
205do_test multiplex-2.4.99 {
shaneh8a922f72010-11-04 20:50:27 +0000206 sqlite3_multiplex_shutdown
207} {SQLITE_OK}
208
shanehfd1552f2010-11-05 03:43:54 +0000209do_test multiplex-2.5.1 {
drhf3717af2011-07-20 16:35:31 +0000210 multiplex_delete test.x
shanehfd1552f2010-11-05 03:43:54 +0000211 sqlite3_multiplex_initialize "" 1
drhf3717af2011-07-20 16:35:31 +0000212 sqlite3 db test.x
shanehd50deee2011-03-29 05:06:46 +0000213 multiplex_set db main 4096 16
shanehfd1552f2010-11-05 03:43:54 +0000214} {SQLITE_OK}
215
216do_test multiplex-2.5.2 {
shanehfd1552f2010-11-05 03:43:54 +0000217 execsql {
218 PRAGMA page_size = 1024;
219 PRAGMA journal_mode = delete;
220 PRAGMA auto_vacuum = off;
221 CREATE TABLE t1(a PRIMARY KEY, b);
222 }
223} {delete}
224
225do_test multiplex-2.5.3 {
226 execsql {
227 INSERT INTO t1 VALUES(1, 'one');
228 INSERT INTO t1 VALUES(2, randomblob(4000));
229 INSERT INTO t1 VALUES(3, 'three');
230 INSERT INTO t1 VALUES(4, randomblob(4000));
shaneh78c4de42011-03-31 05:31:24 +0000231 INSERT INTO t1 VALUES(5, 'five');
232 INSERT INTO t1 VALUES(6, randomblob($g_chunk_size));
233 INSERT INTO t1 VALUES(7, randomblob($g_chunk_size));
shanehfd1552f2010-11-05 03:43:54 +0000234 }
235} {}
236
237do_test multiplex-2.5.4 {
238 db eval {SELECT * FROM t1 WHERE a=1}
239} {1 one}
240
241do_test multiplex-2.5.5 {
242 db eval {SELECT * FROM t1 WHERE a=3}
243} {3 three}
244
245do_test multiplex-2.5.6 {
246 db eval {SELECT * FROM t1 WHERE a=5}
247} {5 five}
248
249do_test multiplex-2.5.7 {
250 db eval {SELECT a,length(b) FROM t1 WHERE a=2}
251} {2 4000}
252
253do_test multiplex-2.5.8 {
254 db eval {SELECT a,length(b) FROM t1 WHERE a=4}
255} {4 4000}
256
drhf3717af2011-07-20 16:35:31 +0000257do_test multiplex-2.5.9 { file size [multiplex_name test.x 0] } [list $g_chunk_size]
258do_test multiplex-2.5.10 { file size [multiplex_name test.x 1] } [list $g_chunk_size]
shanehb5830292010-11-05 17:51:25 +0000259
shanehfd1552f2010-11-05 03:43:54 +0000260do_test multiplex-2.5.99 {
261 db close
262 sqlite3_multiplex_shutdown
263} {SQLITE_OK}
264
shanehb5830292010-11-05 17:51:25 +0000265
shaneh0596bee2010-11-05 20:50:43 +0000266set all_journal_modes {delete persist truncate memory off}
267foreach jmode $all_journal_modes {
268 for {set sz 151} {$sz<8000} {set sz [expr $sz+419]} {
shanehb5830292010-11-05 17:51:25 +0000269
shaneh0596bee2010-11-05 20:50:43 +0000270 do_test multiplex-2.6.1.$sz.$jmode {
271 multiplex_delete test.db
272 sqlite3_multiplex_initialize "" 1
shanehac039682011-03-30 21:03:07 +0000273 sqlite3 db test.db
shanehd50deee2011-03-29 05:06:46 +0000274 multiplex_set db main $sz 32
shaneh0596bee2010-11-05 20:50:43 +0000275 } {SQLITE_OK}
shanehb5830292010-11-05 17:51:25 +0000276
shaneh0596bee2010-11-05 20:50:43 +0000277 do_test multiplex-2.6.2.$sz.$jmode {
shaneh0596bee2010-11-05 20:50:43 +0000278 db eval {
279 PRAGMA page_size = 1024;
280 PRAGMA auto_vacuum = off;
281 }
282 db eval "PRAGMA journal_mode = $jmode;"
283 } $jmode
shanehb5830292010-11-05 17:51:25 +0000284
shaneh0596bee2010-11-05 20:50:43 +0000285 do_test multiplex-2.6.3.$sz.$jmode {
286 execsql {
287 CREATE TABLE t1(a PRIMARY KEY, b);
288 INSERT INTO t1 VALUES(1, 'one');
289 INSERT INTO t1 VALUES(2, randomblob($g_chunk_size));
290 }
291 } {}
shanehb5830292010-11-05 17:51:25 +0000292
shaneh0596bee2010-11-05 20:50:43 +0000293 do_test multiplex-2.6.4.$sz.$jmode {
294 db eval {SELECT b FROM t1 WHERE a=1}
295 } {one}
shanehb5830292010-11-05 17:51:25 +0000296
shaneh0596bee2010-11-05 20:50:43 +0000297 do_test multiplex-2.6.5.$sz.$jmode {
298 db eval {SELECT length(b) FROM t1 WHERE a=2}
299 } [list $g_chunk_size]
shanehb5830292010-11-05 17:51:25 +0000300
shaneh050d09a2010-11-08 19:16:16 +0000301 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 +0000302
shaneh0596bee2010-11-05 20:50:43 +0000303 do_test multiplex-2.6.99.$sz.$jmode {
304 db close
305 sqlite3_multiplex_shutdown
306 } {SQLITE_OK}
shanehb5830292010-11-05 17:51:25 +0000307
shaneh0596bee2010-11-05 20:50:43 +0000308 }
shanehb5830292010-11-05 17:51:25 +0000309}
310
shaneh3801b652011-04-01 14:22:46 +0000311do_test multiplex-2.7.1 { multiplex_delete test.db } {}
312do_test multiplex-2.7.2 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
313do_test multiplex-2.7.3 { sqlite3 db test.db } {}
314do_test multiplex-2.7.4 { lindex [ catchsql { SELECT multiplex_control(2, 65536); } ] 0 } {0}
315do_test multiplex-2.7.5 { lindex [ catchsql { SELECT multiplex_control(1, 0); } ] 0 } {0}
shanehc27fa4b2011-03-31 15:11:53 +0000316do_test multiplex-2.7.6 {
317 execsql {
318 CREATE TABLE t1(a PRIMARY KEY, b);
319 INSERT INTO t1 VALUES(1, randomblob(1000));
320 }
321} {}
322# verify only one file, and file size is less than chunks size
323do_test multiplex-2.7.7 { expr ([file size [multiplex_name test.db 0]] < 65536) } {1}
324do_test multiplex-2.7.8 { file exists [multiplex_name test.db 1] } {0}
325do_test multiplex-2.7.9 {
326 execsql {
shaneh3801b652011-04-01 14:22:46 +0000327 INSERT INTO t1 VALUES(2, randomblob(65536));
shanehc27fa4b2011-03-31 15:11:53 +0000328 }
329} {}
330# verify only one file, and file size exceeds chunks size
331do_test multiplex-2.7.10 { expr ([file size [multiplex_name test.db 0]] > 65536) } {1}
332do_test multiplex-2.7.11 { file exists [multiplex_name test.db 1] } {0}
333do_test multiplex-2.7.12 { db close } {}
334do_test multiplex-2.7.13 { sqlite3_multiplex_shutdown } {SQLITE_OK}
335
shaneh8a922f72010-11-04 20:50:27 +0000336#-------------------------------------------------------------------------
337# Try some tests with more than one connection to a database file. Still
338# in rollback mode.
339#
340# multiplex-3.1.*: Two connections to a single database file.
341#
342# multiplex-3.2.*: Two connections to each of several database files (that
343# are in the same multiplex group).
344#
345do_test multiplex-3.1.1 {
346 multiplex_delete test.db
347 sqlite3_multiplex_initialize "" 1
shanehac039682011-03-30 21:03:07 +0000348 sqlite3 db test.db
shanehd50deee2011-03-29 05:06:46 +0000349 multiplex_set db main 32768 16
shaneh8a922f72010-11-04 20:50:27 +0000350} {SQLITE_OK}
351do_test multiplex-3.1.2 {
shaneh8a922f72010-11-04 20:50:27 +0000352 execsql {
353 PRAGMA page_size = 1024;
354 PRAGMA journal_mode = delete;
355 PRAGMA auto_vacuum = off;
356 CREATE TABLE t1(a PRIMARY KEY, b);
357 INSERT INTO t1 VALUES(1, 'one');
358 }
shaneh050d09a2010-11-08 19:16:16 +0000359 file size [multiplex_name test.db 0]
shaneh8a922f72010-11-04 20:50:27 +0000360} {3072}
361do_test multiplex-3.1.3 {
362 sqlite3 db2 test.db
363 execsql { CREATE TABLE t2(a, b) } db2
364} {}
365do_test multiplex-3.1.4 {
366 execsql { CREATE TABLE t3(a, b) }
367} {}
368do_test multiplex-3.1.5 {
369 catchsql { CREATE TABLE t3(a, b) }
370} {1 {table t3 already exists}}
371do_test multiplex-3.1.6 {
372 db close
373 db2 close
374} {}
375
376do_test multiplex-3.2.1a {
377
378 multiplex_delete test.db
379 multiplex_delete test2.db
380
381 sqlite3 db1a test.db
382 sqlite3 db2a test2.db
383
384 foreach db {db1a db2a} {
385 execsql {
386 PRAGMA page_size = 1024;
387 PRAGMA journal_mode = delete;
388 PRAGMA auto_vacuum = off;
389 CREATE TABLE t1(a, b);
390 } $db
391 }
392
shaneh050d09a2010-11-08 19:16:16 +0000393 list [file size [multiplex_name test.db 0]] [file size [multiplex_name test2.db 0]]
shaneh8a922f72010-11-04 20:50:27 +0000394} {2048 2048}
395
396do_test multiplex-3.2.1b {
397 sqlite3 db1b test.db
398 sqlite3 db2b test2.db
399} {}
400
401do_test multiplex-3.2.2 { execsql { INSERT INTO t1 VALUES('x', 'y') } db1a } {}
402do_test multiplex-3.2.3 { execsql { INSERT INTO t1 VALUES('v', 'w') } db1b } {}
403do_test multiplex-3.2.4 { execsql { INSERT INTO t1 VALUES('t', 'u') } db2a } {}
404do_test multiplex-3.2.5 { execsql { INSERT INTO t1 VALUES('r', 's') } db2b } {}
405
406do_test multiplex-3.2.6 {
407 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1a
408} {}
409do_test multiplex-3.2.7 {
410 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1b
411} {}
412do_test multiplex-3.2.8 {
413 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2a
414} {}
415do_test multiplex-3.2.9 {
416 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2b
417} {}
418
419do_test multiplex-3.3.1 {
420 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1a
421 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1b
422 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2a
423 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2b
424} {}
425
426do_test multiplex-3.2.X {
427 foreach db {db1a db2a db2b db1b} { catch { $db close } }
428} {}
429
430#-------------------------------------------------------------------------
431#
432
433sqlite3_multiplex_initialize "" 1
shanehd50deee2011-03-29 05:06:46 +0000434multiplex_set db main 32768 16
shaneh8a922f72010-11-04 20:50:27 +0000435
436# Return a list of all currently defined multiplexs.
437proc multiplex_list {} {
438 set allq {}
439 foreach q [sqlite3_multiplex_dump] {
440 lappend allq [lindex $q 0]
441 }
442 return [lsort $allq]
443}
444
445do_test multiplex-4.1.6 {
446 multiplex_delete test2.db
447 sqlite3 db test2.db
448 db eval {CREATE TABLE t2(x); INSERT INTO t2 VALUES('tab-t2');}
449 set res [multiplex_list]
450 list [regexp {test2.db} $res]
451} {1}
452do_test multiplex-4.1.6a {
453 sqlite3 db2 test2.db
454 db2 eval {SELECT * FROM t2}
455} {tab-t2}
456do_test multiplex-4.1.7 {
457 execsql {INSERT INTO t2 VALUES(zeroblob(200000))}
458} {}
459do_test multiplex-4.1.8 {
460 sqlite3 db2 test2.db
461 db2 eval {SELECT count(*) FROM t2}
462} {2}
463do_test multiplex-4.1.8a {
464 db2 eval { DELETE FROM t2 WHERE x = 'tab-t2' }
465} {}
466do_test multiplex-4.1.8b {
467 sqlite3 db2 test2.db
468 db2 eval {SELECT count(*) FROM t2}
469} {1}
470
471
472do_test multiplex-4.1.9 {
473 execsql {INSERT INTO t2 VALUES(zeroblob(200000))}
474} {}
475do_test multiplex-4.1.10 {
476 set res [multiplex_list]
477 list [regexp {test2.db} $res]
478} {1}
479do_test multiplex-4.1.11 {
480 db2 close
481 set res [multiplex_list]
482 list [regexp {test2.db} $res]
483} {1}
484do_test multiplex-4.1.12 {
485 db close
486 multiplex_list
487} {}
488
489
490#-------------------------------------------------------------------------
491# The following tests test that the multiplex VFS handles malloc and IO
492# errors.
493#
494
495sqlite3_multiplex_initialize "" 1
shanehd50deee2011-03-29 05:06:46 +0000496multiplex_set db main 32768 16
shaneh8a922f72010-11-04 20:50:27 +0000497
498do_faultsim_test multiplex-5.1 -prep {
499 catch {db close}
500} -body {
501 sqlite3 db test2.db
502}
503do_faultsim_test multiplex-5.2 -prep {
504 catch {db close}
505} -body {
506 sqlite3 db test.db
507}
508
509catch { db close }
510multiplex_delete test.db
shanehb5830292010-11-05 17:51:25 +0000511multiplex_delete test2.db
shaneh8a922f72010-11-04 20:50:27 +0000512
513do_test multiplex-5.3.prep {
514 sqlite3 db test.db
515 execsql {
516 PRAGMA auto_vacuum = 1;
517 PRAGMA page_size = 1024;
518 CREATE TABLE t1(a, b);
519 INSERT INTO t1 VALUES(10, zeroblob(1200));
520 }
521 faultsim_save_and_close
522} {}
523do_faultsim_test multiplex-5.3 -prep {
524 faultsim_restore_and_reopen
525} -body {
526 execsql { DELETE FROM t1 }
527}
528
529do_test multiplex-5.4.1 {
530 catch { db close }
531 multiplex_delete test.db
532 file mkdir test.db
533 list [catch { sqlite3 db test.db } msg] $msg
534} {1 {unable to open database file}}
mistachkinfda06be2011-08-02 00:57:34 +0000535catch { delete_file test.db }
shaneh8a922f72010-11-04 20:50:27 +0000536
537do_faultsim_test multiplex-5.5 -prep {
538 catch { sqlite3_multiplex_shutdown }
539} -body {
540 sqlite3_multiplex_initialize "" 1
shanehd50deee2011-03-29 05:06:46 +0000541 multiplex_set db main 32768 16
shaneh8a922f72010-11-04 20:50:27 +0000542}
543
shanehcc4e19b2011-05-18 02:22:41 +0000544#-------------------------------------------------------------------------
545# Test that you can vacuum a multiplex'ed DB.
546
547ifcapable vacuum {
548
drhbabb61f2011-06-15 23:34:51 +0000549sqlite3_multiplex_shutdown
shanehcc4e19b2011-05-18 02:22:41 +0000550do_test multiplex-6.0.0 {
551 multiplex_delete test.db
drhf3717af2011-07-20 16:35:31 +0000552 multiplex_delete test.x
shanehcc4e19b2011-05-18 02:22:41 +0000553 sqlite3_multiplex_initialize "" 1
drhf3717af2011-07-20 16:35:31 +0000554 sqlite3 db test.x
shanehcc4e19b2011-05-18 02:22:41 +0000555 multiplex_set db main 4096 16
556} {SQLITE_OK}
557
558do_test multiplex-6.1.0 {
559 execsql {
560 PRAGMA page_size=1024;
561 PRAGMA journal_mode=DELETE;
562 PRAGMA auto_vacuum=OFF;
563 }
564 execsql {
565 CREATE TABLE t1(a, b);
566 INSERT INTO t1 VALUES(1, randomblob($g_chunk_size));
567 INSERT INTO t1 VALUES(2, randomblob($g_chunk_size));
568 }
569} {}
drhf3717af2011-07-20 16:35:31 +0000570do_test multiplex-6.2.1 { file size [multiplex_name test.x 0] } [list $g_chunk_size]
571do_test multiplex-6.2.2 { file size [multiplex_name test.x 1] } [list $g_chunk_size]
shanehcc4e19b2011-05-18 02:22:41 +0000572
573do_test multiplex-6.3.0 {
574 execsql { VACUUM }
575} {}
576
577do_test multiplex-6.99 {
578 db close
drhf3717af2011-07-20 16:35:31 +0000579 multiplex_delete test.x
shanehcc4e19b2011-05-18 02:22:41 +0000580 sqlite3_multiplex_shutdown
581} {SQLITE_OK}
582
583}
584
585
shaneh8a922f72010-11-04 20:50:27 +0000586catch { sqlite3_multiplex_shutdown }
587finish_test