/ Check-in [f2004b44]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:More tests and added support for xDelete in multiplex VFS.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: f2004b44bfba62a7a2296b161a25aefdf55e035a
User & Date: shaneh 2010-11-05 17:51:25
Context
2010-11-05
18:07
Fix os_unix.c so that it works with the test_multiplex module. check-in: 72ba3e36 user: dan tags: trunk
17:51
More tests and added support for xDelete in multiplex VFS. check-in: f2004b44 user: shaneh tags: trunk
03:58
Additional error checking and tests for multiplex VFS. check-in: 1ab9a592 user: shaneh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/test_multiplex.c.

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
...
108
109
110
111
112
113
114





115
116
117
118
119
120
121
...
139
140
141
142
143
144
145
146
147
148
149
150

151
152
153
154
155
156
157
...
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
...
215
216
217
218
219
220
221





































222
223
224
225
226
227
228
...
229
230
231
232
233
234
235

236
237
238
239
240
241
242
243

244
245
246
247
248
249
250
...
571
572
573
574
575
576
577





578
579
580
581
582
583
584
585
586
587

588
589
590
591
592
593
594
...
617
618
619
620
621
622
623

624
625
626
627
628
629
630
#include "sqlite3.h"
#include <string.h>
#include <assert.h>
#include "sqliteInt.h"

/************************ Shim Definitions ******************************/

#define SQLITE_MULTIPLEX_CHUNK_SIZE 0x80000000
#define SQLITE_MULTIPLEX_MAX_CHUNKS 32

/************************ Object Definitions ******************************/

/* Forward declaration of all object types */
typedef struct multiplexGroup multiplexGroup;
typedef struct multiplexConn multiplexConn;
................................................................................
  multiplexGroup *pGroups;

  /* Chunk params.
  */
  int nChunkSize;
  int nMaxChunks;






} gMultiplex;

/************************* Utility Routines *********************************/
/*
** Acquire and release the mutex used to serialize access to the
** list of multiplexGroups.
*/
................................................................................
        return pSubOpen;
      }
      return NULL;
    }
    *rc = SQLITE_OK;
    return pSubOpen;
  }
  *rc = SQLITE_ERROR;
  return NULL;
}

/************************* VFS Method Wrappers *****************************/

/*
** This is the xOpen method used for the "multiplex" VFS.
**
** Most of the work is done by the underlying original VFS.  This method
** simply links the new file into the appropriate multiplex group if it is a
** file that needs to be tracked.
*/
................................................................................
  sqlite3_file *pSubOpen;                        /* Real file descriptor */
  sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs;   /* Real VFS */
  int nName = sqlite3Strlen30(zName);
  int i;

  UNUSED_PARAMETER(pVfs);

  /* If the file is not a main database file or a WAL, then use the
  ** normal xOpen method.
  */
  if( (flags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL))==0 ){
    return pOrigVfs->xOpen(pOrigVfs, zName, pConn, flags, pOutFlags);
  }

  /* We need to create a group structure and manage
  ** access to this group of files.
  */
  multiplexEnter();
  pMultiplexOpen = (multiplexConn*)pConn;
  /* allocate space for group, file handles, 
  ** and file name (+ extra for "-0000\0") 
................................................................................
    }else{
      sqlite3_free(pGroup);
    }
  }
  multiplexLeave();
  return rc;
}






































/************************ I/O Method Wrappers *******************************/

