/ Artifact Content
Login

Artifact ad3710e5ff4781317866b11875691173d2d6a8f7:


# 2010 October 29
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#

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.1 { sqlite3_multiplex_initialize nosuchvfs 1 } {SQLITE_ERROR}
do_test multiplex-1.2 { sqlite3_multiplex_initialize "" 1 }        {SQLITE_OK}
do_test multiplex-1.3 { sqlite3_multiplex_initialize "" 1 }        {SQLITE_MISUSE}
do_test multiplex-1.4 { sqlite3_multiplex_shutdown }               {SQLITE_OK}

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.1.*: Test simple writing to a multiplex file.
#
#   multiplex-2.2.*: More writing.
#
#   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;
  }
  execsql {
    CREATE TABLE t1(a, b);
    INSERT INTO t1 VALUES(1, randomblob(1100));
    INSERT INTO t1 VALUES(2, randomblob(1100));
  }
} {}
do_test multiplex-2.1.3 { file size test.db } {4096}
do_test multiplex-2.1.4 {
  execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
} {}

do_test multiplex-2.2.1 {
  execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
} {}
do_test multiplex-2.2.3 { file size test.db } {6144}

do_test multiplex-2.3.1 {
  sqlite3 db2 bak.db
  db2 close
} {}

do_test multiplex-2.4.1 {
  sqlite3_multiplex_shutdown
} {SQLITE_MISUSE}
do_test multiplex-2.4.2 {
  execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
} {}
do_test multiplex-2.4.4 { file size test.db } {7168}
do_test multiplex-2.4.99 {
  db close
  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;
    PRAGMA auto_vacuum = off;
    CREATE TABLE t1(a PRIMARY KEY, b);
  }
} {delete}

do_test multiplex-2.5.3 { 
  execsql { 
    INSERT INTO t1 VALUES(1, 'one');
    INSERT INTO t1 VALUES(2, randomblob(4000));
    INSERT INTO t1 VALUES(3, 'three');
    INSERT INTO t1 VALUES(4, randomblob(4000));
    INSERT INTO t1 VALUES(5, 'five') 
  }
} {}

do_test multiplex-2.5.4 {
  db eval {SELECT * FROM t1 WHERE a=1}
} {1 one}

do_test multiplex-2.5.5 {
  db eval {SELECT * FROM t1 WHERE a=3}
} {3 three}

do_test multiplex-2.5.6 {
  db eval {SELECT * FROM t1 WHERE a=5}
} {5 five}

do_test multiplex-2.5.7 {
  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;
    CREATE TABLE t1(a PRIMARY KEY, b);
    INSERT INTO t1 VALUES(1, 'one');
  }
  file size test.db
} {3072}
do_test multiplex-3.1.3 {
  sqlite3 db2 test.db
  execsql { CREATE TABLE t2(a, b) } db2
} {}
do_test multiplex-3.1.4 {
  execsql { CREATE TABLE t3(a, b) }
} {}
do_test multiplex-3.1.5 {
  catchsql { CREATE TABLE t3(a, b) }
} {1 {table t3 already exists}}
do_test multiplex-3.1.6 {
  db close
  db2 close
} {}

do_test multiplex-3.2.1a {

  multiplex_delete test.db
  multiplex_delete test2.db

  sqlite3 db1a test.db
  sqlite3 db2a test2.db

  foreach db {db1a db2a} {
    execsql {
      PRAGMA page_size = 1024;
      PRAGMA journal_mode = delete;
      PRAGMA auto_vacuum = off;
      CREATE TABLE t1(a, b);
    } $db
  }

  list [file size test.db] [file size test2.db]
} {2048 2048}

do_test multiplex-3.2.1b {
  sqlite3 db1b test.db
  sqlite3 db2b test2.db
} {}

do_test multiplex-3.2.2 { execsql { INSERT INTO t1 VALUES('x', 'y') } db1a } {}
do_test multiplex-3.2.3 { execsql { INSERT INTO t1 VALUES('v', 'w') } db1b } {}
do_test multiplex-3.2.4 { execsql { INSERT INTO t1 VALUES('t', 'u') } db2a } {}
do_test multiplex-3.2.5 { execsql { INSERT INTO t1 VALUES('r', 's') } db2b } {}

do_test multiplex-3.2.6 { 
  execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1a
} {}
do_test multiplex-3.2.7 { 
  execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1b
} {}
do_test multiplex-3.2.8 { 
  execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2a
} {}
do_test multiplex-3.2.9 { 
  execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2b
} {}

do_test multiplex-3.3.1 { 
  execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1a
  execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db1b
  execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2a
  execsql { INSERT INTO t1 VALUES(randomblob(500), randomblob(500)) } db2b
} {}

do_test multiplex-3.2.X {
  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]
  }
  return [lsort $allq]
}

do_test multiplex-4.1.6 {
  multiplex_delete test2.db
  sqlite3 db test2.db
  db eval {CREATE TABLE t2(x); INSERT INTO t2 VALUES('tab-t2');}
  set res [multiplex_list]
  list [regexp {test2.db} $res]
} {1}
do_test multiplex-4.1.6a {
  sqlite3 db2 test2.db
  db2 eval {SELECT * FROM t2}
} {tab-t2}
do_test multiplex-4.1.7 {
  execsql {INSERT INTO t2 VALUES(zeroblob(200000))}
} {}
do_test multiplex-4.1.8 {
  sqlite3 db2 test2.db
  db2 eval {SELECT count(*) FROM t2}
} {2}
do_test multiplex-4.1.8a {
   db2 eval { DELETE FROM t2 WHERE x = 'tab-t2' }
} {}
do_test multiplex-4.1.8b {
  sqlite3 db2 test2.db
  db2 eval {SELECT count(*) FROM t2}
} {1}


do_test multiplex-4.1.9 {
  execsql {INSERT INTO t2 VALUES(zeroblob(200000))}
} {}
do_test multiplex-4.1.10 {
  set res [multiplex_list]
  list [regexp {test2.db} $res]
} {1}
do_test multiplex-4.1.11 {
  db2 close
  set res [multiplex_list]
  list [regexp {test2.db} $res]
} {1}
do_test multiplex-4.1.12 {
  db close
  multiplex_list
} {}


#-------------------------------------------------------------------------
# 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);
    INSERT INTO t1 VALUES(10, zeroblob(1200));
  }
  faultsim_save_and_close
} {}
do_faultsim_test multiplex-5.3 -prep {
  faultsim_restore_and_reopen
} -body {
  execsql { DELETE FROM t1 }
}

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