blob: 5db56f264ace22ae634e14628faf5ed10574e78a [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
shaneh8a922f72010-11-04 20:50:27 +0000198do_test multiplex-2.4.1 {
199 sqlite3_multiplex_shutdown
200} {SQLITE_MISUSE}
201do_test multiplex-2.4.2 {
202 execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
203} {}
drha1a82982014-07-30 15:43:05 +0000204do_test multiplex-2.4.3 {
205 set ::log
206} {SQLITE_MISUSE {sqlite3_multiplex_shutdown() called while database connections are still open}}
drhf3717af2011-07-20 16:35:31 +0000207do_test multiplex-2.4.4 { file size [multiplex_name test.x 0] } {7168}
drhec0c7652012-01-09 13:41:59 +0000208do_test multiplex-2.4.5 {
shaneh8a922f72010-11-04 20:50:27 +0000209 db close
drhec0c7652012-01-09 13:41:59 +0000210 sqlite3 db test.x
211 db eval vacuum
212 db close
213 glob test.x*
214} {test.x}
215do_test multiplex-2.4.99 {
shaneh8a922f72010-11-04 20:50:27 +0000216 sqlite3_multiplex_shutdown
217} {SQLITE_OK}
218
shanehfd1552f2010-11-05 03:43:54 +0000219do_test multiplex-2.5.1 {
drhf3717af2011-07-20 16:35:31 +0000220 multiplex_delete test.x
shanehfd1552f2010-11-05 03:43:54 +0000221 sqlite3_multiplex_initialize "" 1
drhf3717af2011-07-20 16:35:31 +0000222 sqlite3 db test.x
shanehd50deee2011-03-29 05:06:46 +0000223 multiplex_set db main 4096 16
shanehfd1552f2010-11-05 03:43:54 +0000224} {SQLITE_OK}
225
226do_test multiplex-2.5.2 {
shanehfd1552f2010-11-05 03:43:54 +0000227 execsql {
228 PRAGMA page_size = 1024;
229 PRAGMA journal_mode = delete;
230 PRAGMA auto_vacuum = off;
231 CREATE TABLE t1(a PRIMARY KEY, b);
232 }
233} {delete}
234
235do_test multiplex-2.5.3 {
236 execsql {
237 INSERT INTO t1 VALUES(1, 'one');
238 INSERT INTO t1 VALUES(2, randomblob(4000));
239 INSERT INTO t1 VALUES(3, 'three');
240 INSERT INTO t1 VALUES(4, randomblob(4000));
shaneh78c4de42011-03-31 05:31:24 +0000241 INSERT INTO t1 VALUES(5, 'five');
242 INSERT INTO t1 VALUES(6, randomblob($g_chunk_size));
243 INSERT INTO t1 VALUES(7, randomblob($g_chunk_size));
shanehfd1552f2010-11-05 03:43:54 +0000244 }
245} {}
246
247do_test multiplex-2.5.4 {
248 db eval {SELECT * FROM t1 WHERE a=1}
249} {1 one}
250
251do_test multiplex-2.5.5 {
252 db eval {SELECT * FROM t1 WHERE a=3}
253} {3 three}
254
255do_test multiplex-2.5.6 {
256 db eval {SELECT * FROM t1 WHERE a=5}
257} {5 five}
258
259do_test multiplex-2.5.7 {
260 db eval {SELECT a,length(b) FROM t1 WHERE a=2}
261} {2 4000}
262
263do_test multiplex-2.5.8 {
264 db eval {SELECT a,length(b) FROM t1 WHERE a=4}
265} {4 4000}
266
drhf3717af2011-07-20 16:35:31 +0000267do_test multiplex-2.5.9 { file size [multiplex_name test.x 0] } [list $g_chunk_size]
268do_test multiplex-2.5.10 { file size [multiplex_name test.x 1] } [list $g_chunk_size]
shanehb5830292010-11-05 17:51:25 +0000269
shanehfd1552f2010-11-05 03:43:54 +0000270do_test multiplex-2.5.99 {
271 db close
272 sqlite3_multiplex_shutdown
273} {SQLITE_OK}
274
shanehb5830292010-11-05 17:51:25 +0000275
shaneh0596bee2010-11-05 20:50:43 +0000276set all_journal_modes {delete persist truncate memory off}
277foreach jmode $all_journal_modes {
278 for {set sz 151} {$sz<8000} {set sz [expr $sz+419]} {
shanehb5830292010-11-05 17:51:25 +0000279
shaneh0596bee2010-11-05 20:50:43 +0000280 do_test multiplex-2.6.1.$sz.$jmode {
281 multiplex_delete test.db
282 sqlite3_multiplex_initialize "" 1
shanehac039682011-03-30 21:03:07 +0000283 sqlite3 db test.db
shanehd50deee2011-03-29 05:06:46 +0000284 multiplex_set db main $sz 32
shaneh0596bee2010-11-05 20:50:43 +0000285 } {SQLITE_OK}
shanehb5830292010-11-05 17:51:25 +0000286
shaneh0596bee2010-11-05 20:50:43 +0000287 do_test multiplex-2.6.2.$sz.$jmode {
shaneh0596bee2010-11-05 20:50:43 +0000288 db eval {
289 PRAGMA page_size = 1024;
290 PRAGMA auto_vacuum = off;
291 }
292 db eval "PRAGMA journal_mode = $jmode;"
293 } $jmode
shanehb5830292010-11-05 17:51:25 +0000294
shaneh0596bee2010-11-05 20:50:43 +0000295 do_test multiplex-2.6.3.$sz.$jmode {
296 execsql {
297 CREATE TABLE t1(a PRIMARY KEY, b);
298 INSERT INTO t1 VALUES(1, 'one');
299 INSERT INTO t1 VALUES(2, randomblob($g_chunk_size));
300 }
301 } {}
shanehb5830292010-11-05 17:51:25 +0000302
shaneh0596bee2010-11-05 20:50:43 +0000303 do_test multiplex-2.6.4.$sz.$jmode {
304 db eval {SELECT b FROM t1 WHERE a=1}
305 } {one}
shanehb5830292010-11-05 17:51:25 +0000306
shaneh0596bee2010-11-05 20:50:43 +0000307 do_test multiplex-2.6.5.$sz.$jmode {
308 db eval {SELECT length(b) FROM t1 WHERE a=2}
309 } [list $g_chunk_size]
shanehb5830292010-11-05 17:51:25 +0000310
shaneh050d09a2010-11-08 19:16:16 +0000311 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 +0000312
shaneh0596bee2010-11-05 20:50:43 +0000313 do_test multiplex-2.6.99.$sz.$jmode {
314 db close
315 sqlite3_multiplex_shutdown
316 } {SQLITE_OK}
shanehb5830292010-11-05 17:51:25 +0000317
shaneh0596bee2010-11-05 20:50:43 +0000318 }
shanehb5830292010-11-05 17:51:25 +0000319}
320
shaneh3801b652011-04-01 14:22:46 +0000321do_test multiplex-2.7.1 { multiplex_delete test.db } {}
322do_test multiplex-2.7.2 { sqlite3_multiplex_initialize "" 1 } {SQLITE_OK}
323do_test multiplex-2.7.3 { sqlite3 db test.db } {}
324do_test multiplex-2.7.4 { lindex [ catchsql { SELECT multiplex_control(2, 65536); } ] 0 } {0}
325do_test multiplex-2.7.5 { lindex [ catchsql { SELECT multiplex_control(1, 0); } ] 0 } {0}
shanehc27fa4b2011-03-31 15:11:53 +0000326do_test multiplex-2.7.6 {
327 execsql {
328 CREATE TABLE t1(a PRIMARY KEY, b);
329 INSERT INTO t1 VALUES(1, randomblob(1000));
330 }
331} {}
332# verify only one file, and file size is less than chunks size
333do_test multiplex-2.7.7 { expr ([file size [multiplex_name test.db 0]] < 65536) } {1}
334do_test multiplex-2.7.8 { file exists [multiplex_name test.db 1] } {0}
335do_test multiplex-2.7.9 {
336 execsql {
shaneh3801b652011-04-01 14:22:46 +0000337 INSERT INTO t1 VALUES(2, randomblob(65536));
shanehc27fa4b2011-03-31 15:11:53 +0000338 }
339} {}
340# verify only one file, and file size exceeds chunks size
341do_test multiplex-2.7.10 { expr ([file size [multiplex_name test.db 0]] > 65536) } {1}
342do_test multiplex-2.7.11 { file exists [multiplex_name test.db 1] } {0}
343do_test multiplex-2.7.12 { db close } {}
344do_test multiplex-2.7.13 { sqlite3_multiplex_shutdown } {SQLITE_OK}
345
shaneh8a922f72010-11-04 20:50:27 +0000346#-------------------------------------------------------------------------
347# Try some tests with more than one connection to a database file. Still
348# in rollback mode.
349#
350# multiplex-3.1.*: Two connections to a single database file.
351#
352# multiplex-3.2.*: Two connections to each of several database files (that
353# are in the same multiplex group).
354#
355do_test multiplex-3.1.1 {
356 multiplex_delete test.db
357 sqlite3_multiplex_initialize "" 1
shanehac039682011-03-30 21:03:07 +0000358 sqlite3 db test.db
shanehd50deee2011-03-29 05:06:46 +0000359 multiplex_set db main 32768 16
shaneh8a922f72010-11-04 20:50:27 +0000360} {SQLITE_OK}
361do_test multiplex-3.1.2 {
shaneh8a922f72010-11-04 20:50:27 +0000362 execsql {
363 PRAGMA page_size = 1024;
364 PRAGMA journal_mode = delete;
365 PRAGMA auto_vacuum = off;
366 CREATE TABLE t1(a PRIMARY KEY, b);
367 INSERT INTO t1 VALUES(1, 'one');
368 }
shaneh050d09a2010-11-08 19:16:16 +0000369 file size [multiplex_name test.db 0]
shaneh8a922f72010-11-04 20:50:27 +0000370} {3072}
371do_test multiplex-3.1.3 {
372 sqlite3 db2 test.db
373 execsql { CREATE TABLE t2(a, b) } db2
374} {}
375do_test multiplex-3.1.4 {
376 execsql { CREATE TABLE t3(a, b) }
377} {}
378do_test multiplex-3.1.5 {
379 catchsql { CREATE TABLE t3(a, b) }
380} {1 {table t3 already exists}}
381do_test multiplex-3.1.6 {
382 db close
383 db2 close
384} {}
385
386do_test multiplex-3.2.1a {
387
388 multiplex_delete test.db
389 multiplex_delete test2.db
390
391 sqlite3 db1a test.db
392 sqlite3 db2a test2.db
393
394 foreach db {db1a db2a} {
395 execsql {
396 PRAGMA page_size = 1024;
397 PRAGMA journal_mode = delete;
398 PRAGMA auto_vacuum = off;
399 CREATE TABLE t1(a, b);
400 } $db
401 }
402
shaneh050d09a2010-11-08 19:16:16 +0000403 list [file size [multiplex_name test.db 0]] [file size [multiplex_name test2.db 0]]
shaneh8a922f72010-11-04 20:50:27 +0000404} {2048 2048}
405
406do_test multiplex-3.2.1b {
407 sqlite3 db1b test.db
408 sqlite3 db2b test2.db
409} {}
410
411do_test multiplex-3.2.2 { execsql { INSERT INTO t1 VALUES('x', 'y') } db1a } {}
412do_test multiplex-3.2.3 { execsql { INSERT INTO t1 VALUES('v', 'w') } db1b } {}
413do_test multiplex-3.2.4 { execsql { INSERT INTO t1 VALUES('t', 'u') } db2a } {}
414do_test multiplex-3.2.5 { execsql { INSERT INTO t1 VALUES('r', 's') } db2b } {}
415
416do_test multiplex-3.2.6 {
417 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1a
418} {}
419do_test multiplex-3.2.7 {
420 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1b
421} {}
422do_test multiplex-3.2.8 {
423 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2a
424} {}
425do_test multiplex-3.2.9 {
426 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2b
427} {}
428
429do_test multiplex-3.3.1 {
430 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1a
431 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1b
432 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2a
433 execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2b
434} {}
435
436do_test multiplex-3.2.X {
437 foreach db {db1a db2a db2b db1b} { catch { $db close } }
438} {}
439
440#-------------------------------------------------------------------------
441#
442
443sqlite3_multiplex_initialize "" 1
shanehd50deee2011-03-29 05:06:46 +0000444multiplex_set db main 32768 16
shaneh8a922f72010-11-04 20:50:27 +0000445
446# Return a list of all currently defined multiplexs.
447proc multiplex_list {} {
448 set allq {}
449 foreach q [sqlite3_multiplex_dump] {
450 lappend allq [lindex $q 0]
451 }
452 return [lsort $allq]
453}
454
455do_test multiplex-4.1.6 {
456 multiplex_delete test2.db
457 sqlite3 db test2.db
458 db eval {CREATE TABLE t2(x); INSERT INTO t2 VALUES('tab-t2');}
459 set res [multiplex_list]
460 list [regexp {test2.db} $res]
461} {1}
462do_test multiplex-4.1.6a {
463 sqlite3 db2 test2.db
464 db2 eval {SELECT * FROM t2}
465} {tab-t2}
466do_test multiplex-4.1.7 {
467 execsql {INSERT INTO t2 VALUES(zeroblob(200000))}
468} {}
469do_test multiplex-4.1.8 {
470 sqlite3 db2 test2.db
471 db2 eval {SELECT count(*) FROM t2}
472} {2}
473do_test multiplex-4.1.8a {
474 db2 eval { DELETE FROM t2 WHERE x = 'tab-t2' }
475} {}
476do_test multiplex-4.1.8b {
477 sqlite3 db2 test2.db
478 db2 eval {SELECT count(*) FROM t2}
479} {1}
480
481
482do_test multiplex-4.1.9 {
483 execsql {INSERT INTO t2 VALUES(zeroblob(200000))}
484} {}
485do_test multiplex-4.1.10 {
486 set res [multiplex_list]
487 list [regexp {test2.db} $res]
488} {1}
489do_test multiplex-4.1.11 {
490 db2 close
491 set res [multiplex_list]
492 list [regexp {test2.db} $res]
493} {1}
494do_test multiplex-4.1.12 {
495 db close
496 multiplex_list
497} {}
498
499
500#-------------------------------------------------------------------------
501# The following tests test that the multiplex VFS handles malloc and IO
502# errors.
503#
504
505sqlite3_multiplex_initialize "" 1
shanehd50deee2011-03-29 05:06:46 +0000506multiplex_set db main 32768 16
shaneh8a922f72010-11-04 20:50:27 +0000507
508do_faultsim_test multiplex-5.1 -prep {
509 catch {db close}
510} -body {
511 sqlite3 db test2.db
512}
513do_faultsim_test multiplex-5.2 -prep {
514 catch {db close}
515} -body {
516 sqlite3 db test.db
517}
518
519catch { db close }
520multiplex_delete test.db
shanehb5830292010-11-05 17:51:25 +0000521multiplex_delete test2.db
shaneh8a922f72010-11-04 20:50:27 +0000522
523do_test multiplex-5.3.prep {
524 sqlite3 db test.db
525 execsql {
526 PRAGMA auto_vacuum = 1;
527 PRAGMA page_size = 1024;
528 CREATE TABLE t1(a, b);
529 INSERT INTO t1 VALUES(10, zeroblob(1200));
530 }
531 faultsim_save_and_close
532} {}
533do_faultsim_test multiplex-5.3 -prep {
534 faultsim_restore_and_reopen
535} -body {
536 execsql { DELETE FROM t1 }
537}
538
539do_test multiplex-5.4.1 {
540 catch { db close }
541 multiplex_delete test.db
542 file mkdir test.db
543 list [catch { sqlite3 db test.db } msg] $msg
544} {1 {unable to open database file}}
mistachkinfda06be2011-08-02 00:57:34 +0000545catch { delete_file test.db }
shaneh8a922f72010-11-04 20:50:27 +0000546
547do_faultsim_test multiplex-5.5 -prep {
548 catch { sqlite3_multiplex_shutdown }
549} -body {
550 sqlite3_multiplex_initialize "" 1
shanehd50deee2011-03-29 05:06:46 +0000551 multiplex_set db main 32768 16
shaneh8a922f72010-11-04 20:50:27 +0000552}
553
shanehcc4e19b2011-05-18 02:22:41 +0000554#-------------------------------------------------------------------------
555# Test that you can vacuum a multiplex'ed DB.
556
557ifcapable vacuum {
558
drhbabb61f2011-06-15 23:34:51 +0000559sqlite3_multiplex_shutdown
shanehcc4e19b2011-05-18 02:22:41 +0000560do_test multiplex-6.0.0 {
561 multiplex_delete test.db
drhf3717af2011-07-20 16:35:31 +0000562 multiplex_delete test.x
shanehcc4e19b2011-05-18 02:22:41 +0000563 sqlite3_multiplex_initialize "" 1
drhf3717af2011-07-20 16:35:31 +0000564 sqlite3 db test.x
shanehcc4e19b2011-05-18 02:22:41 +0000565 multiplex_set db main 4096 16
566} {SQLITE_OK}
567
568do_test multiplex-6.1.0 {
569 execsql {
570 PRAGMA page_size=1024;
571 PRAGMA journal_mode=DELETE;
572 PRAGMA auto_vacuum=OFF;
573 }
574 execsql {
575 CREATE TABLE t1(a, b);
576 INSERT INTO t1 VALUES(1, randomblob($g_chunk_size));
577 INSERT INTO t1 VALUES(2, randomblob($g_chunk_size));
578 }
579} {}
drhf3717af2011-07-20 16:35:31 +0000580do_test multiplex-6.2.1 { file size [multiplex_name test.x 0] } [list $g_chunk_size]
581do_test multiplex-6.2.2 { file size [multiplex_name test.x 1] } [list $g_chunk_size]
shanehcc4e19b2011-05-18 02:22:41 +0000582
583do_test multiplex-6.3.0 {
584 execsql { VACUUM }
585} {}
586
587do_test multiplex-6.99 {
588 db close
drhf3717af2011-07-20 16:35:31 +0000589 multiplex_delete test.x
shanehcc4e19b2011-05-18 02:22:41 +0000590 sqlite3_multiplex_shutdown
591} {SQLITE_OK}
592
593}
594
595
danf6296ca2014-07-31 18:14:37 +0000596catch { db close }
shaneh8a922f72010-11-04 20:50:27 +0000597catch { sqlite3_multiplex_shutdown }
danf6296ca2014-07-31 18:14:37 +0000598sqlite3_shutdown
599test_sqlite3_log
600sqlite3_initialize
shaneh8a922f72010-11-04 20:50:27 +0000601finish_test