/* xClose requests get passed through to the original VFS.
** We loop over all open chunk handles and close them.
** The group structure for this file is unlinked from 
** our list of groups and freed.
................................................................................
*/
static int multiplexClose(sqlite3_file *pConn){
  multiplexConn *p = (multiplexConn*)pConn;
  multiplexGroup *pGroup = p->pGroup;
  int rc = SQLITE_OK;
  int i;
  multiplexEnter();

  for(i=0; i<gMultiplex.nMaxChunks; i++){
    if( pGroup->bOpen[i] ){
      sqlite3_file *pSubOpen = pGroup->pReal[i];
      int rc2 = pSubOpen->pMethods->xClose(pSubOpen);
      if( rc2!=SQLITE_OK ) rc = rc2;
      pGroup->bOpen[i] = 0;
    }
  }

  if( pGroup->pNext ) pGroup->pNext->pPrev = pGroup->pPrev;
  if( pGroup->pPrev ){
    pGroup->pPrev->pNext = pGroup->pNext;
  }else{
    gMultiplex.pGroups = pGroup->pNext;
  }
  sqlite3_free(pGroup);
................................................................................
  if( gMultiplex.isInitialized ) return SQLITE_MISUSE;
  pOrigVfs = sqlite3_vfs_find(zOrigVfsName);
  if( pOrigVfs==0 ) return SQLITE_ERROR;
  assert( pOrigVfs!=&gMultiplex.sThisVfs );
  gMultiplex.pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
  if( !gMultiplex.pMutex ){
    return SQLITE_NOMEM;





  }
  gMultiplex.nChunkSize = SQLITE_MULTIPLEX_CHUNK_SIZE;
  gMultiplex.nMaxChunks = SQLITE_MULTIPLEX_MAX_CHUNKS;
  gMultiplex.pGroups = NULL;
  gMultiplex.isInitialized = 1;
  gMultiplex.pOrigVfs = pOrigVfs;
  gMultiplex.sThisVfs = *pOrigVfs;
  gMultiplex.sThisVfs.szOsFile += sizeof(multiplexConn);
  gMultiplex.sThisVfs.zName = "multiplex";
  gMultiplex.sThisVfs.xOpen = multiplexOpen;

  gMultiplex.sIoMethodsV1.iVersion = 1;
  gMultiplex.sIoMethodsV1.xClose = multiplexClose;
  gMultiplex.sIoMethodsV1.xRead = multiplexRead;
  gMultiplex.sIoMethodsV1.xWrite = multiplexWrite;
  gMultiplex.sIoMethodsV1.xTruncate = multiplexTruncate;
  gMultiplex.sIoMethodsV1.xSync = multiplexSync;
  gMultiplex.sIoMethodsV1.xFileSize = multiplexFileSize;
................................................................................
** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly once while
** shutting down in order to free all remaining multiplex groups.
*/
int sqlite3_multiplex_shutdown(void){
  if( gMultiplex.isInitialized==0 ) return SQLITE_MISUSE;
  if( gMultiplex.pGroups ) return SQLITE_MISUSE;
  gMultiplex.isInitialized = 0;

  sqlite3_mutex_free(gMultiplex.pMutex);
  sqlite3_vfs_unregister(&gMultiplex.sThisVfs);
  memset(&gMultiplex, 0, sizeof(gMultiplex));
  return SQLITE_OK;
}

/*







|







 







>
>
>
>
>







 







|




>







 







<
<
<
<
<
<
<







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>








>







 







>
>
>
>
>










>







 







>







22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
...
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
...
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
...
174
175
176
177
178
179
180







181
182
183
184
185
186
187
...
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
...
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
...
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
...
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
#include "sqlite3.h"
#include <string.h>
#include <assert.h>
#include "sqliteInt.h"

/************************ Shim Definitions ******************************/

#define SQLITE_MULTIPLEX_CHUNK_SIZE 0x40000000
#define SQLITE_MULTIPLEX_MAX_CHUNKS 32

/************************ Object Definitions ******************************/

/* Forward declaration of all object types */
typedef struct multiplexGroup multiplexGroup;
typedef struct multiplexConn multiplexConn;
................................................................................
  multiplexGroup *pGroups;

  /* Chunk params.
  */
  int nChunkSize;
  int nMaxChunks;

  /* Storage for temp file names.  Allocated during 
  ** initialization to the max pathname of the underlying VFS.
  */
  char *zName;

} gMultiplex;

/************************* Utility Routines *********************************/
/*
** Acquire and release the mutex used to serialize access to the
** list of multiplexGroups.
*/
................................................................................
        return pSubOpen;
      }
      return NULL;
    }
    *rc = SQLITE_OK;
    return pSubOpen;
  }
  *rc = SQLITE_FULL;
  return NULL;
}

