Home | History | Annotate | Download | only in test
      1 # 2010 November 1
      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 # This file implements regression tests for SQLite library.  The
     12 # focus of this file is testing that WAL databases may be accessed without
     13 # using the xShm primitives if the connection is in exclusive-mode.
     14 #
     15 
     16 set testdir [file dirname $argv0]
     17 source $testdir/tester.tcl
     18 set testprefix walnoshm
     19 ifcapable !wal {finish_test ; return }
     20 
     21 db close
     22 testvfs tvfsshm
     23 testvfs tvfs -default 1 -iversion 1 
     24 sqlite3 db test.db
     25 
     26 #--------------------------------------------------------------------------
     27 # Test that when using a version 1 VFS, a database can only be converted
     28 # to WAL mode after setting locking_mode=EXCLUSIVE. Also, test that if a
     29 # WAL database is opened using heap-memory for the WAL index, the connection
     30 # cannot change back to locking_mode=NORMAL while the database is still in
     31 # WAL mode.
     32 #
     33 do_execsql_test 1.1 {
     34   CREATE TABLE t1(x, y);
     35   INSERT INTO t1 VALUES(1, 2);
     36 }
     37 
     38 do_execsql_test 1.2 { 
     39   PRAGMA journal_mode = WAL;
     40   SELECT * FROM t1;
     41 } {delete 1 2}
     42 do_test 1.3 { file exists test.db-wal } {0}
     43 
     44 do_execsql_test 1.4 { 
     45   PRAGMA locking_mode = exclusive;
     46   PRAGMA journal_mode = WAL;
     47   SELECT * FROM t1;
     48 } {exclusive wal 1 2}
     49 do_test 1.5 { file exists test.db-wal } {1}
     50 
     51 do_execsql_test 1.6 { INSERT INTO t1 VALUES(3, 4) }
     52 
     53 do_execsql_test 1.7 {
     54   PRAGMA locking_mode = normal;
     55 } {exclusive}
     56 do_execsql_test 1.8 {
     57   PRAGMA journal_mode = delete;
     58   PRAGMA main.locking_mode;
     59 } {delete exclusive}
     60 do_execsql_test 1.9 {
     61   PRAGMA locking_mode = normal;
     62 } {normal}
     63 do_execsql_test 1.10 {
     64   SELECT * FROM t1;
     65 } {1 2 3 4}
     66 do_test 1.11 { file exists test.db-wal } {0}
     67 
     68 #-------------------------------------------------------------------------
     69 #
     70 # 2.1.*: Test that a connection using a version 1 VFS can open a WAL database
     71 #        and convert it to rollback mode if it is set to use
     72 #        locking_mode=exclusive.
     73 #
     74 # 2.2.*: Test that if the exclusive lock cannot be obtained while attempting
     75 #        the above, the operation fails and the WAL file is not opened.
     76 #
     77 do_execsql_test 2.1.1 {
     78   CREATE TABLE t2(x, y);
     79   INSERT INTO t2 VALUES('a', 'b');
     80   INSERT INTO t2 VALUES('c', 'd');
     81 }
     82 do_execsql_test 2.1.2 {
     83   PRAGMA locking_mode = exclusive;
     84   PRAGMA journal_mode = WAL;
     85   INSERT INTO t2 VALUES('e', 'f');
     86   INSERT INTO t2 VALUES('g', 'h');
     87 } {exclusive wal}
     88 
     89 do_test 2.1.3 {
     90   file copy -force test.db     test2.db
     91   file copy -force test.db-wal test2.db-wal
     92   sqlite3 db2 test2.db
     93   catchsql { SELECT * FROM t2 } db2
     94 } {1 {unable to open database file}}
     95 do_test 2.1.4 {
     96   catchsql { PRAGMA journal_mode = delete } db2
     97 } {1 {unable to open database file}}
     98 do_test 2.1.5 {
     99   execsql { 
    100     PRAGMA locking_mode = exclusive; 
    101     PRAGMA journal_mode = delete;
    102     SELECT * FROM t2;
    103   } db2
    104 } {exclusive delete a b c d e f g h}
    105 
    106 do_test 2.2.1 {
    107   file copy -force test.db     test2.db
    108   file copy -force test.db-wal test2.db-wal
    109   sqlite3 db3 test2.db -vfs tvfsshm
    110   sqlite3 db2 test2.db
    111   execsql { SELECT * FROM t2 } db3
    112 } {a b c d e f g h}
    113 
    114 do_test 2.2.2 {
    115   execsql  { PRAGMA locking_mode = exclusive }  db2
    116   catchsql { PRAGMA journal_mode = delete } db2
    117 } {1 {database is locked}}
    118 
    119 do_test 2.2.3 {
    120   # This is to test that [db2] is not holding a PENDING lock (which can 
    121   # happen when an attempt to obtain an EXCLUSIVE lock fails).
    122   sqlite3 db4 test2.db -vfs tvfsshm
    123   execsql { SELECT * FROM t2 } db4
    124 } {a b c d e f g h}
    125 
    126 do_test 2.2.4 {
    127   catchsql { SELECT * FROM t2 } db2
    128 } {1 {database is locked}}
    129 
    130 do_test 2.2.5 {
    131   db4 close
    132   sqlite3 db4 test2.db -vfs tvfsshm
    133   execsql { SELECT * FROM t2 } db4
    134 } {a b c d e f g h}
    135 
    136 do_test 2.2.6 {
    137   db3 close
    138   db4 close
    139   execsql { SELECT * FROM t2 } db2
    140 } {a b c d e f g h}
    141 
    142 db2 close
    143 db close
    144 
    145 #-------------------------------------------------------------------------
    146 #
    147 # 3.1: Test that if locking_mode=EXCLUSIVE is set after the wal file is
    148 #      opened, it is possible to drop back to locking_mode=NORMAL.
    149 #
    150 # 3.2: Test that if locking_mode=EXCLUSIVE is set before the wal file is
    151 #      opened, it is not.
    152 #
    153 do_test 3.1 {
    154   sqlite3 db test.db -vfs tvfsshm
    155   execsql { 
    156     SELECT * FROM t1;
    157     PRAGMA locking_mode = EXCLUSIVE;
    158     INSERT INTO t1 VALUES(5, 6);
    159     PRAGMA locking_mode = NORMAL;
    160     INSERT INTO t1 VALUES(7, 8);
    161   }
    162   sqlite3 db2 test.db -vfs tvfsshm
    163   execsql { SELECT * FROM t1 } db2
    164 } {1 2 3 4 5 6 7 8}
    165 db close
    166 db2 close
    167 do_test 3.2 {
    168   sqlite3 db  test.db -vfs tvfsshm
    169   execsql { 
    170     PRAGMA locking_mode = EXCLUSIVE;
    171     INSERT INTO t1 VALUES(9, 10);
    172     PRAGMA locking_mode = NORMAL;
    173     INSERT INTO t1 VALUES(11, 12);
    174   }
    175   sqlite3 db2 test.db -vfs tvfsshm
    176   catchsql { SELECT * FROM t1 } db2
    177 } {1 {database is locked}}
    178 db close
    179 db2 close
    180 
    181 tvfs delete
    182 tvfsshm delete
    183 
    184 finish_test
    185