blob: 6ea328906ee275faf87a171441b8423b1f351084 [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} {}
185
186do_test multiplex-2.2.1 {
187 execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
188} {}
drhf3717af2011-07-20 16:35:31 +0000189do_test multiplex-2.2.3 { file size [multiplex_name test.x 0] } {6144}
shaneh8a922f72010-11-04 20:50:27 +0000190
191do_test multiplex-2.3.1 {
drhf3717af2011-07-20 16:35:31 +0000192 sqlite3 db2 test2.x
shaneh8a922f72010-11-04 20:50:27 +0000193 db2 close
194} {}
195
shanehd50deee2011-03-29 05:06:46 +0000196
drha1a82982014-07-30 15:43:05 +0000197unset -nocomplain ::log
dandadafa82016-10-27 14:51:02 +0000198#do_test multiplex-2.4.1 {
199# sqlite3_multiplex_shutdown
200#} {SQLITE_MISUSE}
shaneh8a922f72010-11-04 20:50:27 +0000201do_test multiplex-2.4.2 {
202 execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
203} {}
dandadafa82016-10-27 14:51:02 +0000204#do_test multiplex-2.4.3 {
205# set ::log
206#} {SQLITE_MISUSE {sqlite3_multiplex_shutdown() called while database connections are still open}}
207
drhf3717af2011-07-20 16:35:31 +0000208do_test multiplex-2.4.4 { file size [multiplex_name test.x 0] } {7168}
drhec0c7652012-01-09 13:41:59 +0000209do_test multiplex-2.4.5 {
shaneh8a922f72010-11-04 20:50:27 +0000210 db close
drhec0c7652012-01-09 13:41:59 +0000211 sqlite3 db test.x
212 db eval vacuum
213 db close
214 glob test.x*
215} {test.x}
216do_test multiplex-2.4.99 {
shaneh8a922f72010-11-04 20:50:27 +0000217 sqlite3_multiplex_shutdown
218} {SQLITE_OK}
219
shanehfd1552f2010-11-05 03:43:54 +0000220do_test multiplex-2.5.1 {
drhf3717af2011-07-20 16:35:31 +0000221 multiplex_delete test.x
shanehfd1552f2010-11-05 03:43:54 +0000222 sqlite3_multiplex_initialize "" 1
drhf3717af2011-07-20 16:35:31 +0000223 sqlite3 db test.x
shanehd50deee2011-03-29 05:06:46 +0000224 multiplex_set db main 4096 16
shanehfd1552f2010-11-05 03:43:54 +0000225} {SQLITE_OK}
226
227do_test multiplex-2.5.2 {
shanehfd1552f2010-11-05 03:43:54 +0000228 execsql {
229 PRAGMA page_size = 1024;
230 PRAGMA journal_mode = delete;
231 PRAGMA auto_vacuum = off;
232 CREATE TABLE t1(a PRIMARY KEY, b);
233 }
234} {delete}
235
236do_test multiplex-2.5.3 {
237 execsql {
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));
shaneh78c4de42011-03-31 05:31:24 +0000242 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));
shanehfd1552f2010-11-05 03:43:54 +0000245 }
246} {}
247
248do_test multiplex-2.5.4 {
249 db eval {SELECT * FROM t1 WHERE a=1}
250} {1 one}
251
252do_test multiplex-2.5.5 {
253 db eval {SELECT * FROM t1 WHERE a=3}
254} {3 three}
255
256do_test multiplex-2.5.6 {
257 db eval {SELECT * FROM t1 WHERE a=5}
258} {5 five}
259
260do_test multiplex-2.5.7 {
261 db eval {SELECT a,length(b) FROM t1 WHERE a=2}
262} {2 4000}
263
264do_test multiplex-2.5.8 {
265 db eval {SELECT a,length(b) FROM t1 WHERE a=4}
266} {4 4000}
267
drhf3717af2011-07-20 16:35:31 +0000268do_test multiplex-2.5.9 { file size [multiplex_name test.x 0] } [list $g_chunk_size]
269do_test multiplex-2.5.10 { file size [multiplex_name test.x 1] } [list $g_chunk_size]
shanehb5830292010-11-05 17:51:25 +0000270
shanehfd1552f2010-11-05 03:43:54 +0000271do_test multiplex-2.5.99 {
272 db close
273 sqlite3_multiplex_shutdown
274} {SQLITE_OK}
275
shanehb5830292010-11-05 17:51:25 +0000276
shaneh0596bee2010-11-05 20:50:43 +0000277set all_journal_modes {delete persist truncate memory off}
278foreach jmode $all_journal_modes {
279 for {set sz 151} {$sz<8000} {set sz [expr $sz+419]} {
shanehb5830292010-11-05 17:51:25 +0000280
shaneh0596bee2010-11-05 20:50:43 +0000281 do_test multiplex-2.6.1.$sz.$jmode {
282 multiplex_delete test.db
283 sqlite3_multiplex_initialize "" 1
shanehac039682011-03-30 21:03:07 +0000284 sqlite3 db test.db
shanehd50deee2011-03-29 05:06:46 +0000285 multiplex_set db main $sz 32
shaneh0596bee2010-11-05 20:50:43 +0000286 } {SQLITE_OK}
shanehb5830292010-11-05 17:51:25 +0000287
shaneh0596bee2010-11-05 20:50:43 +0000288 do_test multiplex-2.6.2.$sz.$jmode {
shaneh0596bee2010-11-05 20:50:43 +0000289 db eval {
290 PRAGMA page_size = 1024;
291 PRAGMA auto_vacuum = off;
292 }
293 db eval "PRAGMA journal_mode = $jmode;"
294 } $jmode
shanehb5830292010-11-05 17:51:25 +0000295
shaneh0596bee2010-11-05 20:50:43 +0000296 do_test multiplex-2.6.3.$sz.$jmode {
297 execsql {
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));
301 }
302 } {}
shanehb5830292010-11-05 17:51:25 +0000303
shaneh0596bee2010-11-05 20:50:43 +0000304 do_test multiplex-2.6.4.$sz.$jmode {
305 db eval {SELECT b FROM t1 WHERE a=1}
306 } {one}
shanehb5830292010-11-05 17:51:25 +0000307
shaneh0596bee2010-11-05 20:50:43 +0000308 do_test multiplex-2.6.5.$sz.$jmode {
309 db eval {SELECT length(b) FROM t1 WHERE a=2}
310 } [list $g_chunk_size]
shanehb5830292010-11-05 17:51:25 +0000311
shaneh050d09a2010-11-08 19:16:16 +0000312 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 +0000313
shaneh0596bee2010-11-05 20:50:43 +0000314 do_test multiplex-2.6.99.$sz.$jmode {
315 db close
316 sqlite3_multiplex_shutdown
317 } {SQLITE_OK}
shanehb5830292010-11-05 17:51:25 +0000318
shaneh0596bee2010-11-05 20:50:43 +0000319 }
shanehb5830292010-11-05 17:51:25 +0000320}
321
shaneh3801b652011-04-01 14:22:46 +0000322do_test multiplex-2.7.1 { multiplex_delete test.db } {}
323do_test multiplex-2.7.2 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
324do_test multiplex-2.7.3 { sqlite3 db test.db } {}
325do_test multiplex-2.7.4 { lindex [ catchsql { SELECT multiplex_control(2, 65536); } ] 0 } {0}
326do_test multiplex-2.7.5 { lindex [ catchsql { SELECT multiplex_control(1, 0); } ] 0 } {0}
shanehc27fa4b2011-03-31 15:11:53 +0000327do_test multiplex-2.7.6 {
328 execsql {
329 CREATE TABLE t1(a PRIMARY KEY, b);
330 INSERT INTO t1 VALUES(1, randomblob(1000));
331 }
332} {}
333# verify only one file, and file size is less than chunks size
334do_test multiplex-2.7.7 { expr ([file size [multiplex_name test.db 0]] < 65536) } {1}
335do_test multiplex-2.7.8 { file exists [multiplex_name test.db 1] } {0}
336do_test multiplex-2.7.9 {
337 execsql {
shaneh3801b652011-04-01 14:22:46 +0000338 INSERT INTO t1 VALUES(2, randomblob(65536));
shanehc27fa4b2011-03-31 15:11:53 +0000339 }
340} {}
341# verify only one file, and file size exceeds chunks size
342do_test multiplex-2.7.10 { expr ([file size [multiplex_name test.db 0]] > 65536) } {1}
343do_test multiplex-2.7.11 { file exists [multiplex_name test.db 1] } {0}
344do_test multiplex-2.7.12 { db close } {}
345do_test multiplex-2.7.13 { sqlite3_multiplex_shutdown } {SQLITE_OK}
346
shaneh8a922f72010-11-04 20:50:27 +0000347#-------------------------------------------------------------------------
348# Try some tests with more than one connection to a database file. Still
349# in rollback mode.
350#
351# multiplex-3.1.*: Two connections to a single database file.
352#
353# multiplex-3.2.*: Two connections to each of several database files (that
354# are in the same multiplex group).
355#
356do_test multiplex-3.1.1 {
357 multiplex_delete test.db
358 sqlite3_multiplex_initialize "" 1
shanehac039682011-03-30 21:03:07 +0000359 sqlite3 db test.db
shanehd50deee2011-03-29 05:06:46 +0000360 multiplex_set db main 32768 16
shaneh8a922f72010-11-04 20:50:27 +0000361} {SQLITE_OK}
362do_test multiplex-3.1.2 {
shaneh8a922f72010-11-04 20:50:27 +0000363 execsql {
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');
369 }
shaneh050d09a2010-11-08 19:16:16 +0000370 file size [multiplex_name test.db 0]
shaneh8a922f72010-11-04 20:50:27 +0000371} {3072}
372do_test multiplex-3.1.3 {
373 sqlite3 db2 test.db
374 execsql { CREATE TABLE t2(a, b) } db2
375} {}
376do_test multiplex-3.1.4 {
377 execsql { CREATE TABLE t3(a, b) }
378} {}
379do_test multiplex-3.1.5 {
380 catchsql { CREATE TABLE t3(a, b) }
381} {1 {table t3 already exists}}
382do_test multiplex-3.1.6 {
383 db close
384 db2 close
385} {}
386
387do_test multiplex-3.2.1a {
388
389 multiplex_delete test.db
390 multiplex_delete test2.db
391
392 sqlite3 db1a test.db
393 sqlite3 db2a test2.db
394
395 foreach db {db1a db2a} {
396 execsql {
397 PRAGMA page_size = 1024;
398 PRAGMA journal_mode = delete;
399 PRAGMA auto_vacuum = off;
400 CREATE TABLE t1(a, b);
401 } $db
402 }
403
shaneh050d09a2010-11-08 19:16:16 +0000404 list [file size [multiplex_name test.db 0]] [file size [multiplex_name test2.db 0]]
shaneh8a922f72010-11-04 20:50:27 +0000405} {2048 2048}
406
407do_test multiplex-3.2.1b {
408 sqlite3 db1b test.db
409 sqlite3 db2b test2.db
410} {}
411
412do_test multiplex-3.2.2 { execsql { INSERT INTO t1 VALUES('x', 'y') } db1a } {}
413do_test multiplex-3.2.3 { execsql { INSERT INTO t1 VALUES('v', 'w') } db1b } {}
414do_test multiplex-3.2.4 { execsql { INSERT INTO t1 VALUES('t', 'u') } db2a } {}
415do_test multiplex-3.2.5 { execsql { INSERT INTO t1 VALUES('r', 's') } db2b } {}
416
417do_test multiplex-3.2.6 {
418 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1a
419} {}
420do_test multiplex-3.2.7 {
421 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1b
422} {}
423do_test multiplex-3.2.8 {
424 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2a
425} {}
426do_test multiplex-3.2.9 {
427 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2b
428} {}
429
430do_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
435} {}
436
437do_test multiplex-3.2.X {
438 foreach db {db1a db2a db2b db1b} { catch { $db close } }
439} {}
440
441#-------------------------------------------------------------------------
442#
443
444sqlite3_multiplex_initialize "" 1
shanehd50deee2011-03-29 05:06:46 +0000445multiplex_set db main 32768 16
shaneh8a922f72010-11-04 20:50:27 +0000446
447# Return a list of all currently defined multiplexs.
448proc multiplex_list {} {
dandadafa82016-10-27 14:51:02 +0000449 glob -nocomplain test2.db*
shaneh8a922f72010-11-04 20:50:27 +0000450}
451
452do_test multiplex-4.1.6 {
453 multiplex_delete test2.db
454 sqlite3 db 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]
458} {1}
459do_test multiplex-4.1.6a {
460 sqlite3 db2 test2.db
461 db2 eval {SELECT * FROM t2}
462} {tab-t2}
463do_test multiplex-4.1.7 {
464 execsql {INSERT INTO t2 VALUES(zeroblob(200000))}
465} {}
466do_test multiplex-4.1.8 {
467 sqlite3 db2 test2.db
468 db2 eval {SELECT count(*) FROM t2}
469} {2}
470do_test multiplex-4.1.8a {
471 db2 eval { DELETE FROM t2 WHERE x = 'tab-t2' }
472} {}
473do_test multiplex-4.1.8b {
474 sqlite3 db2 test2.db
475 db2 eval {SELECT count(*) FROM t2}
476} {1}
477
478
479do_test multiplex-4.1.9 {
480 execsql {INSERT INTO t2 VALUES(zeroblob(200000))}
481} {}
482do_test multiplex-4.1.10 {
483 set res [multiplex_list]
484 list [regexp {test2.db} $res]
485} {1}
486do_test multiplex-4.1.11 {
487 db2 close
488 set res [multiplex_list]
489 list [regexp {test2.db} $res]
490} {1}
491do_test multiplex-4.1.12 {
492 db close
493 multiplex_list
dandadafa82016-10-27 14:51:02 +0000494} {test2.db}
shaneh8a922f72010-11-04 20:50:27 +0000495
496
497#-------------------------------------------------------------------------
498# The following tests test that the multiplex VFS handles malloc and IO
499# errors.
500#
501
502sqlite3_multiplex_initialize "" 1
shanehd50deee2011-03-29 05:06:46 +0000503multiplex_set db main 32768 16
shaneh8a922f72010-11-04 20:50:27 +0000504
505do_faultsim_test multiplex-5.1 -prep {
506 catch {db close}
507} -body {
508 sqlite3 db test2.db
509}
510do_faultsim_test multiplex-5.2 -prep {
511 catch {db close}
512} -body {
513 sqlite3 db test.db
514}
515
516catch { db close }
517multiplex_delete test.db
shanehb5830292010-11-05 17:51:25 +0000518multiplex_delete test2.db
shaneh8a922f72010-11-04 20:50:27 +0000519
520do_test multiplex-5.3.prep {
521 sqlite3 db test.db
522 execsql {
523 PRAGMA auto_vacuum = 1;
524 PRAGMA page_size = 1024;
525 CREATE TABLE t1(a, b);
526 INSERT INTO t1 VALUES(10, zeroblob(1200));
527 }
528 faultsim_save_and_close
529} {}
530do_faultsim_test multiplex-5.3 -prep {
531 faultsim_restore_and_reopen
532} -body {
533 execsql { DELETE FROM t1 }
534}
535
536do_test multiplex-5.4.1 {
537 catch { db close }
538 multiplex_delete test.db
539 file mkdir test.db
540 list [catch { sqlite3 db test.db } msg] $msg
541} {1 {unable to open database file}}
mistachkinfda06be2011-08-02 00:57:34 +0000542catch { delete_file test.db }
shaneh8a922f72010-11-04 20:50:27 +0000543
544do_faultsim_test multiplex-5.5 -prep {
545 catch { sqlite3_multiplex_shutdown }
546} -body {
547 sqlite3_multiplex_initialize "" 1
shanehd50deee2011-03-29 05:06:46 +0000548 multiplex_set db main 32768 16
shaneh8a922f72010-11-04 20:50:27 +0000549}
550
shanehcc4e19b2011-05-18 02:22:41 +0000551#-------------------------------------------------------------------------
552# Test that you can vacuum a multiplex'ed DB.
553
554ifcapable vacuum {
555
drhbabb61f2011-06-15 23:34:51 +0000556sqlite3_multiplex_shutdown
shanehcc4e19b2011-05-18 02:22:41 +0000557do_test multiplex-6.0.0 {
558 multiplex_delete test.db
drhf3717af2011-07-20 16:35:31 +0000559 multiplex_delete test.x
shanehcc4e19b2011-05-18 02:22:41 +0000560 sqlite3_multiplex_initialize "" 1
drhf3717af2011-07-20 16:35:31 +0000561 sqlite3 db test.x
shanehcc4e19b2011-05-18 02:22:41 +0000562 multiplex_set db main 4096 16
563} {SQLITE_OK}
564
565do_test multiplex-6.1.0 {
566 execsql {
567 PRAGMA page_size=1024;
568 PRAGMA journal_mode=DELETE;
569 PRAGMA auto_vacuum=OFF;
570 }
571 execsql {
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));
575 }
576} {}
drhf3717af2011-07-20 16:35:31 +0000577do_test multiplex-6.2.1 { file size [multiplex_name test.x 0] } [list $g_chunk_size]
578do_test multiplex-6.2.2 { file size [multiplex_name test.x 1] } [list $g_chunk_size]
shanehcc4e19b2011-05-18 02:22:41 +0000579
580do_test multiplex-6.3.0 {
581 execsql { VACUUM }
582} {}
583
584do_test multiplex-6.99 {
585 db close
drhf3717af2011-07-20 16:35:31 +0000586 multiplex_delete test.x
shanehcc4e19b2011-05-18 02:22:41 +0000587 sqlite3_multiplex_shutdown
588} {SQLITE_OK}
589
590}
591
592
danf6296ca2014-07-31 18:14:37 +0000593catch { db close }
shaneh8a922f72010-11-04 20:50:27 +0000594catch { sqlite3_multiplex_shutdown }
danf6296ca2014-07-31 18:14:37 +0000595sqlite3_shutdown
596test_sqlite3_log
597sqlite3_initialize
shaneh8a922f72010-11-04 20:50:27 +0000598finish_test