/************************* VFS Method Wrappers *****************************/

/*
** This is the xOpen method used for the "multiplex" VFS.
**
** Most of the work is done by the underlying original VFS.  This method
** simply links the new file into the appropriate multiplex group if it is a
** file that needs to be tracked.
*/
................................................................................
  sqlite3_file *pSubOpen;                        /* Real file descriptor */
  sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs;   /* Real VFS */
  int nName = sqlite3Strlen30(zName);
  int i;

  UNUSED_PARAMETER(pVfs);








  /* We need to create a group structure and manage
  ** access to this group of files.
  */
  multiplexEnter();
  pMultiplexOpen = (multiplexConn*)pConn;
  /* allocate space for group, file handles, 
  ** and file name (+ extra for "-0000\0") 
................................................................................
    }else{
      sqlite3_free(pGroup);
    }
  }
  multiplexLeave();
  return rc;
}

/*
** This is the xDelete method used for the "multiplex" VFS.
** It attempts to delete the filename specified, as well
** as addiitional files with the "-####" extension.
*/
static int multiplexDelete(
  sqlite3_vfs *pVfs,         /* The multiplex VFS */
  const char *zName,         /* Name of file to delete */
  int syncDir
){
  sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs;   /* Real VFS */
  int rc = SQLITE_OK;
  int nName = sqlite3Strlen30(zName);
  int i;

  UNUSED_PARAMETER(pVfs);

  multiplexEnter();
  memcpy(gMultiplex.zName, zName, nName+1);
  for(i=0; i<gMultiplex.nMaxChunks; i++){
    int rc2;
    int exists = 0;
    if( i ) sqlite3_snprintf(nName+6, gMultiplex.zName+nName, "-%04d", i);
    rc2 = pOrigVfs->xAccess(pOrigVfs, gMultiplex.zName, SQLITE_ACCESS_EXISTS, &exists);
    if( rc2==SQLITE_OK && exists){
      /* if it exists, delete it */
      rc2 = pOrigVfs->xDelete(pOrigVfs, gMultiplex.zName, syncDir);
      if( rc2!=SQLITE_OK ) rc = rc2;
    }else{
      /* stop at first "gap" */
      break;
    }
  }
  multiplexLeave();
  return rc;
}

/************************ I/O Method Wrappers *******************************/

/* xClose requests get passed through to the original VFS.
** We loop over all open chunk handles and close them.
** The group structure for this file is unlinked from 
** our list of groups and freed.
................................................................................
*/
static int multiplexClose(sqlite3_file *pConn){
  multiplexConn *p = (multiplexConn*)pConn;
  multiplexGroup *pGroup = p->pGroup;
  int rc = SQLITE_OK;
  int i;
  multiplexEnter();
  /* close any open handles */
  for(i=0; i<gMultiplex.nMaxChunks; i++){
    if( pGroup->bOpen[i] ){
      sqlite3_file *pSubOpen = pGroup->pReal[i];
      int rc2 = pSubOpen->pMethods->xClose(pSubOpen);
      if( rc2!=SQLITE_OK ) rc = rc2;
      pGroup->bOpen[i] = 0;
    }
  }
  /* remove from linked list */
  if( pGroup->pNext ) pGroup->pNext->pPrev = pGroup->pPrev;
  if( pGroup->pPrev ){
    pGroup->pPrev->pNext = pGroup->pNext;
  }else{
    gMultiplex.pGroups = pGroup->pNext;
  }
  sqlite3_free(pGroup);
................................................................................
  if( gMultiplex.isInitialized ) return SQLITE_MISUSE;
  pOrigVfs = sqlite3_vfs_find(zOrigVfsName);
  if( pOrigVfs==0 ) return SQLITE_ERROR;
  assert( pOrigVfs!=&gMultiplex.sThisVfs );
  gMultiplex.pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
  if( !gMultiplex.pMutex ){
    return SQLITE_NOMEM;
  }
  gMultiplex.zName = sqlite3_malloc(pOrigVfs->mxPathname);
  if( !gMultiplex.zName ){
    sqlite3_mutex_free(gMultiplex.pMutex);
    return SQLITE_NOMEM;
  }
  gMultiplex.nChunkSize = SQLITE_MULTIPLEX_CHUNK_SIZE;
  gMultiplex.nMaxChunks = SQLITE_MULTIPLEX_MAX_CHUNKS;
  gMultiplex.pGroups = NULL;
  gMultiplex.isInitialized = 1;
  gMultiplex.pOrigVfs = pOrigVfs;
  gMultiplex.sThisVfs = *pOrigVfs;
  gMultiplex.sThisVfs.szOsFile += sizeof(multiplexConn);
  gMultiplex.sThisVfs.zName = "multiplex";
  gMultiplex.sThisVfs.xOpen = multiplexOpen;
  gMultiplex.sThisVfs.xDelete = multiplexDelete;
  gMultiplex.sIoMethodsV1.iVersion = 1;
  gMultiplex.sIoMethodsV1.xClose = multiplexClose;
  gMultiplex.sIoMethodsV1.xRead = multiplexRead;
  gMultiplex.sIoMethodsV1.xWrite = multiplexWrite;
  gMultiplex.sIoMethodsV1.xTruncate = multiplexTruncate;
  gMultiplex.sIoMethodsV1.xSync = multiplexSync;
  gMultiplex.sIoMethodsV1.xFileSize = multiplexFileSize;
................................................................................
** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly once while
** shutting down in order to free all remaining multiplex groups.
*/
int sqlite3_multiplex_shutdown(void){
  if( gMultiplex.isInitialized==0 ) return SQLITE_MISUSE;
  if( gMultiplex.pGroups ) return SQLITE_MISUSE;
  gMultiplex.isInitialized = 0;
  sqlite3_free(gMultiplex.zName);
  sqlite3_mutex_free(gMultiplex.pMutex);
  sqlite3_vfs_unregister(&gMultiplex.sThisVfs);
  memset(&gMultiplex, 0, sizeof(gMultiplex));
  return SQLITE_OK;
}

/*

Changes to test/multiplex.test.

9
10
11
12
13
14
15
16











17
18


19
20
21
22
23




24
25
26
27
28
29
30
..
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
..
59
60
61
62
63
64
65
66
67


68
69
70
71
72
73
74
75
76
...
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
...
151
152
153
154
155
156
157



158
159
160
161
162











































163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
...
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
...
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
...
331
332
333
334
335
336
337

338
339
340
341
342
343
344
...
354
355
356
357
358
359
360

361
362
363
364
365
366
367
368
369
370
#
#***********************************************************************
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
source $testdir/malloc_common.tcl












proc multiplex_delete {name} {
  file delete -force $name


  file delete -force $name-journal
  file delete -force $name-wal
  for {set i 1} {$i<=15} {incr i} {
    file delete -force $name-000$i
    file delete -force $name-00$i




  }
}

db close

#-------------------------------------------------------------------------
#   multiplex-1.1.*: Test initialize and shutdown.
................................................................................

do_test multiplex-1.5 { sqlite3_multiplex_initialize "" 0 }        {SQLITE_OK}
do_test multiplex-1.6 { sqlite3_multiplex_shutdown }               {SQLITE_OK}
do_test multiplex-1.7 { sqlite3_multiplex_initialize "" 1 }        {SQLITE_OK}
do_test multiplex-1.8 { sqlite3_multiplex_shutdown }               {SQLITE_OK}

do_test multiplex-1.9  { sqlite3_multiplex_initialize "" 1 }       {SQLITE_OK}
do_test multiplex-1.10.1 { sqlite3_multiplex_set 0x8000 16 }       {SQLITE_OK}
do_test multiplex-1.10.2 { sqlite3_multiplex_set 0x8000 -1 }       {SQLITE_MISUSE}
do_test multiplex-1.10.3 { sqlite3_multiplex_set -1 16 }           {SQLITE_MISUSE}
do_test multiplex-1.10.4 { sqlite3_multiplex_set 31 16 }           {SQLITE_MISUSE}
do_test multiplex-1.10.5 { sqlite3_multiplex_set 0x8000 33 }       {SQLITE_MISUSE}
do_test multiplex-1.11 { sqlite3_multiplex_shutdown }              {SQLITE_OK}


#-------------------------------------------------------------------------
# Some simple warm-body tests with a single database file in rollback 
# mode:
#
................................................................................
#   multiplex-2.3.*: Open and close a second db.
#
#   multiplex-2.4.*: Try to shutdown the multiplex system before closing the db
#                file. Check that this fails and the multiplex system still works
#                afterwards. Then close the database and successfully shut
#                down the multiplex system.
#
#   multiplex-2.5.*: More reading/writing with small chunk size.



sqlite3_multiplex_initialize "" 1
sqlite3_multiplex_set 0x8000 16

do_test multiplex-2.1.2 {
  sqlite3 db test.db
  execsql {
    PRAGMA page_size=1024;
    PRAGMA auto_vacuum=OFF;
    PRAGMA journal_mode=DELETE;
................................................................................
  sqlite3_multiplex_shutdown
} {SQLITE_OK}


do_test multiplex-2.5.1 {
  multiplex_delete test.db
  sqlite3_multiplex_initialize "" 1
  sqlite3_multiplex_set 0x1000 16
} {SQLITE_OK}

do_test multiplex-2.5.2 {
  sqlite3 db test.db
  execsql {
    PRAGMA page_size = 1024;
    PRAGMA journal_mode = delete;
................................................................................
  db eval {SELECT a,length(b) FROM t1 WHERE a=2}
} {2 4000}

do_test multiplex-2.5.8 {
  db eval {SELECT a,length(b) FROM t1 WHERE a=4}
} {4 4000}




do_test multiplex-2.5.99 {
  db close
  sqlite3_multiplex_shutdown
} {SQLITE_OK}












































#-------------------------------------------------------------------------
# Try some tests with more than one connection to a database file. Still
# in rollback mode.
#
#   multiplex-3.1.*: Two connections to a single database file.
#
#   multiplex-3.2.*: Two connections to each of several database files (that
#                are in the same multiplex group).
#
do_test multiplex-3.1.1 {
  multiplex_delete test.db
  sqlite3_multiplex_initialize "" 1
  sqlite3_multiplex_set 0x8000 16
} {SQLITE_OK}
do_test multiplex-3.1.2 {
  sqlite3 db test.db
  execsql {
    PRAGMA page_size = 1024;
    PRAGMA journal_mode = delete;
    PRAGMA auto_vacuum = off;
................................................................................
  foreach db {db1a db2a db2b db1b} { catch { $db close } }
} {}

#-------------------------------------------------------------------------
#

sqlite3_multiplex_initialize "" 1
sqlite3_multiplex_set 0x8000 16

# Return a list of all currently defined multiplexs.
proc multiplex_list {} {
  set allq {}
  foreach q [sqlite3_multiplex_dump] {
    lappend allq [lindex $q 0]
  }
................................................................................

#-------------------------------------------------------------------------
# The following tests test that the multiplex VFS handles malloc and IO 
# errors.
#

sqlite3_multiplex_initialize "" 1
sqlite3_multiplex_set 0x8000 16

do_faultsim_test multiplex-5.1 -prep {
  catch {db close}
} -body {
  sqlite3 db test2.db
}
do_faultsim_test multiplex-5.2 -prep {
................................................................................
  catch {db close}
} -body {
  sqlite3 db test.db
}

catch { db close }
multiplex_delete test.db


do_test multiplex-5.3.prep {
  sqlite3 db test.db
  execsql {
    PRAGMA auto_vacuum = 1;
    PRAGMA page_size = 1024;
    CREATE TABLE t1(a, b);
................................................................................

do_test multiplex-5.4.1 {
  catch { db close }
  multiplex_delete test.db
  file mkdir test.db
  list [catch { sqlite3 db test.db } msg] $msg
} {1 {unable to open database file}}


do_faultsim_test multiplex-5.5 -prep {
  catch { sqlite3_multiplex_shutdown }
} -body {
  sqlite3_multiplex_initialize "" 1
  sqlite3_multiplex_set 0x8000 16
}

catch { sqlite3_multiplex_shutdown }
finish_test








>
>
>
>
>
>
>
>
>
>
>

<
>
>
|
|
|
<
<
>
>
>
>







 







|
|
|
|
|







 







|
|
>
>

|







 







|







 







>
>
>





>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>












|







 







|







 







|







 







>







 







>





|




9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

29
30
31
32
33


34
35
36
37
38
39
40
41
42
43
44
..
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
..
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
...
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
...
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
...
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
...
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
...
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
...
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#
#***********************************************************************
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
source $testdir/malloc_common.tcl

set g_chunk_size 2147483648
set g_max_chunks 32

proc multiplex_set {chunk_size max_chunks} {
  global g_chunk_size
  global g_max_chunks
  set g_chunk_size $chunk_size
  set g_max_chunks $max_chunks
  sqlite3_multiplex_set $chunk_size $max_chunks
}

proc multiplex_delete {name} {

  global g_max_chunks
  forcedelete $name
  forcedelete $name-journal
  forcedelete $name-wal
  for {set i 1} {$i<$g_max_chunks} {incr i} {


    set num [format "%04d" $i]
    forcedelete $name-$num
    forcedelete $name-journal-$num
    forcedelete $name-wal-$num
  }
}

db close

#-------------------------------------------------------------------------
#   multiplex-1.1.*: Test initialize and shutdown.
................................................................................

do_test multiplex-1.5 { sqlite3_multiplex_initialize "" 0 }        {SQLITE_OK}
do_test multiplex-1.6 { sqlite3_multiplex_shutdown }               {SQLITE_OK}
do_test multiplex-1.7 { sqlite3_multiplex_initialize "" 1 }        {SQLITE_OK}
do_test multiplex-1.8 { sqlite3_multiplex_shutdown }               {SQLITE_OK}

do_test multiplex-1.9  { sqlite3_multiplex_initialize "" 1 }       {SQLITE_OK}
do_test multiplex-1.10.1 { multiplex_set 32768 16 }                {SQLITE_OK}
do_test multiplex-1.10.2 { multiplex_set 32768 -1 }                {SQLITE_MISUSE}
do_test multiplex-1.10.3 { multiplex_set -1 16 }                   {SQLITE_MISUSE}
do_test multiplex-1.10.4 { multiplex_set 31 16 }                   {SQLITE_MISUSE}
do_test multiplex-1.10.5 { multiplex_set 32768 33 }                {SQLITE_MISUSE}
do_test multiplex-1.11 { sqlite3_multiplex_shutdown }              {SQLITE_OK}


#-------------------------------------------------------------------------
# Some simple warm-body tests with a single database file in rollback 
# mode:
#
................................................................................
#   multiplex-2.3.*: Open and close a second db.
#
#   multiplex-2.4.*: Try to shutdown the multiplex system before closing the db
#                file. Check that this fails and the multiplex system still works
#                afterwards. Then close the database and successfully shut
#                down the multiplex system.
#
#   multiplex-2.5.*: More reading/writing.
#
#   multiplex-2.6.*: More reading/writing with varying small chunk sizes.

sqlite3_multiplex_initialize "" 1
multiplex_set 32768 16

do_test multiplex-2.1.2 {
  sqlite3 db test.db
  execsql {
    PRAGMA page_size=1024;
    PRAGMA auto_vacuum=OFF;
    PRAGMA journal_mode=DELETE;
................................................................................
  sqlite3_multiplex_shutdown
} {SQLITE_OK}


do_test multiplex-2.5.1 {
  multiplex_delete test.db
  sqlite3_multiplex_initialize "" 1
  multiplex_set 4096 16
} {SQLITE_OK}

do_test multiplex-2.5.2 {
  sqlite3 db test.db
  execsql {
    PRAGMA page_size = 1024;
    PRAGMA journal_mode = delete;
................................................................................
  db eval {SELECT a,length(b) FROM t1 WHERE a=2}
} {2 4000}

do_test multiplex-2.5.8 {
  db eval {SELECT a,length(b) FROM t1 WHERE a=4}
} {4 4000}

do_test multiplex-2.5.9 { file size test.db } [list $g_chunk_size]
do_test multiplex-2.5.10 { file size test.db-0001 } [list $g_chunk_size]

do_test multiplex-2.5.99 {
  db close
  sqlite3_multiplex_shutdown
} {SQLITE_OK}


for {set sz 151} {$sz<8000} {set sz [expr $sz+419]} {

  do_test multiplex-2.6.1.$sz {
    multiplex_delete test.db
    sqlite3_multiplex_initialize "" 1
    multiplex_set $sz 32
  } {SQLITE_OK}

  do_test multiplex-2.6.2.$sz {
    sqlite3 db test.db
    execsql {
      PRAGMA page_size = 1024;
      PRAGMA journal_mode = delete;
      PRAGMA auto_vacuum = off;
      CREATE TABLE t1(a PRIMARY KEY, b);
    }
  } {delete}

  do_test multiplex-2.6.3.$sz { 
    execsql { 
      INSERT INTO t1 VALUES(1, 'one');
      INSERT INTO t1 VALUES(2, randomblob($g_chunk_size));
    }
  } {}

  do_test multiplex-2.6.4.$sz {
    db eval {SELECT b FROM t1 WHERE a=1}
  } {one}

  do_test multiplex-2.6.5.$sz {
    db eval {SELECT length(b) FROM t1 WHERE a=2}
  } [list $g_chunk_size]

  do_test multiplex-2.6.6.$sz { file size test.db } [list $g_chunk_size]

  do_test multiplex-2.6.99.$sz {
    db close
    sqlite3_multiplex_shutdown
  } {SQLITE_OK}

}

#-------------------------------------------------------------------------
# Try some tests with more than one connection to a database file. Still
# in rollback mode.
#
#   multiplex-3.1.*: Two connections to a single database file.
#
#   multiplex-3.2.*: Two connections to each of several database files (that
#                are in the same multiplex group).
#
do_test multiplex-3.1.1 {
  multiplex_delete test.db
  sqlite3_multiplex_initialize "" 1
  multiplex_set 32768 16
} {SQLITE_OK}
do_test multiplex-3.1.2 {
  sqlite3 db test.db
  execsql {
    PRAGMA page_size = 1024;
    PRAGMA journal_mode = delete;
    PRAGMA auto_vacuum = off;
................................................................................
  foreach db {db1a db2a db2b db1b} { catch { $db close } }
} {}

#-------------------------------------------------------------------------
#

sqlite3_multiplex_initialize "" 1
multiplex_set 32768 16

# Return a list of all currently defined multiplexs.
proc multiplex_list {} {
  set allq {}
  foreach q [sqlite3_multiplex_dump] {
    lappend allq [lindex $q 0]
  }
................................................................................

#-------------------------------------------------------------------------
# The following tests test that the multiplex VFS handles malloc and IO 
# errors.
#

sqlite3_multiplex_initialize "" 1
multiplex_set 32768 16

do_faultsim_test multiplex-5.1 -prep {
  catch {db close}
} -body {
  sqlite3 db test2.db
}
do_faultsim_test multiplex-5.2 -prep {
................................................................................
  catch {db close}
} -body {
  sqlite3 db test.db
}

catch { db close }
multiplex_delete test.db
multiplex_delete test2.db

do_test multiplex-5.3.prep {
  sqlite3 db test.db
  execsql {
    PRAGMA auto_vacuum = 1;
    PRAGMA page_size = 1024;
    CREATE TABLE t1(a, b);
................................................................................

do_test multiplex-5.4.1 {
  catch { db close }
  multiplex_delete test.db
  file mkdir test.db
  list [catch { sqlite3 db test.db } msg] $msg
} {1 {unable to open database file}}
catch { file delete test.db }

do_faultsim_test multiplex-5.5 -prep {
  catch { sqlite3_multiplex_shutdown }
} -body {
  sqlite3_multiplex_initialize "" 1
  multiplex_set 32768 16
}

catch { sqlite3_multiplex_shutdown }
finish_test